47 #include "llvm/ADT/ArrayRef.h"
48 #include "llvm/ADT/SmallString.h"
49 #include "llvm/ADT/SmallVector.h"
50 #include "llvm/ADT/StringExtras.h"
51 #include "llvm/ADT/StringRef.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/raw_ostream.h"
59 using namespace clang;
67 class StmtPrinter :
public StmtVisitor<StmtPrinter> {
78 StringRef NL =
"\n",
const ASTContext *Context =
nullptr)
79 :
OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
80 NL(NL), Context(Context) {}
84 void PrintStmt(
Stmt *S,
int SubIndent) {
85 IndentLevel += SubIndent;
86 if (S && isa<Expr>(S)) {
94 Indent() <<
"<<<NULL STATEMENT>>>" << NL;
96 IndentLevel -= SubIndent;
99 void PrintInitStmt(
Stmt *S,
unsigned PrefixWidth) {
101 IndentLevel += (PrefixWidth + 1) / 2;
102 if (
auto *DS = dyn_cast<DeclStmt>(S))
103 PrintRawDeclStmt(DS);
105 PrintExpr(cast<Expr>(S));
107 IndentLevel -= (PrefixWidth + 1) / 2;
110 void PrintControlledStmt(
Stmt *S) {
111 if (
auto *CS = dyn_cast<CompoundStmt>(S)) {
113 PrintRawCompoundStmt(CS);
122 void PrintRawDecl(
Decl *D);
123 void PrintRawDeclStmt(
const DeclStmt *S);
124 void PrintRawIfStmt(
IfStmt *If);
130 bool ForceNoStmt =
false);
132 void PrintExpr(
Expr *E) {
139 raw_ostream &
Indent(
int Delta = 0) {
140 for (
int i = 0, e = IndentLevel+Delta; i < e; ++i)
145 void Visit(
Stmt* S) {
151 void VisitStmt(
Stmt *
Node) LLVM_ATTRIBUTE_UNUSED {
152 Indent() <<
"<<unknown stmt type>>" << NL;
155 void VisitExpr(
Expr *
Node) LLVM_ATTRIBUTE_UNUSED {
156 OS <<
"<<unknown expr type>>";
161 #define ABSTRACT_STMT(CLASS)
162 #define STMT(CLASS, PARENT) \
163 void Visit##CLASS(CLASS *Node);
164 #include "clang/AST/StmtNodes.inc"
177 for (
auto *I :
Node->body())
183 void StmtPrinter::PrintRawDecl(
Decl *D) {
184 D->
print(OS, Policy, IndentLevel);
187 void StmtPrinter::PrintRawDeclStmt(
const DeclStmt *S) {
198 PrintRawDeclStmt(
Node);
204 PrintRawCompoundStmt(
Node);
210 PrintExpr(
Node->getLHS());
211 if (
Node->getRHS()) {
213 PrintExpr(
Node->getRHS());
217 PrintStmt(
Node->getSubStmt(), 0);
221 Indent(-1) <<
"default:" << NL;
222 PrintStmt(
Node->getSubStmt(), 0);
227 PrintStmt(
Node->getSubStmt(), 0);
231 for (
const auto *
Attr :
Node->getAttrs()) {
235 PrintStmt(
Node->getSubStmt(), 0);
238 void StmtPrinter::PrintRawIfStmt(
IfStmt *If) {
257 PrintInitStmt(If->
getInit(), 4);
259 PrintRawDeclStmt(DS);
264 if (
auto *CS = dyn_cast<CompoundStmt>(If->
getThen())) {
266 PrintRawCompoundStmt(CS);
277 if (
auto *CS = dyn_cast<CompoundStmt>(Else)) {
279 PrintRawCompoundStmt(CS);
281 }
else if (
auto *ElseIf = dyn_cast<IfStmt>(Else)) {
283 PrintRawIfStmt(ElseIf);
291 void StmtPrinter::VisitIfStmt(
IfStmt *If) {
299 PrintInitStmt(
Node->getInit(), 8);
300 if (
const DeclStmt *DS =
Node->getConditionVariableDeclStmt())
301 PrintRawDeclStmt(DS);
303 PrintExpr(
Node->getCond());
305 PrintControlledStmt(
Node->getBody());
310 if (
const DeclStmt *DS =
Node->getConditionVariableDeclStmt())
311 PrintRawDeclStmt(DS);
313 PrintExpr(
Node->getCond());
315 PrintStmt(
Node->getBody());
320 if (
auto *CS = dyn_cast<CompoundStmt>(
Node->getBody())) {
321 PrintRawCompoundStmt(CS);
325 PrintStmt(
Node->getBody());
330 PrintExpr(
Node->getCond());
337 PrintInitStmt(
Node->getInit(), 5);
339 OS << (
Node->getCond() ?
"; " :
";");
341 PrintExpr(
Node->getCond());
343 if (
Node->getInc()) {
345 PrintExpr(
Node->getInc());
348 PrintControlledStmt(
Node->getBody());
353 if (
auto *DS = dyn_cast<DeclStmt>(
Node->getElement()))
354 PrintRawDeclStmt(DS);
356 PrintExpr(cast<Expr>(
Node->getElement()));
358 PrintExpr(
Node->getCollection());
360 PrintControlledStmt(
Node->getBody());
366 PrintInitStmt(
Node->getInit(), 5);
368 SubPolicy.SuppressInitializers =
true;
369 Node->getLoopVariable()->
print(OS, SubPolicy, IndentLevel);
371 PrintExpr(
Node->getRangeInit());
373 PrintControlledStmt(
Node->getBody());
378 if (
Node->isIfExists())
379 OS <<
"__if_exists (";
381 OS <<
"__if_not_exists (";
384 =
Node->getQualifierLoc().getNestedNameSpecifier())
387 OS <<
Node->getNameInfo() <<
") ";
389 PrintRawCompoundStmt(
Node->getSubStmt());
393 Indent() <<
"goto " <<
Node->getLabel()->getName() <<
";";
399 PrintExpr(
Node->getTarget());
416 if (
Node->getRetValue()) {
418 PrintExpr(
Node->getRetValue());
427 if (
Node->isVolatile())
430 if (
Node->isAsmGoto())
434 VisitStringLiteral(
Node->getAsmString());
437 if (
Node->getNumOutputs() != 0 ||
Node->getNumInputs() != 0 ||
438 Node->getNumClobbers() != 0 ||
Node->getNumLabels() != 0)
441 for (
unsigned i = 0, e =
Node->getNumOutputs(); i != e; ++i) {
445 if (!
Node->getOutputName(i).empty()) {
447 OS <<
Node->getOutputName(i);
451 VisitStringLiteral(
Node->getOutputConstraintLiteral(i));
453 Visit(
Node->getOutputExpr(i));
458 if (
Node->getNumInputs() != 0 ||
Node->getNumClobbers() != 0 ||
459 Node->getNumLabels() != 0)
462 for (
unsigned i = 0, e =
Node->getNumInputs(); i != e; ++i) {
466 if (!
Node->getInputName(i).empty()) {
468 OS <<
Node->getInputName(i);
472 VisitStringLiteral(
Node->getInputConstraintLiteral(i));
474 Visit(
Node->getInputExpr(i));
479 if (
Node->getNumClobbers() != 0 ||
Node->getNumLabels())
482 for (
unsigned i = 0, e =
Node->getNumClobbers(); i != e; ++i) {
486 VisitStringLiteral(
Node->getClobberStringLiteral(i));
490 if (
Node->getNumLabels() != 0)
493 for (
unsigned i = 0, e =
Node->getNumLabels(); i != e; ++i) {
496 OS <<
Node->getLabelName(i);
506 if (
Node->hasBraces())
508 OS <<
Node->getAsmString() << NL;
509 if (
Node->hasBraces())
514 PrintStmt(
Node->getCapturedDecl()->getBody());
519 if (
auto *TS = dyn_cast<CompoundStmt>(
Node->getTryBody())) {
520 PrintRawCompoundStmt(TS);
526 if (
Decl *DS = catchStmt->getCatchParamDecl())
529 if (
auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
530 PrintRawCompoundStmt(CS);
537 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
546 Indent() <<
"@catch (...) { /* todo */ } " << NL;
551 if (
Node->getThrowExpr()) {
553 PrintExpr(
Node->getThrowExpr());
558 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
560 OS <<
"@available(...)";
564 Indent() <<
"@synchronized (";
565 PrintExpr(
Node->getSynchExpr());
567 PrintRawCompoundStmt(
Node->getSynchBody());
572 Indent() <<
"@autoreleasepool";
573 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(
Node->getSubStmt()));
579 if (
Decl *ExDecl =
Node->getExceptionDecl())
580 PrintRawDecl(ExDecl);
584 PrintRawCompoundStmt(cast<CompoundStmt>(
Node->getHandlerBlock()));
589 PrintRawCXXCatchStmt(
Node);
595 PrintRawCompoundStmt(
Node->getTryBlock());
596 for (
unsigned i = 0, e =
Node->getNumHandlers(); i < e; ++i) {
598 PrintRawCXXCatchStmt(
Node->getHandler(i));
604 Indent() << (
Node->getIsCXXTry() ?
"try " :
"__try ");
605 PrintRawCompoundStmt(
Node->getTryBlock());
609 PrintRawSEHExceptHandler(E);
611 assert(F &&
"Must have a finally block...");
612 PrintRawSEHFinallyStmt(F);
619 PrintRawCompoundStmt(
Node->getBlock());
625 VisitExpr(
Node->getFilterExpr());
627 PrintRawCompoundStmt(
Node->getBlock());
633 PrintRawSEHExceptHandler(
Node);
639 PrintRawSEHFinallyStmt(
Node);
653 PrintStmt(
Node->getLoopStmt());
660 for (
auto *Clause : Clauses)
661 if (Clause && !Clause->isImplicit()) {
663 Printer.Visit(Clause);
666 if (!ForceNoStmt && S->hasAssociatedStmt())
667 PrintStmt(S->getRawStmt());
671 Indent() <<
"#pragma omp metadirective";
672 PrintOMPExecutableDirective(
Node);
676 Indent() <<
"#pragma omp parallel";
677 PrintOMPExecutableDirective(
Node);
681 Indent() <<
"#pragma omp simd";
682 PrintOMPExecutableDirective(
Node);
686 Indent() <<
"#pragma omp tile";
687 PrintOMPExecutableDirective(
Node);
691 Indent() <<
"#pragma omp unroll";
692 PrintOMPExecutableDirective(
Node);
696 Indent() <<
"#pragma omp for";
697 PrintOMPExecutableDirective(
Node);
701 Indent() <<
"#pragma omp for simd";
702 PrintOMPExecutableDirective(
Node);
706 Indent() <<
"#pragma omp sections";
707 PrintOMPExecutableDirective(
Node);
711 Indent() <<
"#pragma omp section";
712 PrintOMPExecutableDirective(
Node);
716 Indent() <<
"#pragma omp single";
717 PrintOMPExecutableDirective(
Node);
721 Indent() <<
"#pragma omp master";
722 PrintOMPExecutableDirective(
Node);
726 Indent() <<
"#pragma omp critical";
727 if (
Node->getDirectiveName().getName()) {
729 Node->getDirectiveName().printName(OS, Policy);
732 PrintOMPExecutableDirective(
Node);
736 Indent() <<
"#pragma omp parallel for";
737 PrintOMPExecutableDirective(
Node);
740 void StmtPrinter::VisitOMPParallelForSimdDirective(
742 Indent() <<
"#pragma omp parallel for simd";
743 PrintOMPExecutableDirective(
Node);
746 void StmtPrinter::VisitOMPParallelMasterDirective(
748 Indent() <<
"#pragma omp parallel master";
749 PrintOMPExecutableDirective(
Node);
752 void StmtPrinter::VisitOMPParallelSectionsDirective(
754 Indent() <<
"#pragma omp parallel sections";
755 PrintOMPExecutableDirective(
Node);
759 Indent() <<
"#pragma omp task";
760 PrintOMPExecutableDirective(
Node);
764 Indent() <<
"#pragma omp taskyield";
765 PrintOMPExecutableDirective(
Node);
769 Indent() <<
"#pragma omp barrier";
770 PrintOMPExecutableDirective(
Node);
774 Indent() <<
"#pragma omp taskwait";
775 PrintOMPExecutableDirective(
Node);
779 Indent() <<
"#pragma omp taskgroup";
780 PrintOMPExecutableDirective(
Node);
784 Indent() <<
"#pragma omp flush";
785 PrintOMPExecutableDirective(
Node);
789 Indent() <<
"#pragma omp depobj";
790 PrintOMPExecutableDirective(
Node);
794 Indent() <<
"#pragma omp scan";
795 PrintOMPExecutableDirective(
Node);
799 Indent() <<
"#pragma omp ordered";
804 Indent() <<
"#pragma omp atomic";
805 PrintOMPExecutableDirective(
Node);
809 Indent() <<
"#pragma omp target";
810 PrintOMPExecutableDirective(
Node);
814 Indent() <<
"#pragma omp target data";
815 PrintOMPExecutableDirective(
Node);
818 void StmtPrinter::VisitOMPTargetEnterDataDirective(
820 Indent() <<
"#pragma omp target enter data";
821 PrintOMPExecutableDirective(
Node,
true);
824 void StmtPrinter::VisitOMPTargetExitDataDirective(
826 Indent() <<
"#pragma omp target exit data";
827 PrintOMPExecutableDirective(
Node,
true);
830 void StmtPrinter::VisitOMPTargetParallelDirective(
832 Indent() <<
"#pragma omp target parallel";
833 PrintOMPExecutableDirective(
Node);
836 void StmtPrinter::VisitOMPTargetParallelForDirective(
838 Indent() <<
"#pragma omp target parallel for";
839 PrintOMPExecutableDirective(
Node);
843 Indent() <<
"#pragma omp teams";
844 PrintOMPExecutableDirective(
Node);
847 void StmtPrinter::VisitOMPCancellationPointDirective(
849 Indent() <<
"#pragma omp cancellation point "
850 << getOpenMPDirectiveName(
Node->getCancelRegion());
851 PrintOMPExecutableDirective(
Node);
855 Indent() <<
"#pragma omp cancel "
856 << getOpenMPDirectiveName(
Node->getCancelRegion());
857 PrintOMPExecutableDirective(
Node);
861 Indent() <<
"#pragma omp taskloop";
862 PrintOMPExecutableDirective(
Node);
865 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
867 Indent() <<
"#pragma omp taskloop simd";
868 PrintOMPExecutableDirective(
Node);
871 void StmtPrinter::VisitOMPMasterTaskLoopDirective(
873 Indent() <<
"#pragma omp master taskloop";
874 PrintOMPExecutableDirective(
Node);
877 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
879 Indent() <<
"#pragma omp master taskloop simd";
880 PrintOMPExecutableDirective(
Node);
883 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
885 Indent() <<
"#pragma omp parallel master taskloop";
886 PrintOMPExecutableDirective(
Node);
889 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
891 Indent() <<
"#pragma omp parallel master taskloop simd";
892 PrintOMPExecutableDirective(
Node);
896 Indent() <<
"#pragma omp distribute";
897 PrintOMPExecutableDirective(
Node);
900 void StmtPrinter::VisitOMPTargetUpdateDirective(
902 Indent() <<
"#pragma omp target update";
903 PrintOMPExecutableDirective(
Node,
true);
906 void StmtPrinter::VisitOMPDistributeParallelForDirective(
908 Indent() <<
"#pragma omp distribute parallel for";
909 PrintOMPExecutableDirective(
Node);
912 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
914 Indent() <<
"#pragma omp distribute parallel for simd";
915 PrintOMPExecutableDirective(
Node);
918 void StmtPrinter::VisitOMPDistributeSimdDirective(
920 Indent() <<
"#pragma omp distribute simd";
921 PrintOMPExecutableDirective(
Node);
924 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
926 Indent() <<
"#pragma omp target parallel for simd";
927 PrintOMPExecutableDirective(
Node);
931 Indent() <<
"#pragma omp target simd";
932 PrintOMPExecutableDirective(
Node);
935 void StmtPrinter::VisitOMPTeamsDistributeDirective(
937 Indent() <<
"#pragma omp teams distribute";
938 PrintOMPExecutableDirective(
Node);
941 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
943 Indent() <<
"#pragma omp teams distribute simd";
944 PrintOMPExecutableDirective(
Node);
947 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
949 Indent() <<
"#pragma omp teams distribute parallel for simd";
950 PrintOMPExecutableDirective(
Node);
953 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
955 Indent() <<
"#pragma omp teams distribute parallel for";
956 PrintOMPExecutableDirective(
Node);
960 Indent() <<
"#pragma omp target teams";
961 PrintOMPExecutableDirective(
Node);
964 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
966 Indent() <<
"#pragma omp target teams distribute";
967 PrintOMPExecutableDirective(
Node);
970 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
972 Indent() <<
"#pragma omp target teams distribute parallel for";
973 PrintOMPExecutableDirective(
Node);
976 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
978 Indent() <<
"#pragma omp target teams distribute parallel for simd";
979 PrintOMPExecutableDirective(
Node);
982 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
984 Indent() <<
"#pragma omp target teams distribute simd";
985 PrintOMPExecutableDirective(
Node);
989 Indent() <<
"#pragma omp interop";
990 PrintOMPExecutableDirective(
Node);
994 Indent() <<
"#pragma omp dispatch";
995 PrintOMPExecutableDirective(
Node);
999 Indent() <<
"#pragma omp masked";
1000 PrintOMPExecutableDirective(
Node);
1004 Indent() <<
"#pragma omp loop";
1005 PrintOMPExecutableDirective(
Node);
1008 void StmtPrinter::VisitOMPTeamsGenericLoopDirective(
1010 Indent() <<
"#pragma omp teams loop";
1011 PrintOMPExecutableDirective(
Node);
1014 void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective(
1016 Indent() <<
"#pragma omp target teams loop";
1017 PrintOMPExecutableDirective(
Node);
1020 void StmtPrinter::VisitOMPParallelGenericLoopDirective(
1022 Indent() <<
"#pragma omp parallel loop";
1023 PrintOMPExecutableDirective(
Node);
1026 void StmtPrinter::VisitOMPTargetParallelGenericLoopDirective(
1028 Indent() <<
"#pragma omp target parallel loop";
1029 PrintOMPExecutableDirective(
Node);
1037 OS <<
Node->getBuiltinStr() <<
"()";
1041 PrintExpr(
Node->getSubExpr());
1045 if (
const auto *OCED = dyn_cast<OMPCapturedExprDecl>(
Node->getDecl())) {
1046 OCED->getInit()->IgnoreImpCasts()->printPretty(OS,
nullptr, Policy);
1049 if (
const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(
Node->getDecl())) {
1050 TPOD->printAsExpr(OS, Policy);
1055 if (
Node->hasTemplateKeyword())
1058 isa<ParmVarDecl, NonTypeTemplateParmDecl>(
Node->getDecl()) &&
1059 Node->getDecl()->getIdentifier())
1060 OS <<
Node->getDecl()->getIdentifier()->deuglifiedName();
1062 Node->getNameInfo().printName(OS, Policy);
1063 if (
Node->hasExplicitTemplateArgs()) {
1065 if (!
Node->hadMultipleCandidates())
1066 if (
auto *TD = dyn_cast<TemplateDecl>(
Node->getDecl()))
1067 TPL = TD->getTemplateParameters();
1072 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1076 if (
Node->hasTemplateKeyword())
1078 OS <<
Node->getNameInfo();
1079 if (
Node->hasExplicitTemplateArgs())
1084 if (
Node->getQualifier())
1085 Node->getQualifier()->
print(OS, Policy);
1086 if (
Node->hasTemplateKeyword())
1088 OS <<
Node->getNameInfo();
1089 if (
Node->hasExplicitTemplateArgs())
1094 if (
const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1095 if (
const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1097 DRE->getBeginLoc().isInvalid())
1105 if (
Node->getBase()) {
1108 PrintExpr(
Node->getBase());
1109 OS << (
Node->isArrow() ?
"->" :
".");
1116 if (
Node->isSuperReceiver())
1118 else if (
Node->isObjectReceiver() &&
Node->getBase()) {
1119 PrintExpr(
Node->getBase());
1121 }
else if (
Node->isClassReceiver() &&
Node->getClassReceiver()) {
1122 OS <<
Node->getClassReceiver()->getName() <<
".";
1125 if (
Node->isImplicitProperty()) {
1126 if (
const auto *Getter =
Node->getImplicitPropertyGetter())
1127 Getter->getSelector().
print(OS);
1130 Node->getImplicitPropertySetter()->getSelector());
1132 OS <<
Node->getExplicitProperty()->getName();
1136 PrintExpr(
Node->getBaseExpr());
1138 PrintExpr(
Node->getKeyExpr());
1142 void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1144 OS <<
"__builtin_sycl_unique_stable_name(";
1145 Node->getTypeSourceInfo()->getType().
print(OS, Policy);
1163 bool Invalid =
false;
1177 bool isSigned =
Node->getType()->isSignedIntegerType();
1180 if (isa<BitIntType>(
Node->getType())) {
1181 OS << (isSigned ?
"wb" :
"uwb");
1187 default: llvm_unreachable(
"Unexpected type for integer literal!");
1188 case BuiltinType::Char_S:
1189 case BuiltinType::Char_U:
OS <<
"i8";
break;
1190 case BuiltinType::UChar:
OS <<
"Ui8";
break;
1191 case BuiltinType::Short:
OS <<
"i16";
break;
1192 case BuiltinType::UShort:
OS <<
"Ui16";
break;
1193 case BuiltinType::Int:
break;
1194 case BuiltinType::UInt:
OS <<
'U';
break;
1195 case BuiltinType::Long:
OS <<
'L';
break;
1196 case BuiltinType::ULong:
OS <<
"UL";
break;
1197 case BuiltinType::LongLong:
OS <<
"LL";
break;
1198 case BuiltinType::ULongLong:
OS <<
"ULL";
break;
1199 case BuiltinType::Int128:
1201 case BuiltinType::UInt128:
1209 OS <<
Node->getValueAsString(10);
1212 default: llvm_unreachable(
"Unexpected type for fixed point literal!");
1213 case BuiltinType::ShortFract:
OS <<
"hr";
break;
1214 case BuiltinType::ShortAccum:
OS <<
"hk";
break;
1215 case BuiltinType::UShortFract:
OS <<
"uhr";
break;
1216 case BuiltinType::UShortAccum:
OS <<
"uhk";
break;
1217 case BuiltinType::Fract:
OS <<
"r";
break;
1218 case BuiltinType::Accum:
OS <<
"k";
break;
1219 case BuiltinType::UFract:
OS <<
"ur";
break;
1220 case BuiltinType::UAccum:
OS <<
"uk";
break;
1221 case BuiltinType::LongFract:
OS <<
"lr";
break;
1222 case BuiltinType::LongAccum:
OS <<
"lk";
break;
1223 case BuiltinType::ULongFract:
OS <<
"ulr";
break;
1224 case BuiltinType::ULongAccum:
OS <<
"ulk";
break;
1231 Node->getValue().toString(Str);
1233 if (Str.find_first_not_of(
"-0123456789") == StringRef::npos)
1241 default: llvm_unreachable(
"Unexpected type for float literal!");
1242 case BuiltinType::Half:
break;
1243 case BuiltinType::Ibm128:
break;
1244 case BuiltinType::Double:
break;
1245 case BuiltinType::Float16: OS <<
"F16";
break;
1247 case BuiltinType::LongDouble: OS <<
'L';
break;
1248 case BuiltinType::Float128: OS <<
'Q';
break;
1259 PrintExpr(
Node->getSubExpr());
1269 PrintExpr(
Node->getSubExpr());
1274 if (!
Node->isPostfix()) {
1279 switch (
Node->getOpcode()) {
1288 if (isa<UnaryOperator>(
Node->getSubExpr()))
1293 PrintExpr(
Node->getSubExpr());
1295 if (
Node->isPostfix())
1300 OS <<
"__builtin_offsetof(";
1301 Node->getTypeSourceInfo()->getType().
print(OS, Policy);
1303 bool PrintedSomething =
false;
1304 for (
unsigned i = 0, n =
Node->getNumComponents(); i < n; ++i) {
1311 PrintedSomething =
true;
1324 if (PrintedSomething)
1327 PrintedSomething =
true;
1328 OS <<
Id->getName();
1333 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1336 if (
Node->getKind() == UETT_AlignOf) {
1338 Spelling =
"alignof";
1340 Spelling =
"_Alignof";
1342 Spelling =
"__alignof";
1347 if (
Node->isArgumentType()) {
1349 Node->getArgumentType().
print(OS, Policy);
1353 PrintExpr(
Node->getArgumentExpr());
1359 PrintExpr(
Node->getControllingExpr());
1366 T.
print(OS, Policy);
1368 PrintExpr(Assoc.getAssociationExpr());
1374 PrintExpr(
Node->getLHS());
1376 PrintExpr(
Node->getRHS());
1381 PrintExpr(
Node->getBase());
1383 PrintExpr(
Node->getRowIdx());
1386 PrintExpr(
Node->getColumnIdx());
1391 PrintExpr(
Node->getBase());
1393 if (
Node->getLowerBound())
1394 PrintExpr(
Node->getLowerBound());
1395 if (
Node->getColonLocFirst().isValid()) {
1397 if (
Node->getLength())
1398 PrintExpr(
Node->getLength());
1400 if (
Node->getColonLocSecond().isValid()) {
1402 if (
Node->getStride())
1403 PrintExpr(
Node->getStride());
1410 for (
Expr *E :
Node->getDimensions()) {
1416 PrintExpr(
Node->getBase());
1421 for (
unsigned I = 0, E =
Node->numOfIterators(); I < E; ++I) {
1422 auto *VD = cast<ValueDecl>(
Node->getIteratorDecl(I));
1423 VD->getType().print(OS, Policy);
1425 OS <<
" " << VD->getName() <<
" = ";
1426 PrintExpr(
Range.Begin);
1428 PrintExpr(
Range.End);
1431 PrintExpr(
Range.Step);
1439 void StmtPrinter::PrintCallArgs(
CallExpr *Call) {
1440 for (
unsigned i = 0, e =
Call->getNumArgs(); i != e; ++i) {
1441 if (isa<CXXDefaultArgExpr>(
Call->getArg(i))) {
1447 PrintExpr(
Call->getArg(i));
1451 void StmtPrinter::VisitCallExpr(
CallExpr *Call) {
1452 PrintExpr(
Call->getCallee());
1454 PrintCallArgs(Call);
1459 if (
const auto *TE = dyn_cast<CXXThisExpr>(E))
1460 return TE->isImplicit();
1466 PrintExpr(
Node->getBase());
1468 auto *ParentMember = dyn_cast<MemberExpr>(
Node->getBase());
1470 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1474 OS << (
Node->isArrow() ?
"->" :
".");
1477 if (
auto *FD = dyn_cast<FieldDecl>(
Node->getMemberDecl()))
1478 if (FD->isAnonymousStructOrUnion())
1483 if (
Node->hasTemplateKeyword())
1485 OS <<
Node->getMemberNameInfo();
1487 if (
auto *FD = dyn_cast<FunctionDecl>(
Node->getMemberDecl())) {
1488 if (!
Node->hadMultipleCandidates())
1489 if (
auto *FTD = FD->getPrimaryTemplate())
1490 TPL = FTD->getTemplateParameters();
1491 }
else if (
auto *VTSD =
1492 dyn_cast<VarTemplateSpecializationDecl>(
Node->getMemberDecl()))
1493 TPL = VTSD->getSpecializedTemplate()->getTemplateParameters();
1494 if (
Node->hasExplicitTemplateArgs())
1499 PrintExpr(
Node->getBase());
1500 OS << (
Node->isArrow() ?
"->isa" :
".isa");
1504 PrintExpr(
Node->getBase());
1506 OS <<
Node->getAccessor().getName();
1511 Node->getTypeAsWritten().
print(OS, Policy);
1513 PrintExpr(
Node->getSubExpr());
1520 PrintExpr(
Node->getInitializer());
1525 PrintExpr(
Node->getSubExpr());
1529 PrintExpr(
Node->getLHS());
1531 PrintExpr(
Node->getRHS());
1535 PrintExpr(
Node->getLHS());
1537 PrintExpr(
Node->getRHS());
1541 PrintExpr(
Node->getCond());
1543 PrintExpr(
Node->getLHS());
1545 PrintExpr(
Node->getRHS());
1552 PrintExpr(
Node->getCommon());
1554 PrintExpr(
Node->getFalseExpr());
1558 OS <<
"&&" <<
Node->getLabel()->getName();
1561 void StmtPrinter::VisitStmtExpr(
StmtExpr *E) {
1568 OS <<
"__builtin_choose_expr(";
1569 PrintExpr(
Node->getCond());
1571 PrintExpr(
Node->getLHS());
1573 PrintExpr(
Node->getRHS());
1577 void StmtPrinter::VisitGNUNullExpr(
GNUNullExpr *) {
1582 OS <<
"__builtin_shufflevector(";
1583 for (
unsigned i = 0, e =
Node->getNumSubExprs(); i != e; ++i) {
1585 PrintExpr(
Node->getExpr(i));
1591 OS <<
"__builtin_convertvector(";
1592 PrintExpr(
Node->getSrcExpr());
1599 if (
Node->getSyntacticForm()) {
1600 Visit(
Node->getSyntacticForm());
1605 for (
unsigned i = 0, e =
Node->getNumInits(); i != e; ++i) {
1607 if (
Node->getInit(i))
1608 PrintExpr(
Node->getInit(i));
1619 PrintExpr(
Node->getSubExpr());
1629 for (
unsigned i = 0, e =
Node->getNumExprs(); i != e; ++i) {
1631 PrintExpr(
Node->getExpr(i));
1637 bool NeedsEquals =
true;
1639 if (D.isFieldDesignator()) {
1640 if (D.getDotLoc().isInvalid()) {
1642 OS << II->getName() <<
":";
1643 NeedsEquals =
false;
1646 OS <<
"." << D.getFieldName()->getName();
1650 if (D.isArrayDesignator()) {
1651 PrintExpr(
Node->getArrayIndex(D));
1653 PrintExpr(
Node->getArrayRangeStart(D));
1655 PrintExpr(
Node->getArrayRangeEnd(D));
1665 PrintExpr(
Node->getInit());
1668 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1672 PrintExpr(
Node->getBase());
1675 OS <<
"/*updater*/";
1676 PrintExpr(
Node->getUpdater());
1681 OS <<
"/*no init*/";
1685 if (
Node->getType()->getAsCXXRecordDecl()) {
1686 OS <<
"/*implicit*/";
1690 OS <<
"/*implicit*/(";
1693 if (
Node->getType()->isRecordType())
1701 OS <<
"__builtin_va_arg(";
1702 PrintExpr(
Node->getSubExpr());
1709 PrintExpr(
Node->getSyntacticForm());
1713 const char *Name =
nullptr;
1714 switch (
Node->getOp()) {
1715 #define BUILTIN(ID, TYPE, ATTRS)
1716 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1717 case AtomicExpr::AO ## ID: \
1720 #include "clang/Basic/Builtins.def"
1725 PrintExpr(
Node->getPtr());
1726 if (
Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1727 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1728 Node->getOp() != AtomicExpr::AO__opencl_atomic_load &&
1729 Node->getOp() != AtomicExpr::AO__hip_atomic_load) {
1731 PrintExpr(
Node->getVal1());
1733 if (
Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1734 Node->isCmpXChg()) {
1736 PrintExpr(
Node->getVal2());
1738 if (
Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1739 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1741 PrintExpr(
Node->getWeak());
1743 if (
Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1744 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1746 PrintExpr(
Node->getOrder());
1748 if (
Node->isCmpXChg()) {
1750 PrintExpr(
Node->getOrderFail());
1758 if (
Kind == OO_PlusPlus ||
Kind == OO_MinusMinus) {
1759 if (
Node->getNumArgs() == 1) {
1761 PrintExpr(
Node->getArg(0));
1763 PrintExpr(
Node->getArg(0));
1766 }
else if (
Kind == OO_Arrow) {
1767 PrintExpr(
Node->getArg(0));
1768 }
else if (
Kind == OO_Call ||
Kind == OO_Subscript) {
1769 PrintExpr(
Node->getArg(0));
1770 OS << (
Kind == OO_Call ?
'(' :
'[');
1771 for (
unsigned ArgIdx = 1; ArgIdx <
Node->getNumArgs(); ++ArgIdx) {
1774 if (!isa<CXXDefaultArgExpr>(
Node->getArg(ArgIdx)))
1775 PrintExpr(
Node->getArg(ArgIdx));
1777 OS << (
Kind == OO_Call ?
')' :
']');
1778 }
else if (
Node->getNumArgs() == 1) {
1780 PrintExpr(
Node->getArg(0));
1781 }
else if (
Node->getNumArgs() == 2) {
1782 PrintExpr(
Node->getArg(0));
1784 PrintExpr(
Node->getArg(1));
1786 llvm_unreachable(
"unknown overloaded operator");
1793 if (MD && isa<CXXConversionDecl>(MD)) {
1794 PrintExpr(
Node->getImplicitObjectArgument());
1797 VisitCallExpr(cast<CallExpr>(
Node));
1801 PrintExpr(
Node->getCallee());
1803 PrintCallArgs(
Node->getConfig());
1805 PrintCallArgs(
Node);
1809 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1812 Node->getDecomposedForm();
1813 PrintExpr(
const_cast<Expr*
>(Decomposed.
LHS));
1815 PrintExpr(
const_cast<Expr*
>(Decomposed.
RHS));
1819 OS <<
Node->getCastName() <<
'<';
1820 Node->getTypeAsWritten().
print(OS, Policy);
1822 PrintExpr(
Node->getSubExpr());
1827 VisitCXXNamedCastExpr(
Node);
1831 VisitCXXNamedCastExpr(
Node);
1835 VisitCXXNamedCastExpr(
Node);
1839 VisitCXXNamedCastExpr(
Node);
1843 OS <<
"__builtin_bit_cast(";
1844 Node->getTypeInfoAsWritten()->getType().
print(OS, Policy);
1846 PrintExpr(
Node->getSubExpr());
1851 VisitCXXNamedCastExpr(
Node);
1856 if (
Node->isTypeOperand()) {
1857 Node->getTypeOperandSourceInfo()->getType().
print(OS, Policy);
1859 PrintExpr(
Node->getExprOperand());
1866 if (
Node->isTypeOperand()) {
1867 Node->getTypeOperandSourceInfo()->getType().
print(OS, Policy);
1869 PrintExpr(
Node->getExprOperand());
1875 PrintExpr(
Node->getBaseExpr());
1876 if (
Node->isArrow())
1881 Node->getQualifierLoc().getNestedNameSpecifier())
1883 OS <<
Node->getPropertyDecl()->getDeclName();
1887 PrintExpr(
Node->getBase());
1889 PrintExpr(
Node->getIdx());
1894 switch (
Node->getLiteralOperatorKind()) {
1896 OS << cast<StringLiteral>(
Node->getArg(0)->IgnoreImpCasts())->getString();
1899 const auto *DRE = cast<DeclRefExpr>(
Node->getCallee()->IgnoreImpCasts());
1901 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1904 if (Args->
size() != 1) {
1906 if (!DRE->hadMultipleCandidates())
1907 if (
const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl()))
1908 TPL = TD->getTemplateParameters();
1909 OS <<
"operator\"\"" <<
Node->getUDSuffix()->getName();
1917 char C = (char)
P.getAsIntegral().getZExtValue();
1924 const auto *Int = cast<IntegerLiteral>(
Node->getCookedLiteral());
1930 auto *
Float = cast<FloatingLiteral>(
Node->getCookedLiteral());
1936 PrintExpr(
Node->getCookedLiteral());
1939 OS <<
Node->getUDSuffix()->getName();
1943 OS << (
Node->getValue() ?
"true" :
"false");
1955 if (!
Node->getSubExpr())
1959 PrintExpr(
Node->getSubExpr());
1972 auto TargetType =
Node->getType();
1973 auto *
Auto = TargetType->getContainedDeducedType();
1974 bool Bare =
Auto &&
Auto->isDeduced();
1979 TargetType.print(OS, Policy);
1984 if (!
Node->isListInitialization())
1986 PrintExpr(
Node->getSubExpr());
1987 if (!
Node->isListInitialization())
1992 PrintExpr(
Node->getSubExpr());
1997 if (
Node->isStdInitListInitialization())
1999 else if (
Node->isListInitialization())
2004 ArgEnd =
Node->arg_end();
2005 Arg != ArgEnd; ++Arg) {
2006 if ((*Arg)->isDefaultArgument())
2008 if (Arg !=
Node->arg_begin())
2012 if (
Node->isStdInitListInitialization())
2014 else if (
Node->isListInitialization())
2022 bool NeedComma =
false;
2023 switch (
Node->getCaptureDefault()) {
2038 CEnd =
Node->explicit_capture_end();
2041 if (
C->capturesVLAType())
2048 switch (
C->getCaptureKind()) {
2060 OS <<
C->getCapturedVar()->getName();
2064 OS <<
C->getCapturedVar()->getName();
2068 llvm_unreachable(
"VLA type in explicit captures.");
2071 if (
C->isPackExpansion())
2074 if (
Node->isInitCapture(C)) {
2077 llvm::StringRef
Pre;
2078 llvm::StringRef
Post;
2080 !isa<ParenListExpr>(D->
getInit())) {
2094 if (!
Node->getExplicitTemplateParameters().empty()) {
2095 Node->getTemplateParameterList()->
print(
2096 OS,
Node->getLambdaClass()->getASTContext(),
2100 if (
Node->hasExplicitParameters()) {
2112 ?
P->getIdentifier()->deuglifiedName().str()
2113 :
P->getNameAsString();
2114 P->getOriginalType().print(OS, Policy, ParamStr);
2123 if (
Node->isMutable())
2132 if (
Node->hasExplicitResultType()) {
2134 Proto->getReturnType().print(OS, Policy);
2143 PrintRawCompoundStmt(
Node->getCompoundStmtBody());
2148 TSInfo->getType().print(OS, Policy);
2154 void StmtPrinter::VisitCXXNewExpr(
CXXNewExpr *E) {
2162 for (
unsigned i = 1; i < NumPlace; ++i) {
2174 llvm::raw_string_ostream
s(TypeS);
2177 (*Size)->printPretty(
s, Helper, Policy);
2216 OS << II->getName();
2225 for (
unsigned i = 0, e = E->
getNumArgs(); i != e; ++i) {
2226 if (isa<CXXDefaultArgExpr>(E->
getArg(i))) {
2241 OS <<
"<forwarded>";
2253 void StmtPrinter::VisitCXXUnresolvedConstructExpr(
2255 Node->getTypeAsWritten().
print(OS, Policy);
2256 if (!
Node->isListInitialization())
2258 for (
auto Arg =
Node->arg_begin(), ArgEnd =
Node->arg_end(); Arg != ArgEnd;
2260 if (Arg !=
Node->arg_begin())
2264 if (!
Node->isListInitialization())
2268 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2270 if (!
Node->isImplicitAccess()) {
2271 PrintExpr(
Node->getBase());
2272 OS << (
Node->isArrow() ?
"->" :
".");
2276 if (
Node->hasTemplateKeyword())
2278 OS <<
Node->getMemberNameInfo();
2279 if (
Node->hasExplicitTemplateArgs())
2284 if (!
Node->isImplicitAccess()) {
2285 PrintExpr(
Node->getBase());
2286 OS << (
Node->isArrow() ?
"->" :
".");
2290 if (
Node->hasTemplateKeyword())
2292 OS <<
Node->getMemberNameInfo();
2293 if (
Node->hasExplicitTemplateArgs())
2299 for (
unsigned I = 0, N = E->
getNumArgs(); I != N; ++I) {
2331 OS <<
"sizeof...(" << *E->
getPack() <<
")";
2334 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2336 OS << *
Node->getParameterPack();
2339 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2341 Visit(
Node->getReplacement());
2349 PrintExpr(
Node->getSubExpr());
2352 void StmtPrinter::VisitCXXFoldExpr(
CXXFoldExpr *E) {
2381 if (!LocalParameters.empty()) {
2384 PrintRawDecl(LocalParam);
2385 if (LocalParam != LocalParameters.back())
2394 if (
auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2395 if (TypeReq->isSubstitutionFailure())
2396 OS <<
"<<error-type>>";
2398 TypeReq->getType()->getType().print(OS, Policy);
2399 }
else if (
auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2400 if (ExprReq->isCompound())
2402 if (ExprReq->isExprSubstitutionFailure())
2403 OS <<
"<<error-expression>>";
2405 PrintExpr(ExprReq->getExpr());
2406 if (ExprReq->isCompound()) {
2408 if (ExprReq->getNoexceptLoc().isValid())
2410 const auto &RetReq = ExprReq->getReturnTypeRequirement();
2411 if (!RetReq.isEmpty()) {
2413 if (RetReq.isSubstitutionFailure())
2414 OS <<
"<<error-type>>";
2415 else if (RetReq.isTypeConstraint())
2416 RetReq.getTypeConstraint()->print(OS, Policy);
2420 auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2422 if (NestedReq->isSubstitutionFailure())
2423 OS <<
"<<error-expression>>";
2425 PrintExpr(NestedReq->getConstraintExpr());
2435 Visit(S->getBody());
2440 if (S->getOperand()) {
2442 Visit(S->getOperand());
2447 void StmtPrinter::VisitCoawaitExpr(
CoawaitExpr *S) {
2449 PrintExpr(S->getOperand());
2454 PrintExpr(S->getOperand());
2457 void StmtPrinter::VisitCoyieldExpr(
CoyieldExpr *S) {
2459 PrintExpr(S->getOperand());
2466 VisitStringLiteral(
Node->getString());
2477 for (
auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2478 if (I != Ch.begin())
2494 Visit(Element.
Value);
2503 Node->getEncodedType().
print(OS, Policy);
2514 OS <<
"@protocol(" << *
Node->getProtocol() <<
')';
2539 for (
unsigned i = 0, e = Mess->
getNumArgs(); i != e; ++i) {
2541 if (i > 0)
OS <<
' ';
2549 PrintExpr(Mess->
getArg(i));
2556 OS << (
Node->getValue() ?
"__objc_yes" :
"__objc_no");
2578 if (isa<FunctionNoProtoType>(AFT)) {
2580 }
else if (!BD->
param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2586 (*AI)->getType().print(OS, Policy, ParamStr);
2589 const auto *FT = cast<FunctionProtoType>(AFT);
2590 if (FT->isVariadic()) {
2600 PrintExpr(
Node->getSourceExpr());
2605 llvm_unreachable(
"Cannot print TypoExpr nodes");
2609 OS <<
"<recovery-expr>(";
2610 const char *Sep =
"";
2611 for (
Expr *E :
Node->subExpressions()) {
2620 OS <<
"__builtin_astype(";
2621 PrintExpr(
Node->getSrcExpr());
2637 StringRef NL,
const ASTContext *Context)
const {
2638 StmtPrinter
P(Out, Helper, Policy, Indentation, NL, Context);
2639 P.Visit(
const_cast<Stmt *
>(
this));
2644 unsigned Indentation, StringRef NL,
2646 StmtPrinter
P(Out, Helper, Policy, Indentation, NL, Context);
2647 P.PrintControlledStmt(
const_cast<Stmt *
>(
this));
2653 llvm::raw_string_ostream TempOut(Buf);