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));
404 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
407 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
411 if (!Node->getInputName(i).empty()) {
413 OS << Node->getInputName(i);
417 VisitStringLiteral(Node->getInputConstraintLiteral(i));
419 Visit(Node->getInputExpr(i));
423 if (Node->getNumClobbers() != 0)
426 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
430 VisitStringLiteral(Node->getClobberStringLiteral(i));
434 if (Policy.IncludeNewlines) OS << "\n";
437 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
438 // FIXME: Implement MS style inline asm statement printer.
439 Indent() << "__asm ";
440 if (Node->hasBraces())
442 OS << Node->getAsmString() << "\n";
443 if (Node->hasBraces())
447 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
448 PrintStmt(Node->getCapturedDecl()->getBody());
451 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
453 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
454 PrintRawCompoundStmt(TS);
458 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
459 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
460 Indent() << "@catch(";
461 if (catchStmt->getCatchParamDecl()) {
462 if (Decl *DS = catchStmt->getCatchParamDecl())
466 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
467 PrintRawCompoundStmt(CS);
472 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
473 Node->getFinallyStmt())) {
474 Indent() << "@finally";
475 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
480 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
483 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
484 Indent() << "@catch (...) { /* todo */ } \n";
487 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
488 Indent() << "@throw";
489 if (Node->getThrowExpr()) {
491 PrintExpr(Node->getThrowExpr());
496 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
497 Indent() << "@synchronized (";
498 PrintExpr(Node->getSynchExpr());
500 PrintRawCompoundStmt(Node->getSynchBody());
504 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
505 Indent() << "@autoreleasepool";
506 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
510 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
512 if (Decl *ExDecl = Node->getExceptionDecl())
513 PrintRawDecl(ExDecl);
517 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
520 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
522 PrintRawCXXCatchStmt(Node);
526 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
528 PrintRawCompoundStmt(Node->getTryBlock());
529 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
531 PrintRawCXXCatchStmt(Node->getHandler(i));
536 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
537 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
538 PrintRawCompoundStmt(Node->getTryBlock());
539 SEHExceptStmt *E = Node->getExceptHandler();
540 SEHFinallyStmt *F = Node->getFinallyHandler();
542 PrintRawSEHExceptHandler(E);
544 assert(F && "Must have a finally block...");
545 PrintRawSEHFinallyStmt(F);
550 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
552 PrintRawCompoundStmt(Node->getBlock());
556 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
558 VisitExpr(Node->getFilterExpr());
560 PrintRawCompoundStmt(Node->getBlock());
564 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
566 PrintRawSEHExceptHandler(Node);
570 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
572 PrintRawSEHFinallyStmt(Node);
576 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
577 Indent() << "__leave;";
578 if (Policy.IncludeNewlines) OS << "\n";
581 //===----------------------------------------------------------------------===//
582 // OpenMP clauses printing methods
583 //===----------------------------------------------------------------------===//
586 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
588 const PrintingPolicy &Policy;
589 /// \brief Process clauses with list of variables.
590 template <typename T>
591 void VisitOMPClauseList(T *Node, char StartSym);
593 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
594 : OS(OS), Policy(Policy) { }
595 #define OPENMP_CLAUSE(Name, Class) \
596 void Visit##Class(Class *S);
597 #include "clang/Basic/OpenMPKinds.def"
600 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
602 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
606 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
608 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
612 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
613 OS << "num_threads(";
614 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
618 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
620 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
624 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
626 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
630 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
632 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
636 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
638 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
642 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
644 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
645 if (Node->getChunkSize()) {
647 Node->getChunkSize()->printPretty(OS, nullptr, Policy);
652 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) {
656 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
660 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
664 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
668 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
670 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
672 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
676 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
680 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
685 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
686 for (typename T::varlist_iterator I = Node->varlist_begin(),
687 E = Node->varlist_end();
689 assert(*I && "Expected non-null Stmt");
690 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
691 OS << (I == Node->varlist_begin() ? StartSym : ',');
692 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
694 OS << (I == Node->varlist_begin() ? StartSym : ',');
695 (*I)->printPretty(OS, nullptr, Policy, 0);
700 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
701 if (!Node->varlist_empty()) {
703 VisitOMPClauseList(Node, '(');
708 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
709 if (!Node->varlist_empty()) {
710 OS << "firstprivate";
711 VisitOMPClauseList(Node, '(');
716 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
717 if (!Node->varlist_empty()) {
719 VisitOMPClauseList(Node, '(');
724 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
725 if (!Node->varlist_empty()) {
727 VisitOMPClauseList(Node, '(');
732 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
733 if (!Node->varlist_empty()) {
735 NestedNameSpecifier *QualifierLoc =
736 Node->getQualifierLoc().getNestedNameSpecifier();
737 OverloadedOperatorKind OOK =
738 Node->getNameInfo().getName().getCXXOverloadedOperator();
739 if (QualifierLoc == nullptr && OOK != OO_None) {
740 // Print reduction identifier in C format
741 OS << getOperatorSpelling(OOK);
744 if (QualifierLoc != nullptr)
745 QualifierLoc->print(OS, Policy);
746 OS << Node->getNameInfo();
749 VisitOMPClauseList(Node, ' ');
754 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
755 if (!Node->varlist_empty()) {
757 VisitOMPClauseList(Node, '(');
758 if (Node->getStep() != nullptr) {
760 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
766 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
767 if (!Node->varlist_empty()) {
769 VisitOMPClauseList(Node, '(');
770 if (Node->getAlignment() != nullptr) {
772 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
778 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
779 if (!Node->varlist_empty()) {
781 VisitOMPClauseList(Node, '(');
786 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
787 if (!Node->varlist_empty()) {
789 VisitOMPClauseList(Node, '(');
794 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
795 if (!Node->varlist_empty()) {
796 VisitOMPClauseList(Node, '(');
802 //===----------------------------------------------------------------------===//
803 // OpenMP directives printing methods
804 //===----------------------------------------------------------------------===//
806 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
807 OMPClausePrinter Printer(OS, Policy);
808 ArrayRef<OMPClause *> Clauses = S->clauses();
809 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
811 if (*I && !(*I)->isImplicit()) {
816 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
817 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
818 "Expected captured statement!");
819 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
824 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
825 Indent() << "#pragma omp parallel ";
826 PrintOMPExecutableDirective(Node);
829 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
830 Indent() << "#pragma omp simd ";
831 PrintOMPExecutableDirective(Node);
834 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
835 Indent() << "#pragma omp for ";
836 PrintOMPExecutableDirective(Node);
839 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
840 Indent() << "#pragma omp for simd ";
841 PrintOMPExecutableDirective(Node);
844 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
845 Indent() << "#pragma omp sections ";
846 PrintOMPExecutableDirective(Node);
849 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
850 Indent() << "#pragma omp section";
851 PrintOMPExecutableDirective(Node);
854 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
855 Indent() << "#pragma omp single ";
856 PrintOMPExecutableDirective(Node);
859 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
860 Indent() << "#pragma omp master";
861 PrintOMPExecutableDirective(Node);
864 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
865 Indent() << "#pragma omp critical";
866 if (Node->getDirectiveName().getName()) {
868 Node->getDirectiveName().printName(OS);
871 PrintOMPExecutableDirective(Node);
874 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
875 Indent() << "#pragma omp parallel for ";
876 PrintOMPExecutableDirective(Node);
879 void StmtPrinter::VisitOMPParallelForSimdDirective(
880 OMPParallelForSimdDirective *Node) {
881 Indent() << "#pragma omp parallel for simd ";
882 PrintOMPExecutableDirective(Node);
885 void StmtPrinter::VisitOMPParallelSectionsDirective(
886 OMPParallelSectionsDirective *Node) {
887 Indent() << "#pragma omp parallel sections ";
888 PrintOMPExecutableDirective(Node);
891 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
892 Indent() << "#pragma omp task ";
893 PrintOMPExecutableDirective(Node);
896 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
897 Indent() << "#pragma omp taskyield";
898 PrintOMPExecutableDirective(Node);
901 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
902 Indent() << "#pragma omp barrier";
903 PrintOMPExecutableDirective(Node);
906 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
907 Indent() << "#pragma omp taskwait";
908 PrintOMPExecutableDirective(Node);
911 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
912 Indent() << "#pragma omp flush ";
913 PrintOMPExecutableDirective(Node);
916 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
917 Indent() << "#pragma omp ordered";
918 PrintOMPExecutableDirective(Node);
921 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
922 Indent() << "#pragma omp atomic ";
923 PrintOMPExecutableDirective(Node);
926 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
927 Indent() << "#pragma omp target ";
928 PrintOMPExecutableDirective(Node);
931 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
932 Indent() << "#pragma omp teams ";
933 PrintOMPExecutableDirective(Node);
936 //===----------------------------------------------------------------------===//
937 // Expr printing methods.
938 //===----------------------------------------------------------------------===//
940 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
941 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
942 Qualifier->print(OS, Policy);
943 if (Node->hasTemplateKeyword())
945 OS << Node->getNameInfo();
946 if (Node->hasExplicitTemplateArgs())
947 TemplateSpecializationType::PrintTemplateArgumentList(
948 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
951 void StmtPrinter::VisitDependentScopeDeclRefExpr(
952 DependentScopeDeclRefExpr *Node) {
953 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
954 Qualifier->print(OS, Policy);
955 if (Node->hasTemplateKeyword())
957 OS << Node->getNameInfo();
958 if (Node->hasExplicitTemplateArgs())
959 TemplateSpecializationType::PrintTemplateArgumentList(
960 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
963 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
964 if (Node->getQualifier())
965 Node->getQualifier()->print(OS, Policy);
966 if (Node->hasTemplateKeyword())
968 OS << Node->getNameInfo();
969 if (Node->hasExplicitTemplateArgs())
970 TemplateSpecializationType::PrintTemplateArgumentList(
971 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
974 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
975 if (Node->getBase()) {
976 PrintExpr(Node->getBase());
977 OS << (Node->isArrow() ? "->" : ".");
979 OS << *Node->getDecl();
982 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
983 if (Node->isSuperReceiver())
985 else if (Node->isObjectReceiver() && Node->getBase()) {
986 PrintExpr(Node->getBase());
988 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
989 OS << Node->getClassReceiver()->getName() << ".";
992 if (Node->isImplicitProperty())
993 Node->getImplicitPropertyGetter()->getSelector().print(OS);
995 OS << Node->getExplicitProperty()->getName();
998 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1000 PrintExpr(Node->getBaseExpr());
1002 PrintExpr(Node->getKeyExpr());
1006 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1007 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1010 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1011 unsigned value = Node->getValue();
1013 switch (Node->getKind()) {
1014 case CharacterLiteral::Ascii: break; // no prefix.
1015 case CharacterLiteral::Wide: OS << 'L'; break;
1016 case CharacterLiteral::UTF16: OS << 'u'; break;
1017 case CharacterLiteral::UTF32: OS << 'U'; break;
1028 // TODO: K&R: the meaning of '\\a' is different in traditional C
1034 // Nonstandard escape sequence.
1054 if (value < 256 && isPrintable((unsigned char)value))
1055 OS << "'" << (char)value << "'";
1056 else if (value < 256)
1057 OS << "'\\x" << llvm::format("%02x", value) << "'";
1058 else if (value <= 0xFFFF)
1059 OS << "'\\u" << llvm::format("%04x", value) << "'";
1061 OS << "'\\U" << llvm::format("%08x", value) << "'";
1065 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1066 bool isSigned = Node->getType()->isSignedIntegerType();
1067 OS << Node->getValue().toString(10, isSigned);
1069 // Emit suffixes. Integer literals are always a builtin integer type.
1070 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1071 default: llvm_unreachable("Unexpected type for integer literal!");
1072 case BuiltinType::SChar: OS << "i8"; break;
1073 case BuiltinType::UChar: OS << "Ui8"; break;
1074 case BuiltinType::Short: OS << "i16"; break;
1075 case BuiltinType::UShort: OS << "Ui16"; break;
1076 case BuiltinType::Int: break; // no suffix.
1077 case BuiltinType::UInt: OS << 'U'; break;
1078 case BuiltinType::Long: OS << 'L'; break;
1079 case BuiltinType::ULong: OS << "UL"; break;
1080 case BuiltinType::LongLong: OS << "LL"; break;
1081 case BuiltinType::ULongLong: OS << "ULL"; break;
1082 case BuiltinType::Int128: OS << "i128"; break;
1083 case BuiltinType::UInt128: OS << "Ui128"; break;
1087 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1089 SmallString<16> Str;
1090 Node->getValue().toString(Str);
1092 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1093 OS << '.'; // Trailing dot in order to separate from ints.
1098 // Emit suffixes. Float literals are always a builtin float type.
1099 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1100 default: llvm_unreachable("Unexpected type for float literal!");
1101 case BuiltinType::Half: break; // FIXME: suffix?
1102 case BuiltinType::Double: break; // no suffix.
1103 case BuiltinType::Float: OS << 'F'; break;
1104 case BuiltinType::LongDouble: OS << 'L'; break;
1108 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1109 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1112 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1113 PrintExpr(Node->getSubExpr());
1117 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1118 Str->outputString(OS);
1120 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1122 PrintExpr(Node->getSubExpr());
1125 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1126 if (!Node->isPostfix()) {
1127 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1129 // Print a space if this is an "identifier operator" like __real, or if
1130 // it might be concatenated incorrectly like '+'.
1131 switch (Node->getOpcode()) {
1140 if (isa<UnaryOperator>(Node->getSubExpr()))
1145 PrintExpr(Node->getSubExpr());
1147 if (Node->isPostfix())
1148 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1151 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1152 OS << "__builtin_offsetof(";
1153 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1155 bool PrintedSomething = false;
1156 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1157 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1158 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1161 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1163 PrintedSomething = true;
1167 // Skip implicit base indirections.
1168 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1171 // Field or identifier node.
1172 IdentifierInfo *Id = ON.getFieldName();
1176 if (PrintedSomething)
1179 PrintedSomething = true;
1180 OS << Id->getName();
1185 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1186 switch(Node->getKind()) {
1191 if (Policy.LangOpts.CPlusPlus)
1193 else if (Policy.LangOpts.C11)
1202 if (Node->isArgumentType()) {
1204 Node->getArgumentType().print(OS, Policy);
1208 PrintExpr(Node->getArgumentExpr());
1212 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1214 PrintExpr(Node->getControllingExpr());
1215 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1217 QualType T = Node->getAssocType(i);
1221 T.print(OS, Policy);
1223 PrintExpr(Node->getAssocExpr(i));
1228 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1229 PrintExpr(Node->getLHS());
1231 PrintExpr(Node->getRHS());
1235 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1236 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1237 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1238 // Don't print any defaulted arguments
1243 PrintExpr(Call->getArg(i));
1247 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1248 PrintExpr(Call->getCallee());
1250 PrintCallArgs(Call);
1253 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1254 // FIXME: Suppress printing implicit bases (like "this")
1255 PrintExpr(Node->getBase());
1257 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1258 FieldDecl *ParentDecl = ParentMember
1259 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1261 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1262 OS << (Node->isArrow() ? "->" : ".");
1264 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1265 if (FD->isAnonymousStructOrUnion())
1268 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1269 Qualifier->print(OS, Policy);
1270 if (Node->hasTemplateKeyword())
1272 OS << Node->getMemberNameInfo();
1273 if (Node->hasExplicitTemplateArgs())
1274 TemplateSpecializationType::PrintTemplateArgumentList(
1275 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1277 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1278 PrintExpr(Node->getBase());
1279 OS << (Node->isArrow() ? "->isa" : ".isa");
1282 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1283 PrintExpr(Node->getBase());
1285 OS << Node->getAccessor().getName();
1287 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1289 Node->getTypeAsWritten().print(OS, Policy);
1291 PrintExpr(Node->getSubExpr());
1293 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1295 Node->getType().print(OS, Policy);
1297 PrintExpr(Node->getInitializer());
1299 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1300 // No need to print anything, simply forward to the subexpression.
1301 PrintExpr(Node->getSubExpr());
1303 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1304 PrintExpr(Node->getLHS());
1305 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1306 PrintExpr(Node->getRHS());
1308 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1309 PrintExpr(Node->getLHS());
1310 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1311 PrintExpr(Node->getRHS());
1313 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1314 PrintExpr(Node->getCond());
1316 PrintExpr(Node->getLHS());
1318 PrintExpr(Node->getRHS());
1324 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1325 PrintExpr(Node->getCommon());
1327 PrintExpr(Node->getFalseExpr());
1329 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1330 OS << "&&" << Node->getLabel()->getName();
1333 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1335 PrintRawCompoundStmt(E->getSubStmt());
1339 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1340 OS << "__builtin_choose_expr(";
1341 PrintExpr(Node->getCond());
1343 PrintExpr(Node->getLHS());
1345 PrintExpr(Node->getRHS());
1349 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1353 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1354 OS << "__builtin_shufflevector(";
1355 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1357 PrintExpr(Node->getExpr(i));
1362 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1363 OS << "__builtin_convertvector(";
1364 PrintExpr(Node->getSrcExpr());
1366 Node->getType().print(OS, Policy);
1370 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1371 if (Node->getSyntacticForm()) {
1372 Visit(Node->getSyntacticForm());
1377 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1379 if (Node->getInit(i))
1380 PrintExpr(Node->getInit(i));
1387 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1389 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1391 PrintExpr(Node->getExpr(i));
1396 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1397 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1398 DEnd = Node->designators_end();
1400 if (D->isFieldDesignator()) {
1401 if (D->getDotLoc().isInvalid()) {
1402 if (IdentifierInfo *II = D->getFieldName())
1403 OS << II->getName() << ":";
1405 OS << "." << D->getFieldName()->getName();
1409 if (D->isArrayDesignator()) {
1410 PrintExpr(Node->getArrayIndex(*D));
1412 PrintExpr(Node->getArrayRangeStart(*D));
1414 PrintExpr(Node->getArrayRangeEnd(*D));
1421 PrintExpr(Node->getInit());
1424 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1425 if (Policy.LangOpts.CPlusPlus) {
1426 OS << "/*implicit*/";
1427 Node->getType().print(OS, Policy);
1430 OS << "/*implicit*/(";
1431 Node->getType().print(OS, Policy);
1433 if (Node->getType()->isRecordType())
1440 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1441 OS << "__builtin_va_arg(";
1442 PrintExpr(Node->getSubExpr());
1444 Node->getType().print(OS, Policy);
1448 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1449 PrintExpr(Node->getSyntacticForm());
1452 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1453 const char *Name = nullptr;
1454 switch (Node->getOp()) {
1455 #define BUILTIN(ID, TYPE, ATTRS)
1456 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1457 case AtomicExpr::AO ## ID: \
1460 #include "clang/Basic/Builtins.def"
1464 // AtomicExpr stores its subexpressions in a permuted order.
1465 PrintExpr(Node->getPtr());
1466 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1467 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1469 PrintExpr(Node->getVal1());
1471 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1472 Node->isCmpXChg()) {
1474 PrintExpr(Node->getVal2());
1476 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1477 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1479 PrintExpr(Node->getWeak());
1481 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1483 PrintExpr(Node->getOrder());
1485 if (Node->isCmpXChg()) {
1487 PrintExpr(Node->getOrderFail());
1493 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1494 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1496 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1498 #include "clang/Basic/OperatorKinds.def"
1501 OverloadedOperatorKind Kind = Node->getOperator();
1502 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1503 if (Node->getNumArgs() == 1) {
1504 OS << OpStrings[Kind] << ' ';
1505 PrintExpr(Node->getArg(0));
1507 PrintExpr(Node->getArg(0));
1508 OS << ' ' << OpStrings[Kind];
1510 } else if (Kind == OO_Arrow) {
1511 PrintExpr(Node->getArg(0));
1512 } else if (Kind == OO_Call) {
1513 PrintExpr(Node->getArg(0));
1515 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1518 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1519 PrintExpr(Node->getArg(ArgIdx));
1522 } else if (Kind == OO_Subscript) {
1523 PrintExpr(Node->getArg(0));
1525 PrintExpr(Node->getArg(1));
1527 } else if (Node->getNumArgs() == 1) {
1528 OS << OpStrings[Kind] << ' ';
1529 PrintExpr(Node->getArg(0));
1530 } else if (Node->getNumArgs() == 2) {
1531 PrintExpr(Node->getArg(0));
1532 OS << ' ' << OpStrings[Kind] << ' ';
1533 PrintExpr(Node->getArg(1));
1535 llvm_unreachable("unknown overloaded operator");
1539 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1540 // If we have a conversion operator call only print the argument.
1541 CXXMethodDecl *MD = Node->getMethodDecl();
1542 if (MD && isa<CXXConversionDecl>(MD)) {
1543 PrintExpr(Node->getImplicitObjectArgument());
1546 VisitCallExpr(cast<CallExpr>(Node));
1549 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1550 PrintExpr(Node->getCallee());
1552 PrintCallArgs(Node->getConfig());
1554 PrintCallArgs(Node);
1558 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1559 OS << Node->getCastName() << '<';
1560 Node->getTypeAsWritten().print(OS, Policy);
1562 PrintExpr(Node->getSubExpr());
1566 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1567 VisitCXXNamedCastExpr(Node);
1570 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1571 VisitCXXNamedCastExpr(Node);
1574 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1575 VisitCXXNamedCastExpr(Node);
1578 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1579 VisitCXXNamedCastExpr(Node);
1582 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1584 if (Node->isTypeOperand()) {
1585 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1587 PrintExpr(Node->getExprOperand());
1592 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1594 if (Node->isTypeOperand()) {
1595 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1597 PrintExpr(Node->getExprOperand());
1602 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1603 PrintExpr(Node->getBaseExpr());
1604 if (Node->isArrow())
1608 if (NestedNameSpecifier *Qualifier =
1609 Node->getQualifierLoc().getNestedNameSpecifier())
1610 Qualifier->print(OS, Policy);
1611 OS << Node->getPropertyDecl()->getDeclName();
1614 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1615 switch (Node->getLiteralOperatorKind()) {
1616 case UserDefinedLiteral::LOK_Raw:
1617 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1619 case UserDefinedLiteral::LOK_Template: {
1620 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1621 const TemplateArgumentList *Args =
1622 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1624 const TemplateArgument &Pack = Args->get(0);
1625 for (const auto &P : Pack.pack_elements()) {
1626 char C = (char)P.getAsIntegral().getZExtValue();
1631 case UserDefinedLiteral::LOK_Integer: {
1632 // Print integer literal without suffix.
1633 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1634 OS << Int->getValue().toString(10, /*isSigned*/false);
1637 case UserDefinedLiteral::LOK_Floating: {
1638 // Print floating literal without suffix.
1639 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1640 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1643 case UserDefinedLiteral::LOK_String:
1644 case UserDefinedLiteral::LOK_Character:
1645 PrintExpr(Node->getCookedLiteral());
1648 OS << Node->getUDSuffix()->getName();
1651 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1652 OS << (Node->getValue() ? "true" : "false");
1655 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1659 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1663 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1664 if (!Node->getSubExpr())
1668 PrintExpr(Node->getSubExpr());
1672 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1673 // Nothing to print: we picked up the default argument.
1676 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1677 // Nothing to print: we picked up the default initializer.
1680 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1681 Node->getType().print(OS, Policy);
1683 PrintExpr(Node->getSubExpr());
1687 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1688 PrintExpr(Node->getSubExpr());
1691 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1692 Node->getType().print(OS, Policy);
1694 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1695 ArgEnd = Node->arg_end();
1696 Arg != ArgEnd; ++Arg) {
1697 if (Arg->isDefaultArgument())
1699 if (Arg != Node->arg_begin())
1706 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1708 bool NeedComma = false;
1709 switch (Node->getCaptureDefault()) {
1723 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1724 CEnd = Node->explicit_capture_end();
1731 switch (C->getCaptureKind()) {
1737 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
1739 OS << C->getCapturedVar()->getName();
1743 OS << C->getCapturedVar()->getName();
1746 llvm_unreachable("VLA type in explicit captures.");
1749 if (C->isInitCapture())
1750 PrintExpr(C->getCapturedVar()->getInit());
1754 if (Node->hasExplicitParameters()) {
1756 CXXMethodDecl *Method = Node->getCallOperator();
1758 for (auto P : Method->params()) {
1764 std::string ParamStr = P->getNameAsString();
1765 P->getOriginalType().print(OS, Policy, ParamStr);
1767 if (Method->isVariadic()) {
1774 if (Node->isMutable())
1777 const FunctionProtoType *Proto
1778 = Method->getType()->getAs<FunctionProtoType>();
1779 Proto->printExceptionSpecification(OS, Policy);
1781 // FIXME: Attributes
1783 // Print the trailing return type if it was specified in the source.
1784 if (Node->hasExplicitResultType()) {
1786 Proto->getReturnType().print(OS, Policy);
1791 CompoundStmt *Body = Node->getBody();
1796 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1797 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1798 TSInfo->getType().print(OS, Policy);
1800 Node->getType().print(OS, Policy);
1804 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1805 if (E->isGlobalNew())
1808 unsigned NumPlace = E->getNumPlacementArgs();
1809 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1811 PrintExpr(E->getPlacementArg(0));
1812 for (unsigned i = 1; i < NumPlace; ++i) {
1813 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1816 PrintExpr(E->getPlacementArg(i));
1820 if (E->isParenTypeId())
1823 if (Expr *Size = E->getArraySize()) {
1824 llvm::raw_string_ostream s(TypeS);
1826 Size->printPretty(s, Helper, Policy);
1829 E->getAllocatedType().print(OS, Policy, TypeS);
1830 if (E->isParenTypeId())
1833 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1835 if (InitStyle == CXXNewExpr::CallInit)
1837 PrintExpr(E->getInitializer());
1838 if (InitStyle == CXXNewExpr::CallInit)
1843 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1844 if (E->isGlobalDelete())
1847 if (E->isArrayForm())
1849 PrintExpr(E->getArgument());
1852 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1853 PrintExpr(E->getBase());
1858 if (E->getQualifier())
1859 E->getQualifier()->print(OS, Policy);
1862 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1863 OS << II->getName();
1865 E->getDestroyedType().print(OS, Policy);
1868 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1869 if (E->isListInitialization())
1872 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1873 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1874 // Don't print any defaulted arguments
1879 PrintExpr(E->getArg(i));
1882 if (E->isListInitialization())
1886 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1887 PrintExpr(E->getSubExpr());
1890 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1891 // Just forward to the subexpression.
1892 PrintExpr(E->getSubExpr());
1896 StmtPrinter::VisitCXXUnresolvedConstructExpr(
1897 CXXUnresolvedConstructExpr *Node) {
1898 Node->getTypeAsWritten().print(OS, Policy);
1900 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1901 ArgEnd = Node->arg_end();
1902 Arg != ArgEnd; ++Arg) {
1903 if (Arg != Node->arg_begin())
1910 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1911 CXXDependentScopeMemberExpr *Node) {
1912 if (!Node->isImplicitAccess()) {
1913 PrintExpr(Node->getBase());
1914 OS << (Node->isArrow() ? "->" : ".");
1916 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1917 Qualifier->print(OS, Policy);
1918 if (Node->hasTemplateKeyword())
1920 OS << Node->getMemberNameInfo();
1921 if (Node->hasExplicitTemplateArgs())
1922 TemplateSpecializationType::PrintTemplateArgumentList(
1923 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1926 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1927 if (!Node->isImplicitAccess()) {
1928 PrintExpr(Node->getBase());
1929 OS << (Node->isArrow() ? "->" : ".");
1931 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1932 Qualifier->print(OS, Policy);
1933 if (Node->hasTemplateKeyword())
1935 OS << Node->getMemberNameInfo();
1936 if (Node->hasExplicitTemplateArgs())
1937 TemplateSpecializationType::PrintTemplateArgumentList(
1938 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1941 static const char *getTypeTraitName(TypeTrait TT) {
1943 #define TYPE_TRAIT_1(Spelling, Name, Key) \
1944 case clang::UTT_##Name: return #Spelling;
1945 #define TYPE_TRAIT_2(Spelling, Name, Key) \
1946 case clang::BTT_##Name: return #Spelling;
1947 #define TYPE_TRAIT_N(Spelling, Name, Key) \
1948 case clang::TT_##Name: return #Spelling;
1949 #include "clang/Basic/TokenKinds.def"
1951 llvm_unreachable("Type trait not covered by switch");
1954 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1956 case ATT_ArrayRank: return "__array_rank";
1957 case ATT_ArrayExtent: return "__array_extent";
1959 llvm_unreachable("Array type trait not covered by switch");
1962 static const char *getExpressionTraitName(ExpressionTrait ET) {
1964 case ET_IsLValueExpr: return "__is_lvalue_expr";
1965 case ET_IsRValueExpr: return "__is_rvalue_expr";
1967 llvm_unreachable("Expression type trait not covered by switch");
1970 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1971 OS << getTypeTraitName(E->getTrait()) << "(";
1972 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1975 E->getArg(I)->getType().print(OS, Policy);
1980 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1981 OS << getTypeTraitName(E->getTrait()) << '(';
1982 E->getQueriedType().print(OS, Policy);
1986 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1987 OS << getExpressionTraitName(E->getTrait()) << '(';
1988 PrintExpr(E->getQueriedExpression());
1992 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1994 PrintExpr(E->getOperand());
1998 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1999 PrintExpr(E->getPattern());
2003 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2004 OS << "sizeof...(" << *E->getPack() << ")";
2007 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2008 SubstNonTypeTemplateParmPackExpr *Node) {
2009 OS << *Node->getParameterPack();
2012 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2013 SubstNonTypeTemplateParmExpr *Node) {
2014 Visit(Node->getReplacement());
2017 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2018 OS << *E->getParameterPack();
2021 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2022 PrintExpr(Node->GetTemporaryExpr());
2025 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2028 PrintExpr(E->getLHS());
2029 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2033 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2034 PrintExpr(E->getRHS());
2041 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2043 VisitStringLiteral(Node->getString());
2046 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2048 Visit(E->getSubExpr());
2051 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2053 StmtRange ch = E->children();
2054 if (ch.first != ch.second) {
2058 if (ch.first == ch.second) break;
2065 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2067 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2071 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2074 Visit(Element.Value);
2075 if (Element.isPackExpansion())
2081 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2083 Node->getEncodedType().print(OS, Policy);
2087 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2089 Node->getSelector().print(OS);
2093 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2094 OS << "@protocol(" << *Node->getProtocol() << ')';
2097 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2099 switch (Mess->getReceiverKind()) {
2100 case ObjCMessageExpr::Instance:
2101 PrintExpr(Mess->getInstanceReceiver());
2104 case ObjCMessageExpr::Class:
2105 Mess->getClassReceiver().print(OS, Policy);
2108 case ObjCMessageExpr::SuperInstance:
2109 case ObjCMessageExpr::SuperClass:
2115 Selector selector = Mess->getSelector();
2116 if (selector.isUnarySelector()) {
2117 OS << selector.getNameForSlot(0);
2119 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2120 if (i < selector.getNumArgs()) {
2121 if (i > 0) OS << ' ';
2122 if (selector.getIdentifierInfoForSlot(i))
2123 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2127 else OS << ", "; // Handle variadic methods.
2129 PrintExpr(Mess->getArg(i));
2135 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2136 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2140 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2141 PrintExpr(E->getSubExpr());
2145 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2146 OS << '(' << E->getBridgeKindName();
2147 E->getType().print(OS, Policy);
2149 PrintExpr(E->getSubExpr());
2152 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2153 BlockDecl *BD = Node->getBlockDecl();
2156 const FunctionType *AFT = Node->getFunctionType();
2158 if (isa<FunctionNoProtoType>(AFT)) {
2160 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2162 for (BlockDecl::param_iterator AI = BD->param_begin(),
2163 E = BD->param_end(); AI != E; ++AI) {
2164 if (AI != BD->param_begin()) OS << ", ";
2165 std::string ParamStr = (*AI)->getNameAsString();
2166 (*AI)->getType().print(OS, Policy, ParamStr);
2169 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2170 if (FT->isVariadic()) {
2171 if (!BD->param_empty()) OS << ", ";
2179 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2180 PrintExpr(Node->getSourceExpr());
2183 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2184 // TODO: Print something reasonable for a TypoExpr, if necessary.
2185 assert(false && "Cannot print TypoExpr nodes");
2188 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2189 OS << "__builtin_astype(";
2190 PrintExpr(Node->getSrcExpr());
2192 Node->getType().print(OS, Policy);
2196 //===----------------------------------------------------------------------===//
2197 // Stmt method implementations
2198 //===----------------------------------------------------------------------===//
2200 void Stmt::dumpPretty(const ASTContext &Context) const {
2201 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2204 void Stmt::printPretty(raw_ostream &OS,
2205 PrinterHelper *Helper,
2206 const PrintingPolicy &Policy,
2207 unsigned Indentation) const {
2208 StmtPrinter P(OS, Helper, Policy, Indentation);
2209 P.Visit(const_cast<Stmt*>(this));
2212 //===----------------------------------------------------------------------===//
2214 //===----------------------------------------------------------------------===//
2216 // Implement virtual destructor.
2217 PrinterHelper::~PrinterHelper() {}