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/Decl.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "clang/AST/DeclTemplate.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/AST/ExprObjC.h"
26 #include "clang/AST/ExprOpenMP.h"
27 #include "clang/AST/NestedNameSpecifier.h"
28 #include "clang/AST/OpenMPClause.h"
29 #include "clang/AST/PrettyPrinter.h"
30 #include "clang/AST/Stmt.h"
31 #include "clang/AST/StmtCXX.h"
32 #include "clang/AST/StmtObjC.h"
33 #include "clang/AST/StmtOpenMP.h"
34 #include "clang/AST/StmtVisitor.h"
35 #include "clang/AST/TemplateBase.h"
36 #include "clang/AST/Type.h"
37 #include "clang/Basic/CharInfo.h"
38 #include "clang/Basic/ExpressionTraits.h"
39 #include "clang/Basic/IdentifierTable.h"
40 #include "clang/Basic/LLVM.h"
41 #include "clang/Basic/Lambda.h"
42 #include "clang/Basic/OpenMPKinds.h"
43 #include "clang/Basic/OperatorKinds.h"
44 #include "clang/Basic/SourceLocation.h"
45 #include "clang/Basic/TypeTraits.h"
46 #include "clang/Lex/Lexer.h"
47 #include "llvm/ADT/ArrayRef.h"
48 #include "llvm/ADT/SmallString.h"
49 #include "llvm/ADT/SmallVector.h"
50 #include "llvm/ADT/StringRef.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/Compiler.h"
53 #include "llvm/Support/ErrorHandling.h"
54 #include "llvm/Support/Format.h"
55 #include "llvm/Support/raw_ostream.h"
59 using namespace clang;
61 //===----------------------------------------------------------------------===//
62 // StmtPrinter Visitor
63 //===----------------------------------------------------------------------===//
67 class StmtPrinter : public StmtVisitor<StmtPrinter> {
70 PrinterHelper* Helper;
71 PrintingPolicy Policy;
72 const ASTContext *Context;
75 StmtPrinter(raw_ostream &os, PrinterHelper *helper,
76 const PrintingPolicy &Policy, unsigned Indentation = 0,
77 const ASTContext *Context = nullptr)
78 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
81 void PrintStmt(Stmt *S) {
82 PrintStmt(S, Policy.Indentation);
85 void PrintStmt(Stmt *S, int SubIndent) {
86 IndentLevel += SubIndent;
87 if (S && isa<Expr>(S)) {
88 // If this is an expr used in a stmt context, indent and newline it.
95 Indent() << "<<<NULL STATEMENT>>>\n";
97 IndentLevel -= SubIndent;
100 void PrintRawCompoundStmt(CompoundStmt *S);
101 void PrintRawDecl(Decl *D);
102 void PrintRawDeclStmt(const DeclStmt *S);
103 void PrintRawIfStmt(IfStmt *If);
104 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
105 void PrintCallArgs(CallExpr *E);
106 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
107 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
108 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
109 bool ForceNoStmt = false);
111 void PrintExpr(Expr *E) {
118 raw_ostream &Indent(int Delta = 0) {
119 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
124 void Visit(Stmt* S) {
125 if (Helper && Helper->handledStmt(S,OS))
127 else StmtVisitor<StmtPrinter>::Visit(S);
130 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
131 Indent() << "<<unknown stmt type>>\n";
134 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
135 OS << "<<unknown expr type>>";
138 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
140 #define ABSTRACT_STMT(CLASS)
141 #define STMT(CLASS, PARENT) \
142 void Visit##CLASS(CLASS *Node);
143 #include "clang/AST/StmtNodes.inc"
148 //===----------------------------------------------------------------------===//
149 // Stmt printing methods.
150 //===----------------------------------------------------------------------===//
152 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
153 /// with no newline after the }.
154 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
156 for (auto *I : Node->body())
162 void StmtPrinter::PrintRawDecl(Decl *D) {
163 D->print(OS, Policy, IndentLevel);
166 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
167 SmallVector<Decl *, 2> Decls(S->decls());
168 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
171 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
175 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
177 PrintRawDeclStmt(Node);
181 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
183 PrintRawCompoundStmt(Node);
187 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
188 Indent(-1) << "case ";
189 PrintExpr(Node->getLHS());
190 if (Node->getRHS()) {
192 PrintExpr(Node->getRHS());
196 PrintStmt(Node->getSubStmt(), 0);
199 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
200 Indent(-1) << "default:\n";
201 PrintStmt(Node->getSubStmt(), 0);
204 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
205 Indent(-1) << Node->getName() << ":\n";
206 PrintStmt(Node->getSubStmt(), 0);
209 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
210 for (const auto *Attr : Node->getAttrs()) {
211 Attr->printPretty(OS, Policy);
214 PrintStmt(Node->getSubStmt(), 0);
217 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
219 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
220 PrintRawDeclStmt(DS);
222 PrintExpr(If->getCond());
225 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
227 PrintRawCompoundStmt(CS);
228 OS << (If->getElse() ? ' ' : '\n');
231 PrintStmt(If->getThen());
232 if (If->getElse()) Indent();
235 if (Stmt *Else = If->getElse()) {
238 if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
240 PrintRawCompoundStmt(CS);
242 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
244 PrintRawIfStmt(ElseIf);
247 PrintStmt(If->getElse());
252 void StmtPrinter::VisitIfStmt(IfStmt *If) {
257 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
258 Indent() << "switch (";
259 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
260 PrintRawDeclStmt(DS);
262 PrintExpr(Node->getCond());
265 // Pretty print compoundstmt bodies (very common).
266 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
268 PrintRawCompoundStmt(CS);
272 PrintStmt(Node->getBody());
276 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
277 Indent() << "while (";
278 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
279 PrintRawDeclStmt(DS);
281 PrintExpr(Node->getCond());
283 PrintStmt(Node->getBody());
286 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
288 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
289 PrintRawCompoundStmt(CS);
293 PrintStmt(Node->getBody());
298 PrintExpr(Node->getCond());
302 void StmtPrinter::VisitForStmt(ForStmt *Node) {
304 if (Node->getInit()) {
305 if (auto *DS = dyn_cast<DeclStmt>(Node->getInit()))
306 PrintRawDeclStmt(DS);
308 PrintExpr(cast<Expr>(Node->getInit()));
311 if (Node->getCond()) {
313 PrintExpr(Node->getCond());
316 if (Node->getInc()) {
318 PrintExpr(Node->getInc());
322 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
323 PrintRawCompoundStmt(CS);
327 PrintStmt(Node->getBody());
331 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
333 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
334 PrintRawDeclStmt(DS);
336 PrintExpr(cast<Expr>(Node->getElement()));
338 PrintExpr(Node->getCollection());
341 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
342 PrintRawCompoundStmt(CS);
346 PrintStmt(Node->getBody());
350 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
352 PrintingPolicy SubPolicy(Policy);
353 SubPolicy.SuppressInitializers = true;
354 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
356 PrintExpr(Node->getRangeInit());
358 PrintStmt(Node->getBody());
360 if (Policy.IncludeNewlines) OS << "\n";
363 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
365 if (Node->isIfExists())
366 OS << "__if_exists (";
368 OS << "__if_not_exists (";
370 if (NestedNameSpecifier *Qualifier
371 = Node->getQualifierLoc().getNestedNameSpecifier())
372 Qualifier->print(OS, Policy);
374 OS << Node->getNameInfo() << ") ";
376 PrintRawCompoundStmt(Node->getSubStmt());
379 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
380 Indent() << "goto " << Node->getLabel()->getName() << ";";
381 if (Policy.IncludeNewlines) OS << "\n";
384 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
385 Indent() << "goto *";
386 PrintExpr(Node->getTarget());
388 if (Policy.IncludeNewlines) OS << "\n";
391 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
392 Indent() << "continue;";
393 if (Policy.IncludeNewlines) OS << "\n";
396 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
397 Indent() << "break;";
398 if (Policy.IncludeNewlines) OS << "\n";
401 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
402 Indent() << "return";
403 if (Node->getRetValue()) {
405 PrintExpr(Node->getRetValue());
408 if (Policy.IncludeNewlines) OS << "\n";
411 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
414 if (Node->isVolatile())
418 VisitStringLiteral(Node->getAsmString());
421 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
422 Node->getNumClobbers() != 0)
425 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
429 if (!Node->getOutputName(i).empty()) {
431 OS << Node->getOutputName(i);
435 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
437 Visit(Node->getOutputExpr(i));
442 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
445 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
449 if (!Node->getInputName(i).empty()) {
451 OS << Node->getInputName(i);
455 VisitStringLiteral(Node->getInputConstraintLiteral(i));
457 Visit(Node->getInputExpr(i));
462 if (Node->getNumClobbers() != 0)
465 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
469 VisitStringLiteral(Node->getClobberStringLiteral(i));
473 if (Policy.IncludeNewlines) OS << "\n";
476 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
477 // FIXME: Implement MS style inline asm statement printer.
478 Indent() << "__asm ";
479 if (Node->hasBraces())
481 OS << Node->getAsmString() << "\n";
482 if (Node->hasBraces())
486 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
487 PrintStmt(Node->getCapturedDecl()->getBody());
490 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
492 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
493 PrintRawCompoundStmt(TS);
497 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
498 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
499 Indent() << "@catch(";
500 if (catchStmt->getCatchParamDecl()) {
501 if (Decl *DS = catchStmt->getCatchParamDecl())
505 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
506 PrintRawCompoundStmt(CS);
511 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
512 Indent() << "@finally";
513 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
518 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
521 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
522 Indent() << "@catch (...) { /* todo */ } \n";
525 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
526 Indent() << "@throw";
527 if (Node->getThrowExpr()) {
529 PrintExpr(Node->getThrowExpr());
534 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
535 ObjCAvailabilityCheckExpr *Node) {
536 OS << "@available(...)";
539 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
540 Indent() << "@synchronized (";
541 PrintExpr(Node->getSynchExpr());
543 PrintRawCompoundStmt(Node->getSynchBody());
547 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
548 Indent() << "@autoreleasepool";
549 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
553 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
555 if (Decl *ExDecl = Node->getExceptionDecl())
556 PrintRawDecl(ExDecl);
560 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
563 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
565 PrintRawCXXCatchStmt(Node);
569 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
571 PrintRawCompoundStmt(Node->getTryBlock());
572 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
574 PrintRawCXXCatchStmt(Node->getHandler(i));
579 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
580 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
581 PrintRawCompoundStmt(Node->getTryBlock());
582 SEHExceptStmt *E = Node->getExceptHandler();
583 SEHFinallyStmt *F = Node->getFinallyHandler();
585 PrintRawSEHExceptHandler(E);
587 assert(F && "Must have a finally block...");
588 PrintRawSEHFinallyStmt(F);
593 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
595 PrintRawCompoundStmt(Node->getBlock());
599 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
601 VisitExpr(Node->getFilterExpr());
603 PrintRawCompoundStmt(Node->getBlock());
607 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
609 PrintRawSEHExceptHandler(Node);
613 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
615 PrintRawSEHFinallyStmt(Node);
619 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
620 Indent() << "__leave;";
621 if (Policy.IncludeNewlines) OS << "\n";
624 //===----------------------------------------------------------------------===//
625 // OpenMP clauses printing methods
626 //===----------------------------------------------------------------------===//
630 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
632 const PrintingPolicy &Policy;
634 /// Process clauses with list of variables.
635 template <typename T>
636 void VisitOMPClauseList(T *Node, char StartSym);
639 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
640 : OS(OS), Policy(Policy) {}
642 #define OPENMP_CLAUSE(Name, Class) \
643 void Visit##Class(Class *S);
644 #include "clang/Basic/OpenMPKinds.def"
649 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
651 if (Node->getNameModifier() != OMPD_unknown)
652 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
653 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
657 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
659 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
663 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
664 OS << "num_threads(";
665 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
669 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
671 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
675 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
677 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
681 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
683 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
687 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
689 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
693 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
695 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
699 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
701 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
702 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
703 Node->getFirstScheduleModifier());
704 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
706 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
707 Node->getSecondScheduleModifier());
711 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
712 if (auto *E = Node->getChunkSize()) {
714 E->printPretty(OS, nullptr, Policy);
719 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
721 if (auto *Num = Node->getNumForLoops()) {
723 Num->printPretty(OS, nullptr, Policy, 0);
728 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
732 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
736 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
740 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
744 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
746 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
748 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
752 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
756 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
760 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
764 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
766 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
768 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
772 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
774 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
778 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
779 OS << "thread_limit(";
780 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
784 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
786 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
790 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
792 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
796 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
798 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
802 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
804 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
809 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
810 for (typename T::varlist_iterator I = Node->varlist_begin(),
811 E = Node->varlist_end();
813 assert(*I && "Expected non-null Stmt");
814 OS << (I == Node->varlist_begin() ? StartSym : ',');
815 if (auto *DRE = dyn_cast<DeclRefExpr>(*I)) {
816 if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
817 DRE->printPretty(OS, nullptr, Policy, 0);
819 DRE->getDecl()->printQualifiedName(OS);
821 (*I)->printPretty(OS, nullptr, Policy, 0);
825 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
826 if (!Node->varlist_empty()) {
828 VisitOMPClauseList(Node, '(');
833 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
834 if (!Node->varlist_empty()) {
835 OS << "firstprivate";
836 VisitOMPClauseList(Node, '(');
841 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
842 if (!Node->varlist_empty()) {
844 VisitOMPClauseList(Node, '(');
849 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
850 if (!Node->varlist_empty()) {
852 VisitOMPClauseList(Node, '(');
857 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
858 if (!Node->varlist_empty()) {
860 NestedNameSpecifier *QualifierLoc =
861 Node->getQualifierLoc().getNestedNameSpecifier();
862 OverloadedOperatorKind OOK =
863 Node->getNameInfo().getName().getCXXOverloadedOperator();
864 if (QualifierLoc == nullptr && OOK != OO_None) {
865 // Print reduction identifier in C format
866 OS << getOperatorSpelling(OOK);
869 if (QualifierLoc != nullptr)
870 QualifierLoc->print(OS, Policy);
871 OS << Node->getNameInfo();
874 VisitOMPClauseList(Node, ' ');
879 void OMPClausePrinter::VisitOMPTaskReductionClause(
880 OMPTaskReductionClause *Node) {
881 if (!Node->varlist_empty()) {
882 OS << "task_reduction(";
883 NestedNameSpecifier *QualifierLoc =
884 Node->getQualifierLoc().getNestedNameSpecifier();
885 OverloadedOperatorKind OOK =
886 Node->getNameInfo().getName().getCXXOverloadedOperator();
887 if (QualifierLoc == nullptr && OOK != OO_None) {
888 // Print reduction identifier in C format
889 OS << getOperatorSpelling(OOK);
892 if (QualifierLoc != nullptr)
893 QualifierLoc->print(OS, Policy);
894 OS << Node->getNameInfo();
897 VisitOMPClauseList(Node, ' ');
902 void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
903 if (!Node->varlist_empty()) {
904 OS << "in_reduction(";
905 NestedNameSpecifier *QualifierLoc =
906 Node->getQualifierLoc().getNestedNameSpecifier();
907 OverloadedOperatorKind OOK =
908 Node->getNameInfo().getName().getCXXOverloadedOperator();
909 if (QualifierLoc == nullptr && OOK != OO_None) {
910 // Print reduction identifier in C format
911 OS << getOperatorSpelling(OOK);
914 if (QualifierLoc != nullptr)
915 QualifierLoc->print(OS, Policy);
916 OS << Node->getNameInfo();
919 VisitOMPClauseList(Node, ' ');
924 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
925 if (!Node->varlist_empty()) {
927 if (Node->getModifierLoc().isValid()) {
929 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
931 VisitOMPClauseList(Node, '(');
932 if (Node->getModifierLoc().isValid())
934 if (Node->getStep() != nullptr) {
936 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
942 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
943 if (!Node->varlist_empty()) {
945 VisitOMPClauseList(Node, '(');
946 if (Node->getAlignment() != nullptr) {
948 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
954 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
955 if (!Node->varlist_empty()) {
957 VisitOMPClauseList(Node, '(');
962 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
963 if (!Node->varlist_empty()) {
965 VisitOMPClauseList(Node, '(');
970 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
971 if (!Node->varlist_empty()) {
972 VisitOMPClauseList(Node, '(');
977 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
979 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
980 Node->getDependencyKind());
981 if (!Node->varlist_empty()) {
983 VisitOMPClauseList(Node, ' ');
988 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
989 if (!Node->varlist_empty()) {
991 if (Node->getMapType() != OMPC_MAP_unknown) {
992 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
993 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
994 Node->getMapTypeModifier());
997 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
1000 VisitOMPClauseList(Node, ' ');
1005 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
1006 if (!Node->varlist_empty()) {
1008 VisitOMPClauseList(Node, '(');
1013 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
1014 if (!Node->varlist_empty()) {
1016 VisitOMPClauseList(Node, '(');
1021 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
1022 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
1023 OMPC_dist_schedule, Node->getDistScheduleKind());
1024 if (auto *E = Node->getChunkSize()) {
1026 E->printPretty(OS, nullptr, Policy);
1031 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
1032 OS << "defaultmap(";
1033 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
1034 Node->getDefaultmapModifier());
1036 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
1037 Node->getDefaultmapKind());
1041 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
1042 if (!Node->varlist_empty()) {
1043 OS << "use_device_ptr";
1044 VisitOMPClauseList(Node, '(');
1049 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
1050 if (!Node->varlist_empty()) {
1051 OS << "is_device_ptr";
1052 VisitOMPClauseList(Node, '(');
1057 //===----------------------------------------------------------------------===//
1058 // OpenMP directives printing methods
1059 //===----------------------------------------------------------------------===//
1061 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
1063 OMPClausePrinter Printer(OS, Policy);
1064 ArrayRef<OMPClause *> Clauses = S->clauses();
1065 for (auto *Clause : Clauses)
1066 if (Clause && !Clause->isImplicit()) {
1068 Printer.Visit(Clause);
1071 if (!ForceNoStmt && S->hasAssociatedStmt())
1072 PrintStmt(S->getInnermostCapturedStmt()->getCapturedStmt());
1075 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
1076 Indent() << "#pragma omp parallel";
1077 PrintOMPExecutableDirective(Node);
1080 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
1081 Indent() << "#pragma omp simd";
1082 PrintOMPExecutableDirective(Node);
1085 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
1086 Indent() << "#pragma omp for";
1087 PrintOMPExecutableDirective(Node);
1090 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
1091 Indent() << "#pragma omp for simd";
1092 PrintOMPExecutableDirective(Node);
1095 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
1096 Indent() << "#pragma omp sections";
1097 PrintOMPExecutableDirective(Node);
1100 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
1101 Indent() << "#pragma omp section";
1102 PrintOMPExecutableDirective(Node);
1105 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
1106 Indent() << "#pragma omp single";
1107 PrintOMPExecutableDirective(Node);
1110 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
1111 Indent() << "#pragma omp master";
1112 PrintOMPExecutableDirective(Node);
1115 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
1116 Indent() << "#pragma omp critical";
1117 if (Node->getDirectiveName().getName()) {
1119 Node->getDirectiveName().printName(OS);
1122 PrintOMPExecutableDirective(Node);
1125 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1126 Indent() << "#pragma omp parallel for";
1127 PrintOMPExecutableDirective(Node);
1130 void StmtPrinter::VisitOMPParallelForSimdDirective(
1131 OMPParallelForSimdDirective *Node) {
1132 Indent() << "#pragma omp parallel for simd";
1133 PrintOMPExecutableDirective(Node);
1136 void StmtPrinter::VisitOMPParallelSectionsDirective(
1137 OMPParallelSectionsDirective *Node) {
1138 Indent() << "#pragma omp parallel sections";
1139 PrintOMPExecutableDirective(Node);
1142 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1143 Indent() << "#pragma omp task";
1144 PrintOMPExecutableDirective(Node);
1147 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1148 Indent() << "#pragma omp taskyield";
1149 PrintOMPExecutableDirective(Node);
1152 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1153 Indent() << "#pragma omp barrier";
1154 PrintOMPExecutableDirective(Node);
1157 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1158 Indent() << "#pragma omp taskwait";
1159 PrintOMPExecutableDirective(Node);
1162 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
1163 Indent() << "#pragma omp taskgroup";
1164 PrintOMPExecutableDirective(Node);
1167 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1168 Indent() << "#pragma omp flush";
1169 PrintOMPExecutableDirective(Node);
1172 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
1173 Indent() << "#pragma omp ordered";
1174 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
1177 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1178 Indent() << "#pragma omp atomic";
1179 PrintOMPExecutableDirective(Node);
1182 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1183 Indent() << "#pragma omp target";
1184 PrintOMPExecutableDirective(Node);
1187 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1188 Indent() << "#pragma omp target data";
1189 PrintOMPExecutableDirective(Node);
1192 void StmtPrinter::VisitOMPTargetEnterDataDirective(
1193 OMPTargetEnterDataDirective *Node) {
1194 Indent() << "#pragma omp target enter data";
1195 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
1198 void StmtPrinter::VisitOMPTargetExitDataDirective(
1199 OMPTargetExitDataDirective *Node) {
1200 Indent() << "#pragma omp target exit data";
1201 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
1204 void StmtPrinter::VisitOMPTargetParallelDirective(
1205 OMPTargetParallelDirective *Node) {
1206 Indent() << "#pragma omp target parallel";
1207 PrintOMPExecutableDirective(Node);
1210 void StmtPrinter::VisitOMPTargetParallelForDirective(
1211 OMPTargetParallelForDirective *Node) {
1212 Indent() << "#pragma omp target parallel for";
1213 PrintOMPExecutableDirective(Node);
1216 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1217 Indent() << "#pragma omp teams";
1218 PrintOMPExecutableDirective(Node);
1221 void StmtPrinter::VisitOMPCancellationPointDirective(
1222 OMPCancellationPointDirective *Node) {
1223 Indent() << "#pragma omp cancellation point "
1224 << getOpenMPDirectiveName(Node->getCancelRegion());
1225 PrintOMPExecutableDirective(Node);
1228 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1229 Indent() << "#pragma omp cancel "
1230 << getOpenMPDirectiveName(Node->getCancelRegion());
1231 PrintOMPExecutableDirective(Node);
1234 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1235 Indent() << "#pragma omp taskloop";
1236 PrintOMPExecutableDirective(Node);
1239 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1240 OMPTaskLoopSimdDirective *Node) {
1241 Indent() << "#pragma omp taskloop simd";
1242 PrintOMPExecutableDirective(Node);
1245 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1246 Indent() << "#pragma omp distribute";
1247 PrintOMPExecutableDirective(Node);
1250 void StmtPrinter::VisitOMPTargetUpdateDirective(
1251 OMPTargetUpdateDirective *Node) {
1252 Indent() << "#pragma omp target update";
1253 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
1256 void StmtPrinter::VisitOMPDistributeParallelForDirective(
1257 OMPDistributeParallelForDirective *Node) {
1258 Indent() << "#pragma omp distribute parallel for";
1259 PrintOMPExecutableDirective(Node);
1262 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1263 OMPDistributeParallelForSimdDirective *Node) {
1264 Indent() << "#pragma omp distribute parallel for simd";
1265 PrintOMPExecutableDirective(Node);
1268 void StmtPrinter::VisitOMPDistributeSimdDirective(
1269 OMPDistributeSimdDirective *Node) {
1270 Indent() << "#pragma omp distribute simd";
1271 PrintOMPExecutableDirective(Node);
1274 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1275 OMPTargetParallelForSimdDirective *Node) {
1276 Indent() << "#pragma omp target parallel for simd";
1277 PrintOMPExecutableDirective(Node);
1280 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1281 Indent() << "#pragma omp target simd";
1282 PrintOMPExecutableDirective(Node);
1285 void StmtPrinter::VisitOMPTeamsDistributeDirective(
1286 OMPTeamsDistributeDirective *Node) {
1287 Indent() << "#pragma omp teams distribute";
1288 PrintOMPExecutableDirective(Node);
1291 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1292 OMPTeamsDistributeSimdDirective *Node) {
1293 Indent() << "#pragma omp teams distribute simd";
1294 PrintOMPExecutableDirective(Node);
1297 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1298 OMPTeamsDistributeParallelForSimdDirective *Node) {
1299 Indent() << "#pragma omp teams distribute parallel for simd";
1300 PrintOMPExecutableDirective(Node);
1303 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1304 OMPTeamsDistributeParallelForDirective *Node) {
1305 Indent() << "#pragma omp teams distribute parallel for";
1306 PrintOMPExecutableDirective(Node);
1309 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1310 Indent() << "#pragma omp target teams";
1311 PrintOMPExecutableDirective(Node);
1314 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1315 OMPTargetTeamsDistributeDirective *Node) {
1316 Indent() << "#pragma omp target teams distribute";
1317 PrintOMPExecutableDirective(Node);
1320 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1321 OMPTargetTeamsDistributeParallelForDirective *Node) {
1322 Indent() << "#pragma omp target teams distribute parallel for";
1323 PrintOMPExecutableDirective(Node);
1326 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1327 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1328 Indent() << "#pragma omp target teams distribute parallel for simd";
1329 PrintOMPExecutableDirective(Node);
1332 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1333 OMPTargetTeamsDistributeSimdDirective *Node) {
1334 Indent() << "#pragma omp target teams distribute simd";
1335 PrintOMPExecutableDirective(Node);
1338 //===----------------------------------------------------------------------===//
1339 // Expr printing methods.
1340 //===----------------------------------------------------------------------===//
1342 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1343 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1344 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1347 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1348 Qualifier->print(OS, Policy);
1349 if (Node->hasTemplateKeyword())
1351 OS << Node->getNameInfo();
1352 if (Node->hasExplicitTemplateArgs())
1353 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1356 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1357 DependentScopeDeclRefExpr *Node) {
1358 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1359 Qualifier->print(OS, Policy);
1360 if (Node->hasTemplateKeyword())
1362 OS << Node->getNameInfo();
1363 if (Node->hasExplicitTemplateArgs())
1364 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1367 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1368 if (Node->getQualifier())
1369 Node->getQualifier()->print(OS, Policy);
1370 if (Node->hasTemplateKeyword())
1372 OS << Node->getNameInfo();
1373 if (Node->hasExplicitTemplateArgs())
1374 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1377 static bool isImplicitSelf(const Expr *E) {
1378 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1379 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1380 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1381 DRE->getLocStart().isInvalid())
1388 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1389 if (Node->getBase()) {
1390 if (!Policy.SuppressImplicitBase ||
1391 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1392 PrintExpr(Node->getBase());
1393 OS << (Node->isArrow() ? "->" : ".");
1396 OS << *Node->getDecl();
1399 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1400 if (Node->isSuperReceiver())
1402 else if (Node->isObjectReceiver() && Node->getBase()) {
1403 PrintExpr(Node->getBase());
1405 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1406 OS << Node->getClassReceiver()->getName() << ".";
1409 if (Node->isImplicitProperty()) {
1410 if (const auto *Getter = Node->getImplicitPropertyGetter())
1411 Getter->getSelector().print(OS);
1413 OS << SelectorTable::getPropertyNameFromSetterSelector(
1414 Node->getImplicitPropertySetter()->getSelector());
1416 OS << Node->getExplicitProperty()->getName();
1419 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1420 PrintExpr(Node->getBaseExpr());
1422 PrintExpr(Node->getKeyExpr());
1426 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1427 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1430 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1431 unsigned value = Node->getValue();
1433 switch (Node->getKind()) {
1434 case CharacterLiteral::Ascii: break; // no prefix.
1435 case CharacterLiteral::Wide: OS << 'L'; break;
1436 case CharacterLiteral::UTF8: OS << "u8"; break;
1437 case CharacterLiteral::UTF16: OS << 'u'; break;
1438 case CharacterLiteral::UTF32: OS << 'U'; break;
1449 // TODO: K&R: the meaning of '\\a' is different in traditional C
1455 // Nonstandard escape sequence.
1475 // A character literal might be sign-extended, which
1476 // would result in an invalid \U escape sequence.
1477 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1478 // are not correctly handled.
1479 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1481 if (value < 256 && isPrintable((unsigned char)value))
1482 OS << "'" << (char)value << "'";
1483 else if (value < 256)
1484 OS << "'\\x" << llvm::format("%02x", value) << "'";
1485 else if (value <= 0xFFFF)
1486 OS << "'\\u" << llvm::format("%04x", value) << "'";
1488 OS << "'\\U" << llvm::format("%08x", value) << "'";
1492 /// Prints the given expression using the original source text. Returns true on
1493 /// success, false otherwise.
1494 static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1495 const ASTContext *Context) {
1498 bool Invalid = false;
1499 StringRef Source = Lexer::getSourceText(
1500 CharSourceRange::getTokenRange(E->getSourceRange()),
1501 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1509 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1510 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1512 bool isSigned = Node->getType()->isSignedIntegerType();
1513 OS << Node->getValue().toString(10, isSigned);
1515 // Emit suffixes. Integer literals are always a builtin integer type.
1516 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1517 default: llvm_unreachable("Unexpected type for integer literal!");
1518 case BuiltinType::Char_S:
1519 case BuiltinType::Char_U: OS << "i8"; break;
1520 case BuiltinType::UChar: OS << "Ui8"; break;
1521 case BuiltinType::Short: OS << "i16"; break;
1522 case BuiltinType::UShort: OS << "Ui16"; break;
1523 case BuiltinType::Int: break; // no suffix.
1524 case BuiltinType::UInt: OS << 'U'; break;
1525 case BuiltinType::Long: OS << 'L'; break;
1526 case BuiltinType::ULong: OS << "UL"; break;
1527 case BuiltinType::LongLong: OS << "LL"; break;
1528 case BuiltinType::ULongLong: OS << "ULL"; break;
1532 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1533 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1535 OS << Node->getValueAsString(/*Radix=*/10);
1537 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1538 default: llvm_unreachable("Unexpected type for fixed point literal!");
1539 case BuiltinType::ShortFract: OS << "hr"; break;
1540 case BuiltinType::ShortAccum: OS << "hk"; break;
1541 case BuiltinType::UShortFract: OS << "uhr"; break;
1542 case BuiltinType::UShortAccum: OS << "uhk"; break;
1543 case BuiltinType::Fract: OS << "r"; break;
1544 case BuiltinType::Accum: OS << "k"; break;
1545 case BuiltinType::UFract: OS << "ur"; break;
1546 case BuiltinType::UAccum: OS << "uk"; break;
1547 case BuiltinType::LongFract: OS << "lr"; break;
1548 case BuiltinType::LongAccum: OS << "lk"; break;
1549 case BuiltinType::ULongFract: OS << "ulr"; break;
1550 case BuiltinType::ULongAccum: OS << "ulk"; break;
1554 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1556 SmallString<16> Str;
1557 Node->getValue().toString(Str);
1559 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1560 OS << '.'; // Trailing dot in order to separate from ints.
1565 // Emit suffixes. Float literals are always a builtin float type.
1566 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1567 default: llvm_unreachable("Unexpected type for float literal!");
1568 case BuiltinType::Half: break; // FIXME: suffix?
1569 case BuiltinType::Double: break; // no suffix.
1570 case BuiltinType::Float16: OS << "F16"; break;
1571 case BuiltinType::Float: OS << 'F'; break;
1572 case BuiltinType::LongDouble: OS << 'L'; break;
1573 case BuiltinType::Float128: OS << 'Q'; break;
1577 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1578 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1580 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1583 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1584 PrintExpr(Node->getSubExpr());
1588 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1589 Str->outputString(OS);
1592 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1594 PrintExpr(Node->getSubExpr());
1598 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1599 if (!Node->isPostfix()) {
1600 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1602 // Print a space if this is an "identifier operator" like __real, or if
1603 // it might be concatenated incorrectly like '+'.
1604 switch (Node->getOpcode()) {
1613 if (isa<UnaryOperator>(Node->getSubExpr()))
1618 PrintExpr(Node->getSubExpr());
1620 if (Node->isPostfix())
1621 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1624 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1625 OS << "__builtin_offsetof(";
1626 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1628 bool PrintedSomething = false;
1629 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1630 OffsetOfNode ON = Node->getComponent(i);
1631 if (ON.getKind() == OffsetOfNode::Array) {
1634 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1636 PrintedSomething = true;
1640 // Skip implicit base indirections.
1641 if (ON.getKind() == OffsetOfNode::Base)
1644 // Field or identifier node.
1645 IdentifierInfo *Id = ON.getFieldName();
1649 if (PrintedSomething)
1652 PrintedSomething = true;
1653 OS << Id->getName();
1658 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1659 switch(Node->getKind()) {
1666 else if (Policy.UnderscoreAlignof)
1674 case UETT_OpenMPRequiredSimdAlign:
1675 OS << "__builtin_omp_required_simd_align";
1678 if (Node->isArgumentType()) {
1680 Node->getArgumentType().print(OS, Policy);
1684 PrintExpr(Node->getArgumentExpr());
1688 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1690 PrintExpr(Node->getControllingExpr());
1691 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1693 QualType T = Node->getAssocType(i);
1697 T.print(OS, Policy);
1699 PrintExpr(Node->getAssocExpr(i));
1704 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1705 PrintExpr(Node->getLHS());
1707 PrintExpr(Node->getRHS());
1711 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1712 PrintExpr(Node->getBase());
1714 if (Node->getLowerBound())
1715 PrintExpr(Node->getLowerBound());
1716 if (Node->getColonLoc().isValid()) {
1718 if (Node->getLength())
1719 PrintExpr(Node->getLength());
1724 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1725 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1726 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1727 // Don't print any defaulted arguments
1732 PrintExpr(Call->getArg(i));
1736 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1737 PrintExpr(Call->getCallee());
1739 PrintCallArgs(Call);
1743 static bool isImplicitThis(const Expr *E) {
1744 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1745 return TE->isImplicit();
1749 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1750 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1751 PrintExpr(Node->getBase());
1753 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1754 FieldDecl *ParentDecl =
1755 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1758 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1759 OS << (Node->isArrow() ? "->" : ".");
1762 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1763 if (FD->isAnonymousStructOrUnion())
1766 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1767 Qualifier->print(OS, Policy);
1768 if (Node->hasTemplateKeyword())
1770 OS << Node->getMemberNameInfo();
1771 if (Node->hasExplicitTemplateArgs())
1772 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1775 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1776 PrintExpr(Node->getBase());
1777 OS << (Node->isArrow() ? "->isa" : ".isa");
1780 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1781 PrintExpr(Node->getBase());
1783 OS << Node->getAccessor().getName();
1786 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1788 Node->getTypeAsWritten().print(OS, Policy);
1790 PrintExpr(Node->getSubExpr());
1793 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1795 Node->getType().print(OS, Policy);
1797 PrintExpr(Node->getInitializer());
1800 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1801 // No need to print anything, simply forward to the subexpression.
1802 PrintExpr(Node->getSubExpr());
1805 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1806 PrintExpr(Node->getLHS());
1807 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1808 PrintExpr(Node->getRHS());
1811 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1812 PrintExpr(Node->getLHS());
1813 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1814 PrintExpr(Node->getRHS());
1817 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1818 PrintExpr(Node->getCond());
1820 PrintExpr(Node->getLHS());
1822 PrintExpr(Node->getRHS());
1828 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1829 PrintExpr(Node->getCommon());
1831 PrintExpr(Node->getFalseExpr());
1834 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1835 OS << "&&" << Node->getLabel()->getName();
1838 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1840 PrintRawCompoundStmt(E->getSubStmt());
1844 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1845 OS << "__builtin_choose_expr(";
1846 PrintExpr(Node->getCond());
1848 PrintExpr(Node->getLHS());
1850 PrintExpr(Node->getRHS());
1854 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1858 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1859 OS << "__builtin_shufflevector(";
1860 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1862 PrintExpr(Node->getExpr(i));
1867 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1868 OS << "__builtin_convertvector(";
1869 PrintExpr(Node->getSrcExpr());
1871 Node->getType().print(OS, Policy);
1875 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1876 if (Node->getSyntacticForm()) {
1877 Visit(Node->getSyntacticForm());
1882 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1884 if (Node->getInit(i))
1885 PrintExpr(Node->getInit(i));
1892 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1893 // There's no way to express this expression in any of our supported
1894 // languages, so just emit something terse and (hopefully) clear.
1896 PrintExpr(Node->getSubExpr());
1900 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1904 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1906 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1908 PrintExpr(Node->getExpr(i));
1913 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1914 bool NeedsEquals = true;
1915 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1916 if (D.isFieldDesignator()) {
1917 if (D.getDotLoc().isInvalid()) {
1918 if (IdentifierInfo *II = D.getFieldName()) {
1919 OS << II->getName() << ":";
1920 NeedsEquals = false;
1923 OS << "." << D.getFieldName()->getName();
1927 if (D.isArrayDesignator()) {
1928 PrintExpr(Node->getArrayIndex(D));
1930 PrintExpr(Node->getArrayRangeStart(D));
1932 PrintExpr(Node->getArrayRangeEnd(D));
1942 PrintExpr(Node->getInit());
1945 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1946 DesignatedInitUpdateExpr *Node) {
1949 PrintExpr(Node->getBase());
1952 OS << "/*updater*/";
1953 PrintExpr(Node->getUpdater());
1957 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1958 OS << "/*no init*/";
1961 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1962 if (Node->getType()->getAsCXXRecordDecl()) {
1963 OS << "/*implicit*/";
1964 Node->getType().print(OS, Policy);
1967 OS << "/*implicit*/(";
1968 Node->getType().print(OS, Policy);
1970 if (Node->getType()->isRecordType())
1977 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1978 OS << "__builtin_va_arg(";
1979 PrintExpr(Node->getSubExpr());
1981 Node->getType().print(OS, Policy);
1985 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1986 PrintExpr(Node->getSyntacticForm());
1989 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1990 const char *Name = nullptr;
1991 switch (Node->getOp()) {
1992 #define BUILTIN(ID, TYPE, ATTRS)
1993 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1994 case AtomicExpr::AO ## ID: \
1997 #include "clang/Basic/Builtins.def"
2001 // AtomicExpr stores its subexpressions in a permuted order.
2002 PrintExpr(Node->getPtr());
2003 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
2004 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
2005 Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
2007 PrintExpr(Node->getVal1());
2009 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
2010 Node->isCmpXChg()) {
2012 PrintExpr(Node->getVal2());
2014 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
2015 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
2017 PrintExpr(Node->getWeak());
2019 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
2020 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
2022 PrintExpr(Node->getOrder());
2024 if (Node->isCmpXChg()) {
2026 PrintExpr(Node->getOrderFail());
2032 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
2033 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
2035 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2037 #include "clang/Basic/OperatorKinds.def"
2040 OverloadedOperatorKind Kind = Node->getOperator();
2041 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
2042 if (Node->getNumArgs() == 1) {
2043 OS << OpStrings[Kind] << ' ';
2044 PrintExpr(Node->getArg(0));
2046 PrintExpr(Node->getArg(0));
2047 OS << ' ' << OpStrings[Kind];
2049 } else if (Kind == OO_Arrow) {
2050 PrintExpr(Node->getArg(0));
2051 } else if (Kind == OO_Call) {
2052 PrintExpr(Node->getArg(0));
2054 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
2057 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
2058 PrintExpr(Node->getArg(ArgIdx));
2061 } else if (Kind == OO_Subscript) {
2062 PrintExpr(Node->getArg(0));
2064 PrintExpr(Node->getArg(1));
2066 } else if (Node->getNumArgs() == 1) {
2067 OS << OpStrings[Kind] << ' ';
2068 PrintExpr(Node->getArg(0));
2069 } else if (Node->getNumArgs() == 2) {
2070 PrintExpr(Node->getArg(0));
2071 OS << ' ' << OpStrings[Kind] << ' ';
2072 PrintExpr(Node->getArg(1));
2074 llvm_unreachable("unknown overloaded operator");
2078 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
2079 // If we have a conversion operator call only print the argument.
2080 CXXMethodDecl *MD = Node->getMethodDecl();
2081 if (MD && isa<CXXConversionDecl>(MD)) {
2082 PrintExpr(Node->getImplicitObjectArgument());
2085 VisitCallExpr(cast<CallExpr>(Node));
2088 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
2089 PrintExpr(Node->getCallee());
2091 PrintCallArgs(Node->getConfig());
2093 PrintCallArgs(Node);
2097 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
2098 OS << Node->getCastName() << '<';
2099 Node->getTypeAsWritten().print(OS, Policy);
2101 PrintExpr(Node->getSubExpr());
2105 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
2106 VisitCXXNamedCastExpr(Node);
2109 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
2110 VisitCXXNamedCastExpr(Node);
2113 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
2114 VisitCXXNamedCastExpr(Node);
2117 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
2118 VisitCXXNamedCastExpr(Node);
2121 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
2123 if (Node->isTypeOperand()) {
2124 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
2126 PrintExpr(Node->getExprOperand());
2131 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
2133 if (Node->isTypeOperand()) {
2134 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
2136 PrintExpr(Node->getExprOperand());
2141 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
2142 PrintExpr(Node->getBaseExpr());
2143 if (Node->isArrow())
2147 if (NestedNameSpecifier *Qualifier =
2148 Node->getQualifierLoc().getNestedNameSpecifier())
2149 Qualifier->print(OS, Policy);
2150 OS << Node->getPropertyDecl()->getDeclName();
2153 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
2154 PrintExpr(Node->getBase());
2156 PrintExpr(Node->getIdx());
2160 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2161 switch (Node->getLiteralOperatorKind()) {
2162 case UserDefinedLiteral::LOK_Raw:
2163 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
2165 case UserDefinedLiteral::LOK_Template: {
2166 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
2167 const TemplateArgumentList *Args =
2168 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
2171 if (Args->size() != 1) {
2172 OS << "operator\"\"" << Node->getUDSuffix()->getName();
2173 printTemplateArgumentList(OS, Args->asArray(), Policy);
2178 const TemplateArgument &Pack = Args->get(0);
2179 for (const auto &P : Pack.pack_elements()) {
2180 char C = (char)P.getAsIntegral().getZExtValue();
2185 case UserDefinedLiteral::LOK_Integer: {
2186 // Print integer literal without suffix.
2187 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2188 OS << Int->getValue().toString(10, /*isSigned*/false);
2191 case UserDefinedLiteral::LOK_Floating: {
2192 // Print floating literal without suffix.
2193 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2194 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2197 case UserDefinedLiteral::LOK_String:
2198 case UserDefinedLiteral::LOK_Character:
2199 PrintExpr(Node->getCookedLiteral());
2202 OS << Node->getUDSuffix()->getName();
2205 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2206 OS << (Node->getValue() ? "true" : "false");
2209 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2213 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2217 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
2218 if (!Node->getSubExpr())
2222 PrintExpr(Node->getSubExpr());
2226 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
2227 // Nothing to print: we picked up the default argument.
2230 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2231 // Nothing to print: we picked up the default initializer.
2234 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2235 Node->getType().print(OS, Policy);
2236 // If there are no parens, this is list-initialization, and the braces are
2237 // part of the syntax of the inner construct.
2238 if (Node->getLParenLoc().isValid())
2240 PrintExpr(Node->getSubExpr());
2241 if (Node->getLParenLoc().isValid())
2245 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2246 PrintExpr(Node->getSubExpr());
2249 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2250 Node->getType().print(OS, Policy);
2251 if (Node->isStdInitListInitialization())
2252 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2253 else if (Node->isListInitialization())
2257 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2258 ArgEnd = Node->arg_end();
2259 Arg != ArgEnd; ++Arg) {
2260 if ((*Arg)->isDefaultArgument())
2262 if (Arg != Node->arg_begin())
2266 if (Node->isStdInitListInitialization())
2268 else if (Node->isListInitialization())
2274 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2276 bool NeedComma = false;
2277 switch (Node->getCaptureDefault()) {
2291 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2292 CEnd = Node->explicit_capture_end();
2295 if (C->capturesVLAType())
2302 switch (C->getCaptureKind()) {
2312 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2314 OS << C->getCapturedVar()->getName();
2318 OS << C->getCapturedVar()->getName();
2322 llvm_unreachable("VLA type in explicit captures.");
2325 if (Node->isInitCapture(C))
2326 PrintExpr(C->getCapturedVar()->getInit());
2330 if (Node->hasExplicitParameters()) {
2332 CXXMethodDecl *Method = Node->getCallOperator();
2334 for (const auto *P : Method->parameters()) {
2340 std::string ParamStr = P->getNameAsString();
2341 P->getOriginalType().print(OS, Policy, ParamStr);
2343 if (Method->isVariadic()) {
2350 if (Node->isMutable())
2353 auto *Proto = Method->getType()->getAs<FunctionProtoType>();
2354 Proto->printExceptionSpecification(OS, Policy);
2356 // FIXME: Attributes
2358 // Print the trailing return type if it was specified in the source.
2359 if (Node->hasExplicitResultType()) {
2361 Proto->getReturnType().print(OS, Policy);
2366 CompoundStmt *Body = Node->getBody();
2371 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2372 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2373 TSInfo->getType().print(OS, Policy);
2375 Node->getType().print(OS, Policy);
2379 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2380 if (E->isGlobalNew())
2383 unsigned NumPlace = E->getNumPlacementArgs();
2384 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2386 PrintExpr(E->getPlacementArg(0));
2387 for (unsigned i = 1; i < NumPlace; ++i) {
2388 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2391 PrintExpr(E->getPlacementArg(i));
2395 if (E->isParenTypeId())
2398 if (Expr *Size = E->getArraySize()) {
2399 llvm::raw_string_ostream s(TypeS);
2401 Size->printPretty(s, Helper, Policy);
2404 E->getAllocatedType().print(OS, Policy, TypeS);
2405 if (E->isParenTypeId())
2408 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2410 if (InitStyle == CXXNewExpr::CallInit)
2412 PrintExpr(E->getInitializer());
2413 if (InitStyle == CXXNewExpr::CallInit)
2418 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2419 if (E->isGlobalDelete())
2422 if (E->isArrayForm())
2424 PrintExpr(E->getArgument());
2427 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2428 PrintExpr(E->getBase());
2433 if (E->getQualifier())
2434 E->getQualifier()->print(OS, Policy);
2437 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2438 OS << II->getName();
2440 E->getDestroyedType().print(OS, Policy);
2443 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2444 if (E->isListInitialization() && !E->isStdInitListInitialization())
2447 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2448 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2449 // Don't print any defaulted arguments
2454 PrintExpr(E->getArg(i));
2457 if (E->isListInitialization() && !E->isStdInitListInitialization())
2461 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2462 // Parens are printed by the surrounding context.
2463 OS << "<forwarded>";
2466 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2467 PrintExpr(E->getSubExpr());
2470 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2471 // Just forward to the subexpression.
2472 PrintExpr(E->getSubExpr());
2476 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2477 CXXUnresolvedConstructExpr *Node) {
2478 Node->getTypeAsWritten().print(OS, Policy);
2480 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2481 ArgEnd = Node->arg_end();
2482 Arg != ArgEnd; ++Arg) {
2483 if (Arg != Node->arg_begin())
2490 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2491 CXXDependentScopeMemberExpr *Node) {
2492 if (!Node->isImplicitAccess()) {
2493 PrintExpr(Node->getBase());
2494 OS << (Node->isArrow() ? "->" : ".");
2496 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2497 Qualifier->print(OS, Policy);
2498 if (Node->hasTemplateKeyword())
2500 OS << Node->getMemberNameInfo();
2501 if (Node->hasExplicitTemplateArgs())
2502 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2505 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2506 if (!Node->isImplicitAccess()) {
2507 PrintExpr(Node->getBase());
2508 OS << (Node->isArrow() ? "->" : ".");
2510 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2511 Qualifier->print(OS, Policy);
2512 if (Node->hasTemplateKeyword())
2514 OS << Node->getMemberNameInfo();
2515 if (Node->hasExplicitTemplateArgs())
2516 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2519 static const char *getTypeTraitName(TypeTrait TT) {
2521 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2522 case clang::UTT_##Name: return #Spelling;
2523 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2524 case clang::BTT_##Name: return #Spelling;
2525 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2526 case clang::TT_##Name: return #Spelling;
2527 #include "clang/Basic/TokenKinds.def"
2529 llvm_unreachable("Type trait not covered by switch");
2532 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2534 case ATT_ArrayRank: return "__array_rank";
2535 case ATT_ArrayExtent: return "__array_extent";
2537 llvm_unreachable("Array type trait not covered by switch");
2540 static const char *getExpressionTraitName(ExpressionTrait ET) {
2542 case ET_IsLValueExpr: return "__is_lvalue_expr";
2543 case ET_IsRValueExpr: return "__is_rvalue_expr";
2545 llvm_unreachable("Expression type trait not covered by switch");
2548 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2549 OS << getTypeTraitName(E->getTrait()) << "(";
2550 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2553 E->getArg(I)->getType().print(OS, Policy);
2558 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2559 OS << getTypeTraitName(E->getTrait()) << '(';
2560 E->getQueriedType().print(OS, Policy);
2564 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2565 OS << getExpressionTraitName(E->getTrait()) << '(';
2566 PrintExpr(E->getQueriedExpression());
2570 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2572 PrintExpr(E->getOperand());
2576 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2577 PrintExpr(E->getPattern());
2581 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2582 OS << "sizeof...(" << *E->getPack() << ")";
2585 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2586 SubstNonTypeTemplateParmPackExpr *Node) {
2587 OS << *Node->getParameterPack();
2590 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2591 SubstNonTypeTemplateParmExpr *Node) {
2592 Visit(Node->getReplacement());
2595 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2596 OS << *E->getParameterPack();
2599 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2600 PrintExpr(Node->GetTemporaryExpr());
2603 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2606 PrintExpr(E->getLHS());
2607 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2611 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2612 PrintExpr(E->getRHS());
2617 // C++ Coroutines TS
2619 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2620 Visit(S->getBody());
2623 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2625 if (S->getOperand()) {
2627 Visit(S->getOperand());
2632 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2634 PrintExpr(S->getOperand());
2637 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2639 PrintExpr(S->getOperand());
2642 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2644 PrintExpr(S->getOperand());
2649 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2651 VisitStringLiteral(Node->getString());
2654 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2656 Visit(E->getSubExpr());
2659 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2661 ObjCArrayLiteral::child_range Ch = E->children();
2662 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2663 if (I != Ch.begin())
2670 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2672 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2676 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2679 Visit(Element.Value);
2680 if (Element.isPackExpansion())
2686 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2688 Node->getEncodedType().print(OS, Policy);
2692 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2694 Node->getSelector().print(OS);
2698 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2699 OS << "@protocol(" << *Node->getProtocol() << ')';
2702 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2704 switch (Mess->getReceiverKind()) {
2705 case ObjCMessageExpr::Instance:
2706 PrintExpr(Mess->getInstanceReceiver());
2709 case ObjCMessageExpr::Class:
2710 Mess->getClassReceiver().print(OS, Policy);
2713 case ObjCMessageExpr::SuperInstance:
2714 case ObjCMessageExpr::SuperClass:
2720 Selector selector = Mess->getSelector();
2721 if (selector.isUnarySelector()) {
2722 OS << selector.getNameForSlot(0);
2724 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2725 if (i < selector.getNumArgs()) {
2726 if (i > 0) OS << ' ';
2727 if (selector.getIdentifierInfoForSlot(i))
2728 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2732 else OS << ", "; // Handle variadic methods.
2734 PrintExpr(Mess->getArg(i));
2740 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2741 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2745 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2746 PrintExpr(E->getSubExpr());
2750 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2751 OS << '(' << E->getBridgeKindName();
2752 E->getType().print(OS, Policy);
2754 PrintExpr(E->getSubExpr());
2757 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2758 BlockDecl *BD = Node->getBlockDecl();
2761 const FunctionType *AFT = Node->getFunctionType();
2763 if (isa<FunctionNoProtoType>(AFT)) {
2765 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2767 for (BlockDecl::param_iterator AI = BD->param_begin(),
2768 E = BD->param_end(); AI != E; ++AI) {
2769 if (AI != BD->param_begin()) OS << ", ";
2770 std::string ParamStr = (*AI)->getNameAsString();
2771 (*AI)->getType().print(OS, Policy, ParamStr);
2774 const auto *FT = cast<FunctionProtoType>(AFT);
2775 if (FT->isVariadic()) {
2776 if (!BD->param_empty()) OS << ", ";
2784 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2785 PrintExpr(Node->getSourceExpr());
2788 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2789 // TODO: Print something reasonable for a TypoExpr, if necessary.
2790 llvm_unreachable("Cannot print TypoExpr nodes");
2793 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2794 OS << "__builtin_astype(";
2795 PrintExpr(Node->getSrcExpr());
2797 Node->getType().print(OS, Policy);
2801 //===----------------------------------------------------------------------===//
2802 // Stmt method implementations
2803 //===----------------------------------------------------------------------===//
2805 void Stmt::dumpPretty(const ASTContext &Context) const {
2806 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2809 void Stmt::printPretty(raw_ostream &OS, PrinterHelper *Helper,
2810 const PrintingPolicy &Policy, unsigned Indentation,
2811 const ASTContext *Context) const {
2812 StmtPrinter P(OS, Helper, Policy, Indentation, Context);
2813 P.Visit(const_cast<Stmt*>(this));
2816 //===----------------------------------------------------------------------===//
2818 //===----------------------------------------------------------------------===//
2820 // Implement virtual destructor.
2821 PrinterHelper::~PrinterHelper() = default;