1 //===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which 10 // pretty print the AST back out to C code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/Attr.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclBase.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/DeclObjC.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/DeclTemplate.h" 22 #include "clang/AST/Expr.h" 23 #include "clang/AST/ExprCXX.h" 24 #include "clang/AST/ExprObjC.h" 25 #include "clang/AST/ExprOpenMP.h" 26 #include "clang/AST/NestedNameSpecifier.h" 27 #include "clang/AST/OpenMPClause.h" 28 #include "clang/AST/PrettyPrinter.h" 29 #include "clang/AST/Stmt.h" 30 #include "clang/AST/StmtCXX.h" 31 #include "clang/AST/StmtObjC.h" 32 #include "clang/AST/StmtOpenMP.h" 33 #include "clang/AST/StmtSYCL.h" 34 #include "clang/AST/StmtVisitor.h" 35 #include "clang/AST/TemplateBase.h" 36 #include "clang/AST/Type.h" 37 #include "clang/Basic/ExpressionTraits.h" 38 #include "clang/Basic/IdentifierTable.h" 39 #include "clang/Basic/JsonSupport.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/STLExtras.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" 56 #include <cassert> 57 #include <optional> 58 #include <string> 59 60 using namespace clang; 61 62 //===----------------------------------------------------------------------===// 63 // StmtPrinter Visitor 64 //===----------------------------------------------------------------------===// 65 66 namespace { 67 68 class StmtPrinter : public StmtVisitor<StmtPrinter> { 69 raw_ostream &OS; 70 unsigned IndentLevel; 71 PrinterHelper* Helper; 72 PrintingPolicy Policy; 73 std::string NL; 74 const ASTContext *Context; 75 76 public: 77 StmtPrinter(raw_ostream &os, PrinterHelper *helper, 78 const PrintingPolicy &Policy, unsigned Indentation = 0, 79 StringRef NL = "\n", const ASTContext *Context = nullptr) 80 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy), 81 NL(NL), Context(Context) {} 82 83 void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); } 84 85 void PrintStmt(Stmt *S, int SubIndent) { 86 IndentLevel += SubIndent; 87 if (isa_and_nonnull<Expr>(S)) { 88 // If this is an expr used in a stmt context, indent and newline it. 89 Indent(); 90 Visit(S); 91 OS << ";" << NL; 92 } else if (S) { 93 Visit(S); 94 } else { 95 Indent() << "<<<NULL STATEMENT>>>" << NL; 96 } 97 IndentLevel -= SubIndent; 98 } 99 100 void PrintInitStmt(Stmt *S, unsigned PrefixWidth) { 101 // FIXME: Cope better with odd prefix widths. 102 IndentLevel += (PrefixWidth + 1) / 2; 103 if (auto *DS = dyn_cast<DeclStmt>(S)) 104 PrintRawDeclStmt(DS); 105 else 106 PrintExpr(cast<Expr>(S)); 107 OS << "; "; 108 IndentLevel -= (PrefixWidth + 1) / 2; 109 } 110 111 void PrintControlledStmt(Stmt *S) { 112 if (auto *CS = dyn_cast<CompoundStmt>(S)) { 113 OS << " "; 114 PrintRawCompoundStmt(CS); 115 OS << NL; 116 } else { 117 OS << NL; 118 PrintStmt(S); 119 } 120 } 121 122 void PrintRawCompoundStmt(CompoundStmt *S); 123 void PrintRawDecl(Decl *D); 124 void PrintRawDeclStmt(const DeclStmt *S); 125 void PrintRawIfStmt(IfStmt *If); 126 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); 127 void PrintCallArgs(CallExpr *E); 128 void PrintRawSEHExceptHandler(SEHExceptStmt *S); 129 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S); 130 void PrintOMPExecutableDirective(OMPExecutableDirective *S, 131 bool ForceNoStmt = false); 132 void PrintFPPragmas(CompoundStmt *S); 133 void PrintOpenACCClauseList(OpenACCConstructStmt *S); 134 void PrintOpenACCConstruct(OpenACCConstructStmt *S); 135 136 void PrintExpr(Expr *E) { 137 if (E) 138 Visit(E); 139 else 140 OS << "<null expr>"; 141 } 142 143 raw_ostream &Indent(int Delta = 0) { 144 for (int i = 0, e = IndentLevel+Delta; i < e; ++i) 145 OS << " "; 146 return OS; 147 } 148 149 void Visit(Stmt* S) { 150 if (Helper && Helper->handledStmt(S,OS)) 151 return; 152 else StmtVisitor<StmtPrinter>::Visit(S); 153 } 154 155 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED { 156 Indent() << "<<unknown stmt type>>" << NL; 157 } 158 159 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED { 160 OS << "<<unknown expr type>>"; 161 } 162 163 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); 164 165 #define ABSTRACT_STMT(CLASS) 166 #define STMT(CLASS, PARENT) \ 167 void Visit##CLASS(CLASS *Node); 168 #include "clang/AST/StmtNodes.inc" 169 }; 170 171 } // namespace 172 173 //===----------------------------------------------------------------------===// 174 // Stmt printing methods. 175 //===----------------------------------------------------------------------===// 176 177 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and 178 /// with no newline after the }. 179 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { 180 assert(Node && "Compound statement cannot be null"); 181 OS << "{" << NL; 182 PrintFPPragmas(Node); 183 for (auto *I : Node->body()) 184 PrintStmt(I); 185 186 Indent() << "}"; 187 } 188 189 void StmtPrinter::PrintFPPragmas(CompoundStmt *S) { 190 if (!S->hasStoredFPFeatures()) 191 return; 192 FPOptionsOverride FPO = S->getStoredFPFeatures(); 193 bool FEnvAccess = false; 194 if (FPO.hasAllowFEnvAccessOverride()) { 195 FEnvAccess = FPO.getAllowFEnvAccessOverride(); 196 Indent() << "#pragma STDC FENV_ACCESS " << (FEnvAccess ? "ON" : "OFF") 197 << NL; 198 } 199 if (FPO.hasSpecifiedExceptionModeOverride()) { 200 LangOptions::FPExceptionModeKind EM = 201 FPO.getSpecifiedExceptionModeOverride(); 202 if (!FEnvAccess || EM != LangOptions::FPE_Strict) { 203 Indent() << "#pragma clang fp exceptions("; 204 switch (FPO.getSpecifiedExceptionModeOverride()) { 205 default: 206 break; 207 case LangOptions::FPE_Ignore: 208 OS << "ignore"; 209 break; 210 case LangOptions::FPE_MayTrap: 211 OS << "maytrap"; 212 break; 213 case LangOptions::FPE_Strict: 214 OS << "strict"; 215 break; 216 } 217 OS << ")\n"; 218 } 219 } 220 if (FPO.hasConstRoundingModeOverride()) { 221 LangOptions::RoundingMode RM = FPO.getConstRoundingModeOverride(); 222 Indent() << "#pragma STDC FENV_ROUND "; 223 switch (RM) { 224 case llvm::RoundingMode::TowardZero: 225 OS << "FE_TOWARDZERO"; 226 break; 227 case llvm::RoundingMode::NearestTiesToEven: 228 OS << "FE_TONEAREST"; 229 break; 230 case llvm::RoundingMode::TowardPositive: 231 OS << "FE_UPWARD"; 232 break; 233 case llvm::RoundingMode::TowardNegative: 234 OS << "FE_DOWNWARD"; 235 break; 236 case llvm::RoundingMode::NearestTiesToAway: 237 OS << "FE_TONEARESTFROMZERO"; 238 break; 239 case llvm::RoundingMode::Dynamic: 240 OS << "FE_DYNAMIC"; 241 break; 242 default: 243 llvm_unreachable("Invalid rounding mode"); 244 } 245 OS << NL; 246 } 247 } 248 249 void StmtPrinter::PrintRawDecl(Decl *D) { 250 D->print(OS, Policy, IndentLevel); 251 } 252 253 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) { 254 SmallVector<Decl *, 2> Decls(S->decls()); 255 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); 256 } 257 258 void StmtPrinter::VisitNullStmt(NullStmt *Node) { 259 Indent() << ";" << NL; 260 } 261 262 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { 263 Indent(); 264 PrintRawDeclStmt(Node); 265 OS << ";" << NL; 266 } 267 268 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { 269 Indent(); 270 PrintRawCompoundStmt(Node); 271 OS << "" << NL; 272 } 273 274 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { 275 Indent(-1) << "case "; 276 PrintExpr(Node->getLHS()); 277 if (Node->getRHS()) { 278 OS << " ... "; 279 PrintExpr(Node->getRHS()); 280 } 281 OS << ":" << NL; 282 283 PrintStmt(Node->getSubStmt(), 0); 284 } 285 286 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { 287 Indent(-1) << "default:" << NL; 288 PrintStmt(Node->getSubStmt(), 0); 289 } 290 291 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { 292 Indent(-1) << Node->getName() << ":" << NL; 293 PrintStmt(Node->getSubStmt(), 0); 294 } 295 296 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) { 297 llvm::ArrayRef<const Attr *> Attrs = Node->getAttrs(); 298 for (const auto *Attr : Attrs) { 299 Attr->printPretty(OS, Policy); 300 if (Attr != Attrs.back()) 301 OS << ' '; 302 } 303 304 PrintStmt(Node->getSubStmt(), 0); 305 } 306 307 void StmtPrinter::PrintRawIfStmt(IfStmt *If) { 308 if (If->isConsteval()) { 309 OS << "if "; 310 if (If->isNegatedConsteval()) 311 OS << "!"; 312 OS << "consteval"; 313 OS << NL; 314 PrintStmt(If->getThen()); 315 if (Stmt *Else = If->getElse()) { 316 Indent(); 317 OS << "else"; 318 PrintStmt(Else); 319 OS << NL; 320 } 321 return; 322 } 323 324 OS << "if ("; 325 if (If->getInit()) 326 PrintInitStmt(If->getInit(), 4); 327 if (const DeclStmt *DS = If->getConditionVariableDeclStmt()) 328 PrintRawDeclStmt(DS); 329 else 330 PrintExpr(If->getCond()); 331 OS << ')'; 332 333 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) { 334 OS << ' '; 335 PrintRawCompoundStmt(CS); 336 OS << (If->getElse() ? " " : NL); 337 } else { 338 OS << NL; 339 PrintStmt(If->getThen()); 340 if (If->getElse()) Indent(); 341 } 342 343 if (Stmt *Else = If->getElse()) { 344 OS << "else"; 345 346 if (auto *CS = dyn_cast<CompoundStmt>(Else)) { 347 OS << ' '; 348 PrintRawCompoundStmt(CS); 349 OS << NL; 350 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) { 351 OS << ' '; 352 PrintRawIfStmt(ElseIf); 353 } else { 354 OS << NL; 355 PrintStmt(If->getElse()); 356 } 357 } 358 } 359 360 void StmtPrinter::VisitIfStmt(IfStmt *If) { 361 Indent(); 362 PrintRawIfStmt(If); 363 } 364 365 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { 366 Indent() << "switch ("; 367 if (Node->getInit()) 368 PrintInitStmt(Node->getInit(), 8); 369 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 370 PrintRawDeclStmt(DS); 371 else 372 PrintExpr(Node->getCond()); 373 OS << ")"; 374 PrintControlledStmt(Node->getBody()); 375 } 376 377 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { 378 Indent() << "while ("; 379 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 380 PrintRawDeclStmt(DS); 381 else 382 PrintExpr(Node->getCond()); 383 OS << ")" << NL; 384 PrintStmt(Node->getBody()); 385 } 386 387 void StmtPrinter::VisitDoStmt(DoStmt *Node) { 388 Indent() << "do "; 389 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 390 PrintRawCompoundStmt(CS); 391 OS << " "; 392 } else { 393 OS << NL; 394 PrintStmt(Node->getBody()); 395 Indent(); 396 } 397 398 OS << "while ("; 399 PrintExpr(Node->getCond()); 400 OS << ");" << NL; 401 } 402 403 void StmtPrinter::VisitForStmt(ForStmt *Node) { 404 Indent() << "for ("; 405 if (Node->getInit()) 406 PrintInitStmt(Node->getInit(), 5); 407 else 408 OS << (Node->getCond() ? "; " : ";"); 409 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 410 PrintRawDeclStmt(DS); 411 else if (Node->getCond()) 412 PrintExpr(Node->getCond()); 413 OS << ";"; 414 if (Node->getInc()) { 415 OS << " "; 416 PrintExpr(Node->getInc()); 417 } 418 OS << ")"; 419 PrintControlledStmt(Node->getBody()); 420 } 421 422 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { 423 Indent() << "for ("; 424 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement())) 425 PrintRawDeclStmt(DS); 426 else 427 PrintExpr(cast<Expr>(Node->getElement())); 428 OS << " in "; 429 PrintExpr(Node->getCollection()); 430 OS << ")"; 431 PrintControlledStmt(Node->getBody()); 432 } 433 434 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) { 435 Indent() << "for ("; 436 if (Node->getInit()) 437 PrintInitStmt(Node->getInit(), 5); 438 PrintingPolicy SubPolicy(Policy); 439 SubPolicy.SuppressInitializers = true; 440 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel); 441 OS << " : "; 442 PrintExpr(Node->getRangeInit()); 443 OS << ")"; 444 PrintControlledStmt(Node->getBody()); 445 } 446 447 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) { 448 Indent(); 449 if (Node->isIfExists()) 450 OS << "__if_exists ("; 451 else 452 OS << "__if_not_exists ("; 453 454 if (NestedNameSpecifier *Qualifier 455 = Node->getQualifierLoc().getNestedNameSpecifier()) 456 Qualifier->print(OS, Policy); 457 458 OS << Node->getNameInfo() << ") "; 459 460 PrintRawCompoundStmt(Node->getSubStmt()); 461 } 462 463 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { 464 Indent() << "goto " << Node->getLabel()->getName() << ";"; 465 if (Policy.IncludeNewlines) OS << NL; 466 } 467 468 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 469 Indent() << "goto *"; 470 PrintExpr(Node->getTarget()); 471 OS << ";"; 472 if (Policy.IncludeNewlines) OS << NL; 473 } 474 475 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 476 Indent() << "continue;"; 477 if (Policy.IncludeNewlines) OS << NL; 478 } 479 480 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 481 Indent() << "break;"; 482 if (Policy.IncludeNewlines) OS << NL; 483 } 484 485 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 486 Indent() << "return"; 487 if (Node->getRetValue()) { 488 OS << " "; 489 PrintExpr(Node->getRetValue()); 490 } 491 OS << ";"; 492 if (Policy.IncludeNewlines) OS << NL; 493 } 494 495 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { 496 Indent() << "asm "; 497 498 if (Node->isVolatile()) 499 OS << "volatile "; 500 501 if (Node->isAsmGoto()) 502 OS << "goto "; 503 504 OS << "("; 505 VisitStringLiteral(Node->getAsmString()); 506 507 // Outputs 508 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 509 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0) 510 OS << " : "; 511 512 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 513 if (i != 0) 514 OS << ", "; 515 516 if (!Node->getOutputName(i).empty()) { 517 OS << '['; 518 OS << Node->getOutputName(i); 519 OS << "] "; 520 } 521 522 VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 523 OS << " ("; 524 Visit(Node->getOutputExpr(i)); 525 OS << ")"; 526 } 527 528 // Inputs 529 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 || 530 Node->getNumLabels() != 0) 531 OS << " : "; 532 533 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 534 if (i != 0) 535 OS << ", "; 536 537 if (!Node->getInputName(i).empty()) { 538 OS << '['; 539 OS << Node->getInputName(i); 540 OS << "] "; 541 } 542 543 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 544 OS << " ("; 545 Visit(Node->getInputExpr(i)); 546 OS << ")"; 547 } 548 549 // Clobbers 550 if (Node->getNumClobbers() != 0 || Node->getNumLabels()) 551 OS << " : "; 552 553 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 554 if (i != 0) 555 OS << ", "; 556 557 VisitStringLiteral(Node->getClobberStringLiteral(i)); 558 } 559 560 // Labels 561 if (Node->getNumLabels() != 0) 562 OS << " : "; 563 564 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) { 565 if (i != 0) 566 OS << ", "; 567 OS << Node->getLabelName(i); 568 } 569 570 OS << ");"; 571 if (Policy.IncludeNewlines) OS << NL; 572 } 573 574 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) { 575 // FIXME: Implement MS style inline asm statement printer. 576 Indent() << "__asm "; 577 if (Node->hasBraces()) 578 OS << "{" << NL; 579 OS << Node->getAsmString() << NL; 580 if (Node->hasBraces()) 581 Indent() << "}" << NL; 582 } 583 584 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) { 585 PrintStmt(Node->getCapturedDecl()->getBody()); 586 } 587 588 void StmtPrinter::VisitSYCLKernelCallStmt(SYCLKernelCallStmt *Node) { 589 PrintStmt(Node->getOutlinedFunctionDecl()->getBody()); 590 } 591 592 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 593 Indent() << "@try"; 594 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 595 PrintRawCompoundStmt(TS); 596 OS << NL; 597 } 598 599 for (ObjCAtCatchStmt *catchStmt : Node->catch_stmts()) { 600 Indent() << "@catch("; 601 if (Decl *DS = catchStmt->getCatchParamDecl()) 602 PrintRawDecl(DS); 603 OS << ")"; 604 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 605 PrintRawCompoundStmt(CS); 606 OS << NL; 607 } 608 } 609 610 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) { 611 Indent() << "@finally"; 612 if (auto *CS = dyn_cast<CompoundStmt>(FS->getFinallyBody())) { 613 PrintRawCompoundStmt(CS); 614 OS << NL; 615 } 616 } 617 } 618 619 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 620 } 621 622 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 623 Indent() << "@catch (...) { /* todo */ } " << NL; 624 } 625 626 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 627 Indent() << "@throw"; 628 if (Node->getThrowExpr()) { 629 OS << " "; 630 PrintExpr(Node->getThrowExpr()); 631 } 632 OS << ";" << NL; 633 } 634 635 void StmtPrinter::VisitObjCAvailabilityCheckExpr( 636 ObjCAvailabilityCheckExpr *Node) { 637 OS << "@available(...)"; 638 } 639 640 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 641 Indent() << "@synchronized ("; 642 PrintExpr(Node->getSynchExpr()); 643 OS << ")"; 644 PrintRawCompoundStmt(Node->getSynchBody()); 645 OS << NL; 646 } 647 648 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 649 Indent() << "@autoreleasepool"; 650 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getSubStmt())); 651 OS << NL; 652 } 653 654 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 655 OS << "catch ("; 656 if (Decl *ExDecl = Node->getExceptionDecl()) 657 PrintRawDecl(ExDecl); 658 else 659 OS << "..."; 660 OS << ") "; 661 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 662 } 663 664 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 665 Indent(); 666 PrintRawCXXCatchStmt(Node); 667 OS << NL; 668 } 669 670 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 671 Indent() << "try "; 672 PrintRawCompoundStmt(Node->getTryBlock()); 673 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 674 OS << " "; 675 PrintRawCXXCatchStmt(Node->getHandler(i)); 676 } 677 OS << NL; 678 } 679 680 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 681 Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 682 PrintRawCompoundStmt(Node->getTryBlock()); 683 SEHExceptStmt *E = Node->getExceptHandler(); 684 SEHFinallyStmt *F = Node->getFinallyHandler(); 685 if(E) 686 PrintRawSEHExceptHandler(E); 687 else { 688 assert(F && "Must have a finally block..."); 689 PrintRawSEHFinallyStmt(F); 690 } 691 OS << NL; 692 } 693 694 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 695 OS << "__finally "; 696 PrintRawCompoundStmt(Node->getBlock()); 697 OS << NL; 698 } 699 700 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 701 OS << "__except ("; 702 VisitExpr(Node->getFilterExpr()); 703 OS << ")" << NL; 704 PrintRawCompoundStmt(Node->getBlock()); 705 OS << NL; 706 } 707 708 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 709 Indent(); 710 PrintRawSEHExceptHandler(Node); 711 OS << NL; 712 } 713 714 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 715 Indent(); 716 PrintRawSEHFinallyStmt(Node); 717 OS << NL; 718 } 719 720 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) { 721 Indent() << "__leave;"; 722 if (Policy.IncludeNewlines) OS << NL; 723 } 724 725 //===----------------------------------------------------------------------===// 726 // OpenMP directives printing methods 727 //===----------------------------------------------------------------------===// 728 729 void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop *Node) { 730 PrintStmt(Node->getLoopStmt()); 731 } 732 733 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S, 734 bool ForceNoStmt) { 735 OMPClausePrinter Printer(OS, Policy); 736 ArrayRef<OMPClause *> Clauses = S->clauses(); 737 for (auto *Clause : Clauses) 738 if (Clause && !Clause->isImplicit()) { 739 OS << ' '; 740 Printer.Visit(Clause); 741 } 742 OS << NL; 743 if (!ForceNoStmt && S->hasAssociatedStmt()) 744 PrintStmt(S->getRawStmt()); 745 } 746 747 void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective *Node) { 748 Indent() << "#pragma omp metadirective"; 749 PrintOMPExecutableDirective(Node); 750 } 751 752 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) { 753 Indent() << "#pragma omp parallel"; 754 PrintOMPExecutableDirective(Node); 755 } 756 757 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) { 758 Indent() << "#pragma omp simd"; 759 PrintOMPExecutableDirective(Node); 760 } 761 762 void StmtPrinter::VisitOMPTileDirective(OMPTileDirective *Node) { 763 Indent() << "#pragma omp tile"; 764 PrintOMPExecutableDirective(Node); 765 } 766 767 void StmtPrinter::VisitOMPUnrollDirective(OMPUnrollDirective *Node) { 768 Indent() << "#pragma omp unroll"; 769 PrintOMPExecutableDirective(Node); 770 } 771 772 void StmtPrinter::VisitOMPReverseDirective(OMPReverseDirective *Node) { 773 Indent() << "#pragma omp reverse"; 774 PrintOMPExecutableDirective(Node); 775 } 776 777 void StmtPrinter::VisitOMPInterchangeDirective(OMPInterchangeDirective *Node) { 778 Indent() << "#pragma omp interchange"; 779 PrintOMPExecutableDirective(Node); 780 } 781 782 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) { 783 Indent() << "#pragma omp for"; 784 PrintOMPExecutableDirective(Node); 785 } 786 787 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) { 788 Indent() << "#pragma omp for simd"; 789 PrintOMPExecutableDirective(Node); 790 } 791 792 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) { 793 Indent() << "#pragma omp sections"; 794 PrintOMPExecutableDirective(Node); 795 } 796 797 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) { 798 Indent() << "#pragma omp section"; 799 PrintOMPExecutableDirective(Node); 800 } 801 802 void StmtPrinter::VisitOMPScopeDirective(OMPScopeDirective *Node) { 803 Indent() << "#pragma omp scope"; 804 PrintOMPExecutableDirective(Node); 805 } 806 807 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) { 808 Indent() << "#pragma omp single"; 809 PrintOMPExecutableDirective(Node); 810 } 811 812 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) { 813 Indent() << "#pragma omp master"; 814 PrintOMPExecutableDirective(Node); 815 } 816 817 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) { 818 Indent() << "#pragma omp critical"; 819 if (Node->getDirectiveName().getName()) { 820 OS << " ("; 821 Node->getDirectiveName().printName(OS, Policy); 822 OS << ")"; 823 } 824 PrintOMPExecutableDirective(Node); 825 } 826 827 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) { 828 Indent() << "#pragma omp parallel for"; 829 PrintOMPExecutableDirective(Node); 830 } 831 832 void StmtPrinter::VisitOMPParallelForSimdDirective( 833 OMPParallelForSimdDirective *Node) { 834 Indent() << "#pragma omp parallel for simd"; 835 PrintOMPExecutableDirective(Node); 836 } 837 838 void StmtPrinter::VisitOMPParallelMasterDirective( 839 OMPParallelMasterDirective *Node) { 840 Indent() << "#pragma omp parallel master"; 841 PrintOMPExecutableDirective(Node); 842 } 843 844 void StmtPrinter::VisitOMPParallelMaskedDirective( 845 OMPParallelMaskedDirective *Node) { 846 Indent() << "#pragma omp parallel masked"; 847 PrintOMPExecutableDirective(Node); 848 } 849 850 void StmtPrinter::VisitOMPParallelSectionsDirective( 851 OMPParallelSectionsDirective *Node) { 852 Indent() << "#pragma omp parallel sections"; 853 PrintOMPExecutableDirective(Node); 854 } 855 856 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) { 857 Indent() << "#pragma omp task"; 858 PrintOMPExecutableDirective(Node); 859 } 860 861 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) { 862 Indent() << "#pragma omp taskyield"; 863 PrintOMPExecutableDirective(Node); 864 } 865 866 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) { 867 Indent() << "#pragma omp barrier"; 868 PrintOMPExecutableDirective(Node); 869 } 870 871 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) { 872 Indent() << "#pragma omp taskwait"; 873 PrintOMPExecutableDirective(Node); 874 } 875 876 void StmtPrinter::VisitOMPAssumeDirective(OMPAssumeDirective *Node) { 877 Indent() << "#pragma omp assume"; 878 PrintOMPExecutableDirective(Node); 879 } 880 881 void StmtPrinter::VisitOMPErrorDirective(OMPErrorDirective *Node) { 882 Indent() << "#pragma omp error"; 883 PrintOMPExecutableDirective(Node); 884 } 885 886 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) { 887 Indent() << "#pragma omp taskgroup"; 888 PrintOMPExecutableDirective(Node); 889 } 890 891 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) { 892 Indent() << "#pragma omp flush"; 893 PrintOMPExecutableDirective(Node); 894 } 895 896 void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) { 897 Indent() << "#pragma omp depobj"; 898 PrintOMPExecutableDirective(Node); 899 } 900 901 void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) { 902 Indent() << "#pragma omp scan"; 903 PrintOMPExecutableDirective(Node); 904 } 905 906 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) { 907 Indent() << "#pragma omp ordered"; 908 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>()); 909 } 910 911 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) { 912 Indent() << "#pragma omp atomic"; 913 PrintOMPExecutableDirective(Node); 914 } 915 916 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) { 917 Indent() << "#pragma omp target"; 918 PrintOMPExecutableDirective(Node); 919 } 920 921 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) { 922 Indent() << "#pragma omp target data"; 923 PrintOMPExecutableDirective(Node); 924 } 925 926 void StmtPrinter::VisitOMPTargetEnterDataDirective( 927 OMPTargetEnterDataDirective *Node) { 928 Indent() << "#pragma omp target enter data"; 929 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true); 930 } 931 932 void StmtPrinter::VisitOMPTargetExitDataDirective( 933 OMPTargetExitDataDirective *Node) { 934 Indent() << "#pragma omp target exit data"; 935 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true); 936 } 937 938 void StmtPrinter::VisitOMPTargetParallelDirective( 939 OMPTargetParallelDirective *Node) { 940 Indent() << "#pragma omp target parallel"; 941 PrintOMPExecutableDirective(Node); 942 } 943 944 void StmtPrinter::VisitOMPTargetParallelForDirective( 945 OMPTargetParallelForDirective *Node) { 946 Indent() << "#pragma omp target parallel for"; 947 PrintOMPExecutableDirective(Node); 948 } 949 950 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) { 951 Indent() << "#pragma omp teams"; 952 PrintOMPExecutableDirective(Node); 953 } 954 955 void StmtPrinter::VisitOMPCancellationPointDirective( 956 OMPCancellationPointDirective *Node) { 957 Indent() << "#pragma omp cancellation point " 958 << getOpenMPDirectiveName(Node->getCancelRegion()); 959 PrintOMPExecutableDirective(Node); 960 } 961 962 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) { 963 Indent() << "#pragma omp cancel " 964 << getOpenMPDirectiveName(Node->getCancelRegion()); 965 PrintOMPExecutableDirective(Node); 966 } 967 968 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) { 969 Indent() << "#pragma omp taskloop"; 970 PrintOMPExecutableDirective(Node); 971 } 972 973 void StmtPrinter::VisitOMPTaskLoopSimdDirective( 974 OMPTaskLoopSimdDirective *Node) { 975 Indent() << "#pragma omp taskloop simd"; 976 PrintOMPExecutableDirective(Node); 977 } 978 979 void StmtPrinter::VisitOMPMasterTaskLoopDirective( 980 OMPMasterTaskLoopDirective *Node) { 981 Indent() << "#pragma omp master taskloop"; 982 PrintOMPExecutableDirective(Node); 983 } 984 985 void StmtPrinter::VisitOMPMaskedTaskLoopDirective( 986 OMPMaskedTaskLoopDirective *Node) { 987 Indent() << "#pragma omp masked taskloop"; 988 PrintOMPExecutableDirective(Node); 989 } 990 991 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective( 992 OMPMasterTaskLoopSimdDirective *Node) { 993 Indent() << "#pragma omp master taskloop simd"; 994 PrintOMPExecutableDirective(Node); 995 } 996 997 void StmtPrinter::VisitOMPMaskedTaskLoopSimdDirective( 998 OMPMaskedTaskLoopSimdDirective *Node) { 999 Indent() << "#pragma omp masked taskloop simd"; 1000 PrintOMPExecutableDirective(Node); 1001 } 1002 1003 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective( 1004 OMPParallelMasterTaskLoopDirective *Node) { 1005 Indent() << "#pragma omp parallel master taskloop"; 1006 PrintOMPExecutableDirective(Node); 1007 } 1008 1009 void StmtPrinter::VisitOMPParallelMaskedTaskLoopDirective( 1010 OMPParallelMaskedTaskLoopDirective *Node) { 1011 Indent() << "#pragma omp parallel masked taskloop"; 1012 PrintOMPExecutableDirective(Node); 1013 } 1014 1015 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective( 1016 OMPParallelMasterTaskLoopSimdDirective *Node) { 1017 Indent() << "#pragma omp parallel master taskloop simd"; 1018 PrintOMPExecutableDirective(Node); 1019 } 1020 1021 void StmtPrinter::VisitOMPParallelMaskedTaskLoopSimdDirective( 1022 OMPParallelMaskedTaskLoopSimdDirective *Node) { 1023 Indent() << "#pragma omp parallel masked taskloop simd"; 1024 PrintOMPExecutableDirective(Node); 1025 } 1026 1027 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) { 1028 Indent() << "#pragma omp distribute"; 1029 PrintOMPExecutableDirective(Node); 1030 } 1031 1032 void StmtPrinter::VisitOMPTargetUpdateDirective( 1033 OMPTargetUpdateDirective *Node) { 1034 Indent() << "#pragma omp target update"; 1035 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true); 1036 } 1037 1038 void StmtPrinter::VisitOMPDistributeParallelForDirective( 1039 OMPDistributeParallelForDirective *Node) { 1040 Indent() << "#pragma omp distribute parallel for"; 1041 PrintOMPExecutableDirective(Node); 1042 } 1043 1044 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective( 1045 OMPDistributeParallelForSimdDirective *Node) { 1046 Indent() << "#pragma omp distribute parallel for simd"; 1047 PrintOMPExecutableDirective(Node); 1048 } 1049 1050 void StmtPrinter::VisitOMPDistributeSimdDirective( 1051 OMPDistributeSimdDirective *Node) { 1052 Indent() << "#pragma omp distribute simd"; 1053 PrintOMPExecutableDirective(Node); 1054 } 1055 1056 void StmtPrinter::VisitOMPTargetParallelForSimdDirective( 1057 OMPTargetParallelForSimdDirective *Node) { 1058 Indent() << "#pragma omp target parallel for simd"; 1059 PrintOMPExecutableDirective(Node); 1060 } 1061 1062 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) { 1063 Indent() << "#pragma omp target simd"; 1064 PrintOMPExecutableDirective(Node); 1065 } 1066 1067 void StmtPrinter::VisitOMPTeamsDistributeDirective( 1068 OMPTeamsDistributeDirective *Node) { 1069 Indent() << "#pragma omp teams distribute"; 1070 PrintOMPExecutableDirective(Node); 1071 } 1072 1073 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective( 1074 OMPTeamsDistributeSimdDirective *Node) { 1075 Indent() << "#pragma omp teams distribute simd"; 1076 PrintOMPExecutableDirective(Node); 1077 } 1078 1079 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective( 1080 OMPTeamsDistributeParallelForSimdDirective *Node) { 1081 Indent() << "#pragma omp teams distribute parallel for simd"; 1082 PrintOMPExecutableDirective(Node); 1083 } 1084 1085 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective( 1086 OMPTeamsDistributeParallelForDirective *Node) { 1087 Indent() << "#pragma omp teams distribute parallel for"; 1088 PrintOMPExecutableDirective(Node); 1089 } 1090 1091 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) { 1092 Indent() << "#pragma omp target teams"; 1093 PrintOMPExecutableDirective(Node); 1094 } 1095 1096 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective( 1097 OMPTargetTeamsDistributeDirective *Node) { 1098 Indent() << "#pragma omp target teams distribute"; 1099 PrintOMPExecutableDirective(Node); 1100 } 1101 1102 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective( 1103 OMPTargetTeamsDistributeParallelForDirective *Node) { 1104 Indent() << "#pragma omp target teams distribute parallel for"; 1105 PrintOMPExecutableDirective(Node); 1106 } 1107 1108 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective( 1109 OMPTargetTeamsDistributeParallelForSimdDirective *Node) { 1110 Indent() << "#pragma omp target teams distribute parallel for simd"; 1111 PrintOMPExecutableDirective(Node); 1112 } 1113 1114 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective( 1115 OMPTargetTeamsDistributeSimdDirective *Node) { 1116 Indent() << "#pragma omp target teams distribute simd"; 1117 PrintOMPExecutableDirective(Node); 1118 } 1119 1120 void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) { 1121 Indent() << "#pragma omp interop"; 1122 PrintOMPExecutableDirective(Node); 1123 } 1124 1125 void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) { 1126 Indent() << "#pragma omp dispatch"; 1127 PrintOMPExecutableDirective(Node); 1128 } 1129 1130 void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) { 1131 Indent() << "#pragma omp masked"; 1132 PrintOMPExecutableDirective(Node); 1133 } 1134 1135 void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) { 1136 Indent() << "#pragma omp loop"; 1137 PrintOMPExecutableDirective(Node); 1138 } 1139 1140 void StmtPrinter::VisitOMPTeamsGenericLoopDirective( 1141 OMPTeamsGenericLoopDirective *Node) { 1142 Indent() << "#pragma omp teams loop"; 1143 PrintOMPExecutableDirective(Node); 1144 } 1145 1146 void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective( 1147 OMPTargetTeamsGenericLoopDirective *Node) { 1148 Indent() << "#pragma omp target teams loop"; 1149 PrintOMPExecutableDirective(Node); 1150 } 1151 1152 void StmtPrinter::VisitOMPParallelGenericLoopDirective( 1153 OMPParallelGenericLoopDirective *Node) { 1154 Indent() << "#pragma omp parallel loop"; 1155 PrintOMPExecutableDirective(Node); 1156 } 1157 1158 void StmtPrinter::VisitOMPTargetParallelGenericLoopDirective( 1159 OMPTargetParallelGenericLoopDirective *Node) { 1160 Indent() << "#pragma omp target parallel loop"; 1161 PrintOMPExecutableDirective(Node); 1162 } 1163 1164 //===----------------------------------------------------------------------===// 1165 // OpenACC construct printing methods 1166 //===----------------------------------------------------------------------===// 1167 void StmtPrinter::PrintOpenACCClauseList(OpenACCConstructStmt *S) { 1168 if (!S->clauses().empty()) { 1169 OS << ' '; 1170 OpenACCClausePrinter Printer(OS, Policy); 1171 Printer.VisitClauseList(S->clauses()); 1172 } 1173 } 1174 void StmtPrinter::PrintOpenACCConstruct(OpenACCConstructStmt *S) { 1175 Indent() << "#pragma acc " << S->getDirectiveKind(); 1176 PrintOpenACCClauseList(S); 1177 OS << '\n'; 1178 } 1179 void StmtPrinter::VisitOpenACCComputeConstruct(OpenACCComputeConstruct *S) { 1180 PrintOpenACCConstruct(S); 1181 PrintStmt(S->getStructuredBlock()); 1182 } 1183 1184 void StmtPrinter::VisitOpenACCLoopConstruct(OpenACCLoopConstruct *S) { 1185 PrintOpenACCConstruct(S); 1186 PrintStmt(S->getLoop()); 1187 } 1188 1189 void StmtPrinter::VisitOpenACCCombinedConstruct(OpenACCCombinedConstruct *S) { 1190 PrintOpenACCConstruct(S); 1191 PrintStmt(S->getLoop()); 1192 } 1193 1194 void StmtPrinter::VisitOpenACCDataConstruct(OpenACCDataConstruct *S) { 1195 PrintOpenACCConstruct(S); 1196 PrintStmt(S->getStructuredBlock()); 1197 } 1198 void StmtPrinter::VisitOpenACCHostDataConstruct(OpenACCHostDataConstruct *S) { 1199 PrintOpenACCConstruct(S); 1200 PrintStmt(S->getStructuredBlock()); 1201 } 1202 void StmtPrinter::VisitOpenACCEnterDataConstruct(OpenACCEnterDataConstruct *S) { 1203 PrintOpenACCConstruct(S); 1204 } 1205 void StmtPrinter::VisitOpenACCExitDataConstruct(OpenACCExitDataConstruct *S) { 1206 PrintOpenACCConstruct(S); 1207 } 1208 void StmtPrinter::VisitOpenACCInitConstruct(OpenACCInitConstruct *S) { 1209 PrintOpenACCConstruct(S); 1210 } 1211 void StmtPrinter::VisitOpenACCShutdownConstruct(OpenACCShutdownConstruct *S) { 1212 PrintOpenACCConstruct(S); 1213 } 1214 void StmtPrinter::VisitOpenACCSetConstruct(OpenACCSetConstruct *S) { 1215 PrintOpenACCConstruct(S); 1216 } 1217 void StmtPrinter::VisitOpenACCUpdateConstruct(OpenACCUpdateConstruct *S) { 1218 PrintOpenACCConstruct(S); 1219 } 1220 1221 void StmtPrinter::VisitOpenACCWaitConstruct(OpenACCWaitConstruct *S) { 1222 Indent() << "#pragma acc wait"; 1223 if (!S->getLParenLoc().isInvalid()) { 1224 OS << "("; 1225 if (S->hasDevNumExpr()) { 1226 OS << "devnum: "; 1227 S->getDevNumExpr()->printPretty(OS, nullptr, Policy); 1228 OS << " : "; 1229 } 1230 1231 if (S->hasQueuesTag()) 1232 OS << "queues: "; 1233 1234 llvm::interleaveComma(S->getQueueIdExprs(), OS, [&](const Expr *E) { 1235 E->printPretty(OS, nullptr, Policy); 1236 }); 1237 1238 OS << ")"; 1239 } 1240 1241 PrintOpenACCClauseList(S); 1242 OS << '\n'; 1243 } 1244 1245 //===----------------------------------------------------------------------===// 1246 // Expr printing methods. 1247 //===----------------------------------------------------------------------===// 1248 1249 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) { 1250 OS << Node->getBuiltinStr() << "()"; 1251 } 1252 1253 void StmtPrinter::VisitEmbedExpr(EmbedExpr *Node) { 1254 llvm::report_fatal_error("Not implemented"); 1255 } 1256 1257 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) { 1258 PrintExpr(Node->getSubExpr()); 1259 } 1260 1261 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 1262 ValueDecl *VD = Node->getDecl(); 1263 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(VD)) { 1264 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy); 1265 return; 1266 } 1267 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(VD)) { 1268 TPOD->printAsExpr(OS, Policy); 1269 return; 1270 } 1271 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1272 Qualifier->print(OS, Policy); 1273 if (Node->hasTemplateKeyword()) 1274 OS << "template "; 1275 DeclarationNameInfo NameInfo = Node->getNameInfo(); 1276 if (IdentifierInfo *ID = NameInfo.getName().getAsIdentifierInfo(); 1277 ID || NameInfo.getName().getNameKind() != DeclarationName::Identifier) { 1278 if (Policy.CleanUglifiedParameters && 1279 isa<ParmVarDecl, NonTypeTemplateParmDecl>(VD) && ID) 1280 OS << ID->deuglifiedName(); 1281 else 1282 NameInfo.printName(OS, Policy); 1283 } else { 1284 switch (VD->getKind()) { 1285 case Decl::NonTypeTemplateParm: { 1286 auto *TD = cast<NonTypeTemplateParmDecl>(VD); 1287 OS << "value-parameter-" << TD->getDepth() << '-' << TD->getIndex() << ""; 1288 break; 1289 } 1290 case Decl::ParmVar: { 1291 auto *PD = cast<ParmVarDecl>(VD); 1292 OS << "function-parameter-" << PD->getFunctionScopeDepth() << '-' 1293 << PD->getFunctionScopeIndex(); 1294 break; 1295 } 1296 default: 1297 llvm_unreachable("Unhandled anonymous declaration kind"); 1298 } 1299 } 1300 if (Node->hasExplicitTemplateArgs()) { 1301 const TemplateParameterList *TPL = nullptr; 1302 if (!Node->hadMultipleCandidates()) 1303 if (auto *TD = dyn_cast<TemplateDecl>(VD)) 1304 TPL = TD->getTemplateParameters(); 1305 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL); 1306 } 1307 } 1308 1309 void StmtPrinter::VisitDependentScopeDeclRefExpr( 1310 DependentScopeDeclRefExpr *Node) { 1311 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1312 Qualifier->print(OS, Policy); 1313 if (Node->hasTemplateKeyword()) 1314 OS << "template "; 1315 OS << Node->getNameInfo(); 1316 if (Node->hasExplicitTemplateArgs()) 1317 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 1318 } 1319 1320 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 1321 if (Node->getQualifier()) 1322 Node->getQualifier()->print(OS, Policy); 1323 if (Node->hasTemplateKeyword()) 1324 OS << "template "; 1325 OS << Node->getNameInfo(); 1326 if (Node->hasExplicitTemplateArgs()) 1327 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 1328 } 1329 1330 static bool isImplicitSelf(const Expr *E) { 1331 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 1332 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) { 1333 if (PD->getParameterKind() == ImplicitParamKind::ObjCSelf && 1334 DRE->getBeginLoc().isInvalid()) 1335 return true; 1336 } 1337 } 1338 return false; 1339 } 1340 1341 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 1342 if (Node->getBase()) { 1343 if (!Policy.SuppressImplicitBase || 1344 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) { 1345 PrintExpr(Node->getBase()); 1346 OS << (Node->isArrow() ? "->" : "."); 1347 } 1348 } 1349 OS << *Node->getDecl(); 1350 } 1351 1352 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 1353 if (Node->isSuperReceiver()) 1354 OS << "super."; 1355 else if (Node->isObjectReceiver() && Node->getBase()) { 1356 PrintExpr(Node->getBase()); 1357 OS << "."; 1358 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 1359 OS << Node->getClassReceiver()->getName() << "."; 1360 } 1361 1362 if (Node->isImplicitProperty()) { 1363 if (const auto *Getter = Node->getImplicitPropertyGetter()) 1364 Getter->getSelector().print(OS); 1365 else 1366 OS << SelectorTable::getPropertyNameFromSetterSelector( 1367 Node->getImplicitPropertySetter()->getSelector()); 1368 } else 1369 OS << Node->getExplicitProperty()->getName(); 1370 } 1371 1372 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 1373 PrintExpr(Node->getBaseExpr()); 1374 OS << "["; 1375 PrintExpr(Node->getKeyExpr()); 1376 OS << "]"; 1377 } 1378 1379 void StmtPrinter::VisitSYCLUniqueStableNameExpr( 1380 SYCLUniqueStableNameExpr *Node) { 1381 OS << "__builtin_sycl_unique_stable_name("; 1382 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1383 OS << ")"; 1384 } 1385 1386 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 1387 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind()); 1388 } 1389 1390 void StmtPrinter::VisitOpenACCAsteriskSizeExpr(OpenACCAsteriskSizeExpr *Node) { 1391 OS << '*'; 1392 } 1393 1394 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 1395 CharacterLiteral::print(Node->getValue(), Node->getKind(), OS); 1396 } 1397 1398 /// Prints the given expression using the original source text. Returns true on 1399 /// success, false otherwise. 1400 static bool printExprAsWritten(raw_ostream &OS, Expr *E, 1401 const ASTContext *Context) { 1402 if (!Context) 1403 return false; 1404 bool Invalid = false; 1405 StringRef Source = Lexer::getSourceText( 1406 CharSourceRange::getTokenRange(E->getSourceRange()), 1407 Context->getSourceManager(), Context->getLangOpts(), &Invalid); 1408 if (!Invalid) { 1409 OS << Source; 1410 return true; 1411 } 1412 return false; 1413 } 1414 1415 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 1416 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context)) 1417 return; 1418 bool isSigned = Node->getType()->isSignedIntegerType(); 1419 OS << toString(Node->getValue(), 10, isSigned); 1420 1421 if (isa<BitIntType>(Node->getType())) { 1422 OS << (isSigned ? "wb" : "uwb"); 1423 return; 1424 } 1425 1426 // Emit suffixes. Integer literals are always a builtin integer type. 1427 switch (Node->getType()->castAs<BuiltinType>()->getKind()) { 1428 default: llvm_unreachable("Unexpected type for integer literal!"); 1429 case BuiltinType::Char_S: 1430 case BuiltinType::Char_U: OS << "i8"; break; 1431 case BuiltinType::UChar: OS << "Ui8"; break; 1432 case BuiltinType::SChar: OS << "i8"; break; 1433 case BuiltinType::Short: OS << "i16"; break; 1434 case BuiltinType::UShort: OS << "Ui16"; break; 1435 case BuiltinType::Int: break; // no suffix. 1436 case BuiltinType::UInt: OS << 'U'; break; 1437 case BuiltinType::Long: OS << 'L'; break; 1438 case BuiltinType::ULong: OS << "UL"; break; 1439 case BuiltinType::LongLong: OS << "LL"; break; 1440 case BuiltinType::ULongLong: OS << "ULL"; break; 1441 case BuiltinType::Int128: 1442 break; // no suffix. 1443 case BuiltinType::UInt128: 1444 break; // no suffix. 1445 case BuiltinType::WChar_S: 1446 case BuiltinType::WChar_U: 1447 break; // no suffix 1448 } 1449 } 1450 1451 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) { 1452 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context)) 1453 return; 1454 OS << Node->getValueAsString(/*Radix=*/10); 1455 1456 switch (Node->getType()->castAs<BuiltinType>()->getKind()) { 1457 default: llvm_unreachable("Unexpected type for fixed point literal!"); 1458 case BuiltinType::ShortFract: OS << "hr"; break; 1459 case BuiltinType::ShortAccum: OS << "hk"; break; 1460 case BuiltinType::UShortFract: OS << "uhr"; break; 1461 case BuiltinType::UShortAccum: OS << "uhk"; break; 1462 case BuiltinType::Fract: OS << "r"; break; 1463 case BuiltinType::Accum: OS << "k"; break; 1464 case BuiltinType::UFract: OS << "ur"; break; 1465 case BuiltinType::UAccum: OS << "uk"; break; 1466 case BuiltinType::LongFract: OS << "lr"; break; 1467 case BuiltinType::LongAccum: OS << "lk"; break; 1468 case BuiltinType::ULongFract: OS << "ulr"; break; 1469 case BuiltinType::ULongAccum: OS << "ulk"; break; 1470 } 1471 } 1472 1473 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 1474 bool PrintSuffix) { 1475 SmallString<16> Str; 1476 Node->getValue().toString(Str); 1477 OS << Str; 1478 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 1479 OS << '.'; // Trailing dot in order to separate from ints. 1480 1481 if (!PrintSuffix) 1482 return; 1483 1484 // Emit suffixes. Float literals are always a builtin float type. 1485 switch (Node->getType()->castAs<BuiltinType>()->getKind()) { 1486 default: llvm_unreachable("Unexpected type for float literal!"); 1487 case BuiltinType::Half: break; // FIXME: suffix? 1488 case BuiltinType::Ibm128: break; // FIXME: No suffix for ibm128 literal 1489 case BuiltinType::Double: break; // no suffix. 1490 case BuiltinType::Float16: OS << "F16"; break; 1491 case BuiltinType::Float: OS << 'F'; break; 1492 case BuiltinType::LongDouble: OS << 'L'; break; 1493 case BuiltinType::Float128: OS << 'Q'; break; 1494 } 1495 } 1496 1497 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 1498 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context)) 1499 return; 1500 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 1501 } 1502 1503 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 1504 PrintExpr(Node->getSubExpr()); 1505 OS << "i"; 1506 } 1507 1508 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 1509 Str->outputString(OS); 1510 } 1511 1512 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 1513 OS << "("; 1514 PrintExpr(Node->getSubExpr()); 1515 OS << ")"; 1516 } 1517 1518 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1519 if (!Node->isPostfix()) { 1520 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1521 1522 // Print a space if this is an "identifier operator" like __real, or if 1523 // it might be concatenated incorrectly like '+'. 1524 switch (Node->getOpcode()) { 1525 default: break; 1526 case UO_Real: 1527 case UO_Imag: 1528 case UO_Extension: 1529 OS << ' '; 1530 break; 1531 case UO_Plus: 1532 case UO_Minus: 1533 if (isa<UnaryOperator>(Node->getSubExpr())) 1534 OS << ' '; 1535 break; 1536 } 1537 } 1538 PrintExpr(Node->getSubExpr()); 1539 1540 if (Node->isPostfix()) 1541 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1542 } 1543 1544 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1545 OS << "__builtin_offsetof("; 1546 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1547 OS << ", "; 1548 bool PrintedSomething = false; 1549 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1550 OffsetOfNode ON = Node->getComponent(i); 1551 if (ON.getKind() == OffsetOfNode::Array) { 1552 // Array node 1553 OS << "["; 1554 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1555 OS << "]"; 1556 PrintedSomething = true; 1557 continue; 1558 } 1559 1560 // Skip implicit base indirections. 1561 if (ON.getKind() == OffsetOfNode::Base) 1562 continue; 1563 1564 // Field or identifier node. 1565 const IdentifierInfo *Id = ON.getFieldName(); 1566 if (!Id) 1567 continue; 1568 1569 if (PrintedSomething) 1570 OS << "."; 1571 else 1572 PrintedSomething = true; 1573 OS << Id->getName(); 1574 } 1575 OS << ")"; 1576 } 1577 1578 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr( 1579 UnaryExprOrTypeTraitExpr *Node) { 1580 const char *Spelling = getTraitSpelling(Node->getKind()); 1581 if (Node->getKind() == UETT_AlignOf) { 1582 if (Policy.Alignof) 1583 Spelling = "alignof"; 1584 else if (Policy.UnderscoreAlignof) 1585 Spelling = "_Alignof"; 1586 else 1587 Spelling = "__alignof"; 1588 } 1589 1590 OS << Spelling; 1591 1592 if (Node->isArgumentType()) { 1593 OS << '('; 1594 Node->getArgumentType().print(OS, Policy); 1595 OS << ')'; 1596 } else { 1597 OS << " "; 1598 PrintExpr(Node->getArgumentExpr()); 1599 } 1600 } 1601 1602 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1603 OS << "_Generic("; 1604 if (Node->isExprPredicate()) 1605 PrintExpr(Node->getControllingExpr()); 1606 else 1607 Node->getControllingType()->getType().print(OS, Policy); 1608 1609 for (const GenericSelectionExpr::Association &Assoc : Node->associations()) { 1610 OS << ", "; 1611 QualType T = Assoc.getType(); 1612 if (T.isNull()) 1613 OS << "default"; 1614 else 1615 T.print(OS, Policy); 1616 OS << ": "; 1617 PrintExpr(Assoc.getAssociationExpr()); 1618 } 1619 OS << ")"; 1620 } 1621 1622 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1623 PrintExpr(Node->getLHS()); 1624 OS << "["; 1625 PrintExpr(Node->getRHS()); 1626 OS << "]"; 1627 } 1628 1629 void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) { 1630 PrintExpr(Node->getBase()); 1631 OS << "["; 1632 PrintExpr(Node->getRowIdx()); 1633 OS << "]"; 1634 OS << "["; 1635 PrintExpr(Node->getColumnIdx()); 1636 OS << "]"; 1637 } 1638 1639 void StmtPrinter::VisitArraySectionExpr(ArraySectionExpr *Node) { 1640 PrintExpr(Node->getBase()); 1641 OS << "["; 1642 if (Node->getLowerBound()) 1643 PrintExpr(Node->getLowerBound()); 1644 if (Node->getColonLocFirst().isValid()) { 1645 OS << ":"; 1646 if (Node->getLength()) 1647 PrintExpr(Node->getLength()); 1648 } 1649 if (Node->isOMPArraySection() && Node->getColonLocSecond().isValid()) { 1650 OS << ":"; 1651 if (Node->getStride()) 1652 PrintExpr(Node->getStride()); 1653 } 1654 OS << "]"; 1655 } 1656 1657 void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) { 1658 OS << "("; 1659 for (Expr *E : Node->getDimensions()) { 1660 OS << "["; 1661 PrintExpr(E); 1662 OS << "]"; 1663 } 1664 OS << ")"; 1665 PrintExpr(Node->getBase()); 1666 } 1667 1668 void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) { 1669 OS << "iterator("; 1670 for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) { 1671 auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I)); 1672 VD->getType().print(OS, Policy); 1673 const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I); 1674 OS << " " << VD->getName() << " = "; 1675 PrintExpr(Range.Begin); 1676 OS << ":"; 1677 PrintExpr(Range.End); 1678 if (Range.Step) { 1679 OS << ":"; 1680 PrintExpr(Range.Step); 1681 } 1682 if (I < E - 1) 1683 OS << ", "; 1684 } 1685 OS << ")"; 1686 } 1687 1688 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1689 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1690 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1691 // Don't print any defaulted arguments 1692 break; 1693 } 1694 1695 if (i) OS << ", "; 1696 PrintExpr(Call->getArg(i)); 1697 } 1698 } 1699 1700 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1701 PrintExpr(Call->getCallee()); 1702 OS << "("; 1703 PrintCallArgs(Call); 1704 OS << ")"; 1705 } 1706 1707 static bool isImplicitThis(const Expr *E) { 1708 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) 1709 return TE->isImplicit(); 1710 return false; 1711 } 1712 1713 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1714 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) { 1715 PrintExpr(Node->getBase()); 1716 1717 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1718 FieldDecl *ParentDecl = 1719 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) 1720 : nullptr; 1721 1722 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1723 OS << (Node->isArrow() ? "->" : "."); 1724 } 1725 1726 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1727 if (FD->isAnonymousStructOrUnion()) 1728 return; 1729 1730 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1731 Qualifier->print(OS, Policy); 1732 if (Node->hasTemplateKeyword()) 1733 OS << "template "; 1734 OS << Node->getMemberNameInfo(); 1735 const TemplateParameterList *TPL = nullptr; 1736 if (auto *FD = dyn_cast<FunctionDecl>(Node->getMemberDecl())) { 1737 if (!Node->hadMultipleCandidates()) 1738 if (auto *FTD = FD->getPrimaryTemplate()) 1739 TPL = FTD->getTemplateParameters(); 1740 } else if (auto *VTSD = 1741 dyn_cast<VarTemplateSpecializationDecl>(Node->getMemberDecl())) 1742 TPL = VTSD->getSpecializedTemplate()->getTemplateParameters(); 1743 if (Node->hasExplicitTemplateArgs()) 1744 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL); 1745 } 1746 1747 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1748 PrintExpr(Node->getBase()); 1749 OS << (Node->isArrow() ? "->isa" : ".isa"); 1750 } 1751 1752 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1753 PrintExpr(Node->getBase()); 1754 OS << "."; 1755 OS << Node->getAccessor().getName(); 1756 } 1757 1758 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1759 OS << '('; 1760 Node->getTypeAsWritten().print(OS, Policy); 1761 OS << ')'; 1762 PrintExpr(Node->getSubExpr()); 1763 } 1764 1765 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1766 OS << '('; 1767 Node->getType().print(OS, Policy); 1768 OS << ')'; 1769 PrintExpr(Node->getInitializer()); 1770 } 1771 1772 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1773 // No need to print anything, simply forward to the subexpression. 1774 PrintExpr(Node->getSubExpr()); 1775 } 1776 1777 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1778 PrintExpr(Node->getLHS()); 1779 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1780 PrintExpr(Node->getRHS()); 1781 } 1782 1783 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1784 PrintExpr(Node->getLHS()); 1785 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1786 PrintExpr(Node->getRHS()); 1787 } 1788 1789 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1790 PrintExpr(Node->getCond()); 1791 OS << " ? "; 1792 PrintExpr(Node->getLHS()); 1793 OS << " : "; 1794 PrintExpr(Node->getRHS()); 1795 } 1796 1797 // GNU extensions. 1798 1799 void 1800 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1801 PrintExpr(Node->getCommon()); 1802 OS << " ?: "; 1803 PrintExpr(Node->getFalseExpr()); 1804 } 1805 1806 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1807 OS << "&&" << Node->getLabel()->getName(); 1808 } 1809 1810 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1811 OS << "("; 1812 PrintRawCompoundStmt(E->getSubStmt()); 1813 OS << ")"; 1814 } 1815 1816 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1817 OS << "__builtin_choose_expr("; 1818 PrintExpr(Node->getCond()); 1819 OS << ", "; 1820 PrintExpr(Node->getLHS()); 1821 OS << ", "; 1822 PrintExpr(Node->getRHS()); 1823 OS << ")"; 1824 } 1825 1826 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1827 OS << "__null"; 1828 } 1829 1830 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1831 OS << "__builtin_shufflevector("; 1832 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1833 if (i) OS << ", "; 1834 PrintExpr(Node->getExpr(i)); 1835 } 1836 OS << ")"; 1837 } 1838 1839 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1840 OS << "__builtin_convertvector("; 1841 PrintExpr(Node->getSrcExpr()); 1842 OS << ", "; 1843 Node->getType().print(OS, Policy); 1844 OS << ")"; 1845 } 1846 1847 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1848 if (Node->getSyntacticForm()) { 1849 Visit(Node->getSyntacticForm()); 1850 return; 1851 } 1852 1853 OS << "{"; 1854 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1855 if (i) OS << ", "; 1856 if (Node->getInit(i)) 1857 PrintExpr(Node->getInit(i)); 1858 else 1859 OS << "{}"; 1860 } 1861 OS << "}"; 1862 } 1863 1864 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) { 1865 // There's no way to express this expression in any of our supported 1866 // languages, so just emit something terse and (hopefully) clear. 1867 OS << "{"; 1868 PrintExpr(Node->getSubExpr()); 1869 OS << "}"; 1870 } 1871 1872 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) { 1873 OS << "*"; 1874 } 1875 1876 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1877 OS << "("; 1878 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1879 if (i) OS << ", "; 1880 PrintExpr(Node->getExpr(i)); 1881 } 1882 OS << ")"; 1883 } 1884 1885 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1886 bool NeedsEquals = true; 1887 for (const DesignatedInitExpr::Designator &D : Node->designators()) { 1888 if (D.isFieldDesignator()) { 1889 if (D.getDotLoc().isInvalid()) { 1890 if (const IdentifierInfo *II = D.getFieldName()) { 1891 OS << II->getName() << ":"; 1892 NeedsEquals = false; 1893 } 1894 } else { 1895 OS << "." << D.getFieldName()->getName(); 1896 } 1897 } else { 1898 OS << "["; 1899 if (D.isArrayDesignator()) { 1900 PrintExpr(Node->getArrayIndex(D)); 1901 } else { 1902 PrintExpr(Node->getArrayRangeStart(D)); 1903 OS << " ... "; 1904 PrintExpr(Node->getArrayRangeEnd(D)); 1905 } 1906 OS << "]"; 1907 } 1908 } 1909 1910 if (NeedsEquals) 1911 OS << " = "; 1912 else 1913 OS << " "; 1914 PrintExpr(Node->getInit()); 1915 } 1916 1917 void StmtPrinter::VisitDesignatedInitUpdateExpr( 1918 DesignatedInitUpdateExpr *Node) { 1919 OS << "{"; 1920 OS << "/*base*/"; 1921 PrintExpr(Node->getBase()); 1922 OS << ", "; 1923 1924 OS << "/*updater*/"; 1925 PrintExpr(Node->getUpdater()); 1926 OS << "}"; 1927 } 1928 1929 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) { 1930 OS << "/*no init*/"; 1931 } 1932 1933 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1934 if (Node->getType()->getAsCXXRecordDecl()) { 1935 OS << "/*implicit*/"; 1936 Node->getType().print(OS, Policy); 1937 OS << "()"; 1938 } else { 1939 OS << "/*implicit*/("; 1940 Node->getType().print(OS, Policy); 1941 OS << ')'; 1942 if (Node->getType()->isRecordType()) 1943 OS << "{}"; 1944 else 1945 OS << 0; 1946 } 1947 } 1948 1949 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1950 OS << "__builtin_va_arg("; 1951 PrintExpr(Node->getSubExpr()); 1952 OS << ", "; 1953 Node->getType().print(OS, Policy); 1954 OS << ")"; 1955 } 1956 1957 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1958 PrintExpr(Node->getSyntacticForm()); 1959 } 1960 1961 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1962 const char *Name = nullptr; 1963 switch (Node->getOp()) { 1964 #define BUILTIN(ID, TYPE, ATTRS) 1965 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1966 case AtomicExpr::AO ## ID: \ 1967 Name = #ID "("; \ 1968 break; 1969 #include "clang/Basic/Builtins.inc" 1970 } 1971 OS << Name; 1972 1973 // AtomicExpr stores its subexpressions in a permuted order. 1974 PrintExpr(Node->getPtr()); 1975 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1976 Node->getOp() != AtomicExpr::AO__atomic_load_n && 1977 Node->getOp() != AtomicExpr::AO__scoped_atomic_load_n && 1978 Node->getOp() != AtomicExpr::AO__opencl_atomic_load && 1979 Node->getOp() != AtomicExpr::AO__hip_atomic_load) { 1980 OS << ", "; 1981 PrintExpr(Node->getVal1()); 1982 } 1983 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1984 Node->isCmpXChg()) { 1985 OS << ", "; 1986 PrintExpr(Node->getVal2()); 1987 } 1988 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1989 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1990 OS << ", "; 1991 PrintExpr(Node->getWeak()); 1992 } 1993 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init && 1994 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) { 1995 OS << ", "; 1996 PrintExpr(Node->getOrder()); 1997 } 1998 if (Node->isCmpXChg()) { 1999 OS << ", "; 2000 PrintExpr(Node->getOrderFail()); 2001 } 2002 OS << ")"; 2003 } 2004 2005 // C++ 2006 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 2007 OverloadedOperatorKind Kind = Node->getOperator(); 2008 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 2009 if (Node->getNumArgs() == 1) { 2010 OS << getOperatorSpelling(Kind) << ' '; 2011 PrintExpr(Node->getArg(0)); 2012 } else { 2013 PrintExpr(Node->getArg(0)); 2014 OS << ' ' << getOperatorSpelling(Kind); 2015 } 2016 } else if (Kind == OO_Arrow) { 2017 PrintExpr(Node->getArg(0)); 2018 } else if (Kind == OO_Call || Kind == OO_Subscript) { 2019 PrintExpr(Node->getArg(0)); 2020 OS << (Kind == OO_Call ? '(' : '['); 2021 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 2022 if (ArgIdx > 1) 2023 OS << ", "; 2024 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 2025 PrintExpr(Node->getArg(ArgIdx)); 2026 } 2027 OS << (Kind == OO_Call ? ')' : ']'); 2028 } else if (Node->getNumArgs() == 1) { 2029 OS << getOperatorSpelling(Kind) << ' '; 2030 PrintExpr(Node->getArg(0)); 2031 } else if (Node->getNumArgs() == 2) { 2032 PrintExpr(Node->getArg(0)); 2033 OS << ' ' << getOperatorSpelling(Kind) << ' '; 2034 PrintExpr(Node->getArg(1)); 2035 } else { 2036 llvm_unreachable("unknown overloaded operator"); 2037 } 2038 } 2039 2040 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 2041 // If we have a conversion operator call only print the argument. 2042 CXXMethodDecl *MD = Node->getMethodDecl(); 2043 if (isa_and_nonnull<CXXConversionDecl>(MD)) { 2044 PrintExpr(Node->getImplicitObjectArgument()); 2045 return; 2046 } 2047 VisitCallExpr(cast<CallExpr>(Node)); 2048 } 2049 2050 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 2051 PrintExpr(Node->getCallee()); 2052 OS << "<<<"; 2053 PrintCallArgs(Node->getConfig()); 2054 OS << ">>>("; 2055 PrintCallArgs(Node); 2056 OS << ")"; 2057 } 2058 2059 void StmtPrinter::VisitCXXRewrittenBinaryOperator( 2060 CXXRewrittenBinaryOperator *Node) { 2061 CXXRewrittenBinaryOperator::DecomposedForm Decomposed = 2062 Node->getDecomposedForm(); 2063 PrintExpr(const_cast<Expr*>(Decomposed.LHS)); 2064 OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' '; 2065 PrintExpr(const_cast<Expr*>(Decomposed.RHS)); 2066 } 2067 2068 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 2069 OS << Node->getCastName() << '<'; 2070 Node->getTypeAsWritten().print(OS, Policy); 2071 OS << ">("; 2072 PrintExpr(Node->getSubExpr()); 2073 OS << ")"; 2074 } 2075 2076 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 2077 VisitCXXNamedCastExpr(Node); 2078 } 2079 2080 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 2081 VisitCXXNamedCastExpr(Node); 2082 } 2083 2084 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 2085 VisitCXXNamedCastExpr(Node); 2086 } 2087 2088 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 2089 VisitCXXNamedCastExpr(Node); 2090 } 2091 2092 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) { 2093 OS << "__builtin_bit_cast("; 2094 Node->getTypeInfoAsWritten()->getType().print(OS, Policy); 2095 OS << ", "; 2096 PrintExpr(Node->getSubExpr()); 2097 OS << ")"; 2098 } 2099 2100 void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) { 2101 VisitCXXNamedCastExpr(Node); 2102 } 2103 2104 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 2105 OS << "typeid("; 2106 if (Node->isTypeOperand()) { 2107 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 2108 } else { 2109 PrintExpr(Node->getExprOperand()); 2110 } 2111 OS << ")"; 2112 } 2113 2114 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 2115 OS << "__uuidof("; 2116 if (Node->isTypeOperand()) { 2117 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 2118 } else { 2119 PrintExpr(Node->getExprOperand()); 2120 } 2121 OS << ")"; 2122 } 2123 2124 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 2125 PrintExpr(Node->getBaseExpr()); 2126 if (Node->isArrow()) 2127 OS << "->"; 2128 else 2129 OS << "."; 2130 if (NestedNameSpecifier *Qualifier = 2131 Node->getQualifierLoc().getNestedNameSpecifier()) 2132 Qualifier->print(OS, Policy); 2133 OS << Node->getPropertyDecl()->getDeclName(); 2134 } 2135 2136 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) { 2137 PrintExpr(Node->getBase()); 2138 OS << "["; 2139 PrintExpr(Node->getIdx()); 2140 OS << "]"; 2141 } 2142 2143 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 2144 switch (Node->getLiteralOperatorKind()) { 2145 case UserDefinedLiteral::LOK_Raw: 2146 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 2147 break; 2148 case UserDefinedLiteral::LOK_Template: { 2149 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 2150 const TemplateArgumentList *Args = 2151 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 2152 assert(Args); 2153 2154 if (Args->size() != 1 || Args->get(0).getKind() != TemplateArgument::Pack) { 2155 const TemplateParameterList *TPL = nullptr; 2156 if (!DRE->hadMultipleCandidates()) 2157 if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl())) 2158 TPL = TD->getTemplateParameters(); 2159 OS << "operator\"\"" << Node->getUDSuffix()->getName(); 2160 printTemplateArgumentList(OS, Args->asArray(), Policy, TPL); 2161 OS << "()"; 2162 return; 2163 } 2164 2165 const TemplateArgument &Pack = Args->get(0); 2166 for (const auto &P : Pack.pack_elements()) { 2167 char C = (char)P.getAsIntegral().getZExtValue(); 2168 OS << C; 2169 } 2170 break; 2171 } 2172 case UserDefinedLiteral::LOK_Integer: { 2173 // Print integer literal without suffix. 2174 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 2175 OS << toString(Int->getValue(), 10, /*isSigned*/false); 2176 break; 2177 } 2178 case UserDefinedLiteral::LOK_Floating: { 2179 // Print floating literal without suffix. 2180 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 2181 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 2182 break; 2183 } 2184 case UserDefinedLiteral::LOK_String: 2185 case UserDefinedLiteral::LOK_Character: 2186 PrintExpr(Node->getCookedLiteral()); 2187 break; 2188 } 2189 OS << Node->getUDSuffix()->getName(); 2190 } 2191 2192 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 2193 OS << (Node->getValue() ? "true" : "false"); 2194 } 2195 2196 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 2197 OS << "nullptr"; 2198 } 2199 2200 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 2201 OS << "this"; 2202 } 2203 2204 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 2205 if (!Node->getSubExpr()) 2206 OS << "throw"; 2207 else { 2208 OS << "throw "; 2209 PrintExpr(Node->getSubExpr()); 2210 } 2211 } 2212 2213 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 2214 // Nothing to print: we picked up the default argument. 2215 } 2216 2217 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 2218 // Nothing to print: we picked up the default initializer. 2219 } 2220 2221 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 2222 auto TargetType = Node->getType(); 2223 auto *Auto = TargetType->getContainedDeducedType(); 2224 bool Bare = Auto && Auto->isDeduced(); 2225 2226 // Parenthesize deduced casts. 2227 if (Bare) 2228 OS << '('; 2229 TargetType.print(OS, Policy); 2230 if (Bare) 2231 OS << ')'; 2232 2233 // No extra braces surrounding the inner construct. 2234 if (!Node->isListInitialization()) 2235 OS << '('; 2236 PrintExpr(Node->getSubExpr()); 2237 if (!Node->isListInitialization()) 2238 OS << ')'; 2239 } 2240 2241 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 2242 PrintExpr(Node->getSubExpr()); 2243 } 2244 2245 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 2246 Node->getType().print(OS, Policy); 2247 if (Node->isStdInitListInitialization()) 2248 /* Nothing to do; braces are part of creating the std::initializer_list. */; 2249 else if (Node->isListInitialization()) 2250 OS << "{"; 2251 else 2252 OS << "("; 2253 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 2254 ArgEnd = Node->arg_end(); 2255 Arg != ArgEnd; ++Arg) { 2256 if ((*Arg)->isDefaultArgument()) 2257 break; 2258 if (Arg != Node->arg_begin()) 2259 OS << ", "; 2260 PrintExpr(*Arg); 2261 } 2262 if (Node->isStdInitListInitialization()) 2263 /* See above. */; 2264 else if (Node->isListInitialization()) 2265 OS << "}"; 2266 else 2267 OS << ")"; 2268 } 2269 2270 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 2271 OS << '['; 2272 bool NeedComma = false; 2273 switch (Node->getCaptureDefault()) { 2274 case LCD_None: 2275 break; 2276 2277 case LCD_ByCopy: 2278 OS << '='; 2279 NeedComma = true; 2280 break; 2281 2282 case LCD_ByRef: 2283 OS << '&'; 2284 NeedComma = true; 2285 break; 2286 } 2287 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 2288 CEnd = Node->explicit_capture_end(); 2289 C != CEnd; 2290 ++C) { 2291 if (C->capturesVLAType()) 2292 continue; 2293 2294 if (NeedComma) 2295 OS << ", "; 2296 NeedComma = true; 2297 2298 switch (C->getCaptureKind()) { 2299 case LCK_This: 2300 OS << "this"; 2301 break; 2302 2303 case LCK_StarThis: 2304 OS << "*this"; 2305 break; 2306 2307 case LCK_ByRef: 2308 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C)) 2309 OS << '&'; 2310 OS << C->getCapturedVar()->getName(); 2311 break; 2312 2313 case LCK_ByCopy: 2314 OS << C->getCapturedVar()->getName(); 2315 break; 2316 2317 case LCK_VLAType: 2318 llvm_unreachable("VLA type in explicit captures."); 2319 } 2320 2321 if (C->isPackExpansion()) 2322 OS << "..."; 2323 2324 if (Node->isInitCapture(C)) { 2325 // Init captures are always VarDecl. 2326 auto *D = cast<VarDecl>(C->getCapturedVar()); 2327 2328 llvm::StringRef Pre; 2329 llvm::StringRef Post; 2330 if (D->getInitStyle() == VarDecl::CallInit && 2331 !isa<ParenListExpr>(D->getInit())) { 2332 Pre = "("; 2333 Post = ")"; 2334 } else if (D->getInitStyle() == VarDecl::CInit) { 2335 Pre = " = "; 2336 } 2337 2338 OS << Pre; 2339 PrintExpr(D->getInit()); 2340 OS << Post; 2341 } 2342 } 2343 OS << ']'; 2344 2345 if (!Node->getExplicitTemplateParameters().empty()) { 2346 Node->getTemplateParameterList()->print( 2347 OS, Node->getLambdaClass()->getASTContext(), 2348 /*OmitTemplateKW*/true); 2349 } 2350 2351 if (Node->hasExplicitParameters()) { 2352 OS << '('; 2353 CXXMethodDecl *Method = Node->getCallOperator(); 2354 NeedComma = false; 2355 for (const auto *P : Method->parameters()) { 2356 if (NeedComma) { 2357 OS << ", "; 2358 } else { 2359 NeedComma = true; 2360 } 2361 std::string ParamStr = 2362 (Policy.CleanUglifiedParameters && P->getIdentifier()) 2363 ? P->getIdentifier()->deuglifiedName().str() 2364 : P->getNameAsString(); 2365 P->getOriginalType().print(OS, Policy, ParamStr); 2366 } 2367 if (Method->isVariadic()) { 2368 if (NeedComma) 2369 OS << ", "; 2370 OS << "..."; 2371 } 2372 OS << ')'; 2373 2374 if (Node->isMutable()) 2375 OS << " mutable"; 2376 2377 auto *Proto = Method->getType()->castAs<FunctionProtoType>(); 2378 Proto->printExceptionSpecification(OS, Policy); 2379 2380 // FIXME: Attributes 2381 2382 // Print the trailing return type if it was specified in the source. 2383 if (Node->hasExplicitResultType()) { 2384 OS << " -> "; 2385 Proto->getReturnType().print(OS, Policy); 2386 } 2387 } 2388 2389 // Print the body. 2390 OS << ' '; 2391 if (Policy.TerseOutput) 2392 OS << "{}"; 2393 else 2394 PrintRawCompoundStmt(Node->getCompoundStmtBody()); 2395 } 2396 2397 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 2398 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 2399 TSInfo->getType().print(OS, Policy); 2400 else 2401 Node->getType().print(OS, Policy); 2402 OS << "()"; 2403 } 2404 2405 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 2406 if (E->isGlobalNew()) 2407 OS << "::"; 2408 OS << "new "; 2409 unsigned NumPlace = E->getNumPlacementArgs(); 2410 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 2411 OS << "("; 2412 PrintExpr(E->getPlacementArg(0)); 2413 for (unsigned i = 1; i < NumPlace; ++i) { 2414 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 2415 break; 2416 OS << ", "; 2417 PrintExpr(E->getPlacementArg(i)); 2418 } 2419 OS << ") "; 2420 } 2421 if (E->isParenTypeId()) 2422 OS << "("; 2423 std::string TypeS; 2424 if (E->isArray()) { 2425 llvm::raw_string_ostream s(TypeS); 2426 s << '['; 2427 if (std::optional<Expr *> Size = E->getArraySize()) 2428 (*Size)->printPretty(s, Helper, Policy); 2429 s << ']'; 2430 } 2431 E->getAllocatedType().print(OS, Policy, TypeS); 2432 if (E->isParenTypeId()) 2433 OS << ")"; 2434 2435 CXXNewInitializationStyle InitStyle = E->getInitializationStyle(); 2436 if (InitStyle != CXXNewInitializationStyle::None) { 2437 bool Bare = InitStyle == CXXNewInitializationStyle::Parens && 2438 !isa<ParenListExpr>(E->getInitializer()); 2439 if (Bare) 2440 OS << "("; 2441 PrintExpr(E->getInitializer()); 2442 if (Bare) 2443 OS << ")"; 2444 } 2445 } 2446 2447 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 2448 if (E->isGlobalDelete()) 2449 OS << "::"; 2450 OS << "delete "; 2451 if (E->isArrayForm()) 2452 OS << "[] "; 2453 PrintExpr(E->getArgument()); 2454 } 2455 2456 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 2457 PrintExpr(E->getBase()); 2458 if (E->isArrow()) 2459 OS << "->"; 2460 else 2461 OS << '.'; 2462 if (E->getQualifier()) 2463 E->getQualifier()->print(OS, Policy); 2464 OS << "~"; 2465 2466 if (const IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 2467 OS << II->getName(); 2468 else 2469 E->getDestroyedType().print(OS, Policy); 2470 } 2471 2472 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 2473 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2474 OS << "{"; 2475 2476 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 2477 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 2478 // Don't print any defaulted arguments 2479 break; 2480 } 2481 2482 if (i) OS << ", "; 2483 PrintExpr(E->getArg(i)); 2484 } 2485 2486 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2487 OS << "}"; 2488 } 2489 2490 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) { 2491 // Parens are printed by the surrounding context. 2492 OS << "<forwarded>"; 2493 } 2494 2495 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 2496 PrintExpr(E->getSubExpr()); 2497 } 2498 2499 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 2500 // Just forward to the subexpression. 2501 PrintExpr(E->getSubExpr()); 2502 } 2503 2504 void StmtPrinter::VisitCXXUnresolvedConstructExpr( 2505 CXXUnresolvedConstructExpr *Node) { 2506 Node->getTypeAsWritten().print(OS, Policy); 2507 if (!Node->isListInitialization()) 2508 OS << '('; 2509 for (auto Arg = Node->arg_begin(), ArgEnd = Node->arg_end(); Arg != ArgEnd; 2510 ++Arg) { 2511 if (Arg != Node->arg_begin()) 2512 OS << ", "; 2513 PrintExpr(*Arg); 2514 } 2515 if (!Node->isListInitialization()) 2516 OS << ')'; 2517 } 2518 2519 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 2520 CXXDependentScopeMemberExpr *Node) { 2521 if (!Node->isImplicitAccess()) { 2522 PrintExpr(Node->getBase()); 2523 OS << (Node->isArrow() ? "->" : "."); 2524 } 2525 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2526 Qualifier->print(OS, Policy); 2527 if (Node->hasTemplateKeyword()) 2528 OS << "template "; 2529 OS << Node->getMemberNameInfo(); 2530 if (Node->hasExplicitTemplateArgs()) 2531 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 2532 } 2533 2534 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 2535 if (!Node->isImplicitAccess()) { 2536 PrintExpr(Node->getBase()); 2537 OS << (Node->isArrow() ? "->" : "."); 2538 } 2539 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2540 Qualifier->print(OS, Policy); 2541 if (Node->hasTemplateKeyword()) 2542 OS << "template "; 2543 OS << Node->getMemberNameInfo(); 2544 if (Node->hasExplicitTemplateArgs()) 2545 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 2546 } 2547 2548 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 2549 OS << getTraitSpelling(E->getTrait()) << "("; 2550 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 2551 if (I > 0) 2552 OS << ", "; 2553 E->getArg(I)->getType().print(OS, Policy); 2554 } 2555 OS << ")"; 2556 } 2557 2558 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2559 OS << getTraitSpelling(E->getTrait()) << '('; 2560 E->getQueriedType().print(OS, Policy); 2561 OS << ')'; 2562 } 2563 2564 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2565 OS << getTraitSpelling(E->getTrait()) << '('; 2566 PrintExpr(E->getQueriedExpression()); 2567 OS << ')'; 2568 } 2569 2570 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 2571 OS << "noexcept("; 2572 PrintExpr(E->getOperand()); 2573 OS << ")"; 2574 } 2575 2576 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 2577 PrintExpr(E->getPattern()); 2578 OS << "..."; 2579 } 2580 2581 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2582 OS << "sizeof...(" << *E->getPack() << ")"; 2583 } 2584 2585 void StmtPrinter::VisitPackIndexingExpr(PackIndexingExpr *E) { 2586 PrintExpr(E->getPackIdExpression()); 2587 OS << "...["; 2588 PrintExpr(E->getIndexExpr()); 2589 OS << "]"; 2590 } 2591 2592 void StmtPrinter::VisitResolvedUnexpandedPackExpr( 2593 ResolvedUnexpandedPackExpr *E) { 2594 OS << "<<resolved pack("; 2595 llvm::interleave( 2596 E->getExprs().begin(), E->getExprs().end(), 2597 [this](auto *X) { PrintExpr(X); }, [this] { OS << ", "; }); 2598 OS << ")>>"; 2599 } 2600 2601 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 2602 SubstNonTypeTemplateParmPackExpr *Node) { 2603 OS << *Node->getParameterPack(); 2604 } 2605 2606 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 2607 SubstNonTypeTemplateParmExpr *Node) { 2608 Visit(Node->getReplacement()); 2609 } 2610 2611 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 2612 OS << *E->getParameterPack(); 2613 } 2614 2615 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 2616 PrintExpr(Node->getSubExpr()); 2617 } 2618 2619 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 2620 OS << "("; 2621 if (E->getLHS()) { 2622 PrintExpr(E->getLHS()); 2623 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2624 } 2625 OS << "..."; 2626 if (E->getRHS()) { 2627 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2628 PrintExpr(E->getRHS()); 2629 } 2630 OS << ")"; 2631 } 2632 2633 void StmtPrinter::VisitCXXParenListInitExpr(CXXParenListInitExpr *Node) { 2634 OS << "("; 2635 llvm::interleaveComma(Node->getInitExprs(), OS, 2636 [&](Expr *E) { PrintExpr(E); }); 2637 OS << ")"; 2638 } 2639 2640 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) { 2641 NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc(); 2642 if (NNS) 2643 NNS.getNestedNameSpecifier()->print(OS, Policy); 2644 if (E->getTemplateKWLoc().isValid()) 2645 OS << "template "; 2646 OS << E->getFoundDecl()->getName(); 2647 printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(), 2648 Policy, 2649 E->getNamedConcept()->getTemplateParameters()); 2650 } 2651 2652 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) { 2653 OS << "requires "; 2654 auto LocalParameters = E->getLocalParameters(); 2655 if (!LocalParameters.empty()) { 2656 OS << "("; 2657 for (ParmVarDecl *LocalParam : LocalParameters) { 2658 PrintRawDecl(LocalParam); 2659 if (LocalParam != LocalParameters.back()) 2660 OS << ", "; 2661 } 2662 2663 OS << ") "; 2664 } 2665 OS << "{ "; 2666 auto Requirements = E->getRequirements(); 2667 for (concepts::Requirement *Req : Requirements) { 2668 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) { 2669 if (TypeReq->isSubstitutionFailure()) 2670 OS << "<<error-type>>"; 2671 else 2672 TypeReq->getType()->getType().print(OS, Policy); 2673 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) { 2674 if (ExprReq->isCompound()) 2675 OS << "{ "; 2676 if (ExprReq->isExprSubstitutionFailure()) 2677 OS << "<<error-expression>>"; 2678 else 2679 PrintExpr(ExprReq->getExpr()); 2680 if (ExprReq->isCompound()) { 2681 OS << " }"; 2682 if (ExprReq->getNoexceptLoc().isValid()) 2683 OS << " noexcept"; 2684 const auto &RetReq = ExprReq->getReturnTypeRequirement(); 2685 if (!RetReq.isEmpty()) { 2686 OS << " -> "; 2687 if (RetReq.isSubstitutionFailure()) 2688 OS << "<<error-type>>"; 2689 else if (RetReq.isTypeConstraint()) 2690 RetReq.getTypeConstraint()->print(OS, Policy); 2691 } 2692 } 2693 } else { 2694 auto *NestedReq = cast<concepts::NestedRequirement>(Req); 2695 OS << "requires "; 2696 if (NestedReq->hasInvalidConstraint()) 2697 OS << "<<error-expression>>"; 2698 else 2699 PrintExpr(NestedReq->getConstraintExpr()); 2700 } 2701 OS << "; "; 2702 } 2703 OS << "}"; 2704 } 2705 2706 // C++ Coroutines 2707 2708 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) { 2709 Visit(S->getBody()); 2710 } 2711 2712 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) { 2713 OS << "co_return"; 2714 if (S->getOperand()) { 2715 OS << " "; 2716 Visit(S->getOperand()); 2717 } 2718 OS << ";"; 2719 } 2720 2721 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) { 2722 OS << "co_await "; 2723 PrintExpr(S->getOperand()); 2724 } 2725 2726 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) { 2727 OS << "co_await "; 2728 PrintExpr(S->getOperand()); 2729 } 2730 2731 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) { 2732 OS << "co_yield "; 2733 PrintExpr(S->getOperand()); 2734 } 2735 2736 // Obj-C 2737 2738 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 2739 OS << "@"; 2740 VisitStringLiteral(Node->getString()); 2741 } 2742 2743 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 2744 OS << "@"; 2745 Visit(E->getSubExpr()); 2746 } 2747 2748 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 2749 OS << "@[ "; 2750 ObjCArrayLiteral::child_range Ch = E->children(); 2751 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) { 2752 if (I != Ch.begin()) 2753 OS << ", "; 2754 Visit(*I); 2755 } 2756 OS << " ]"; 2757 } 2758 2759 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 2760 OS << "@{ "; 2761 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 2762 if (I > 0) 2763 OS << ", "; 2764 2765 ObjCDictionaryElement Element = E->getKeyValueElement(I); 2766 Visit(Element.Key); 2767 OS << " : "; 2768 Visit(Element.Value); 2769 if (Element.isPackExpansion()) 2770 OS << "..."; 2771 } 2772 OS << " }"; 2773 } 2774 2775 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 2776 OS << "@encode("; 2777 Node->getEncodedType().print(OS, Policy); 2778 OS << ')'; 2779 } 2780 2781 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 2782 OS << "@selector("; 2783 Node->getSelector().print(OS); 2784 OS << ')'; 2785 } 2786 2787 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 2788 OS << "@protocol(" << *Node->getProtocol() << ')'; 2789 } 2790 2791 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 2792 OS << "["; 2793 switch (Mess->getReceiverKind()) { 2794 case ObjCMessageExpr::Instance: 2795 PrintExpr(Mess->getInstanceReceiver()); 2796 break; 2797 2798 case ObjCMessageExpr::Class: 2799 Mess->getClassReceiver().print(OS, Policy); 2800 break; 2801 2802 case ObjCMessageExpr::SuperInstance: 2803 case ObjCMessageExpr::SuperClass: 2804 OS << "Super"; 2805 break; 2806 } 2807 2808 OS << ' '; 2809 Selector selector = Mess->getSelector(); 2810 if (selector.isUnarySelector()) { 2811 OS << selector.getNameForSlot(0); 2812 } else { 2813 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 2814 if (i < selector.getNumArgs()) { 2815 if (i > 0) OS << ' '; 2816 if (selector.getIdentifierInfoForSlot(i)) 2817 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 2818 else 2819 OS << ":"; 2820 } 2821 else OS << ", "; // Handle variadic methods. 2822 2823 PrintExpr(Mess->getArg(i)); 2824 } 2825 } 2826 OS << "]"; 2827 } 2828 2829 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 2830 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 2831 } 2832 2833 void 2834 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2835 PrintExpr(E->getSubExpr()); 2836 } 2837 2838 void 2839 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2840 OS << '(' << E->getBridgeKindName(); 2841 E->getType().print(OS, Policy); 2842 OS << ')'; 2843 PrintExpr(E->getSubExpr()); 2844 } 2845 2846 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2847 BlockDecl *BD = Node->getBlockDecl(); 2848 OS << "^"; 2849 2850 const FunctionType *AFT = Node->getFunctionType(); 2851 2852 if (isa<FunctionNoProtoType>(AFT)) { 2853 OS << "()"; 2854 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2855 OS << '('; 2856 for (BlockDecl::param_iterator AI = BD->param_begin(), 2857 E = BD->param_end(); AI != E; ++AI) { 2858 if (AI != BD->param_begin()) OS << ", "; 2859 std::string ParamStr = (*AI)->getNameAsString(); 2860 (*AI)->getType().print(OS, Policy, ParamStr); 2861 } 2862 2863 const auto *FT = cast<FunctionProtoType>(AFT); 2864 if (FT->isVariadic()) { 2865 if (!BD->param_empty()) OS << ", "; 2866 OS << "..."; 2867 } 2868 OS << ')'; 2869 } 2870 OS << "{ }"; 2871 } 2872 2873 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2874 PrintExpr(Node->getSourceExpr()); 2875 } 2876 2877 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 2878 // TODO: Print something reasonable for a TypoExpr, if necessary. 2879 llvm_unreachable("Cannot print TypoExpr nodes"); 2880 } 2881 2882 void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) { 2883 OS << "<recovery-expr>("; 2884 const char *Sep = ""; 2885 for (Expr *E : Node->subExpressions()) { 2886 OS << Sep; 2887 PrintExpr(E); 2888 Sep = ", "; 2889 } 2890 OS << ')'; 2891 } 2892 2893 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2894 OS << "__builtin_astype("; 2895 PrintExpr(Node->getSrcExpr()); 2896 OS << ", "; 2897 Node->getType().print(OS, Policy); 2898 OS << ")"; 2899 } 2900 2901 void StmtPrinter::VisitHLSLOutArgExpr(HLSLOutArgExpr *Node) { 2902 PrintExpr(Node->getArgLValue()); 2903 } 2904 2905 //===----------------------------------------------------------------------===// 2906 // Stmt method implementations 2907 //===----------------------------------------------------------------------===// 2908 2909 void Stmt::dumpPretty(const ASTContext &Context) const { 2910 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2911 } 2912 2913 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper, 2914 const PrintingPolicy &Policy, unsigned Indentation, 2915 StringRef NL, const ASTContext *Context) const { 2916 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context); 2917 P.Visit(const_cast<Stmt *>(this)); 2918 } 2919 2920 void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper, 2921 const PrintingPolicy &Policy, 2922 unsigned Indentation, StringRef NL, 2923 const ASTContext *Context) const { 2924 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context); 2925 P.PrintControlledStmt(const_cast<Stmt *>(this)); 2926 } 2927 2928 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper, 2929 const PrintingPolicy &Policy, bool AddQuotes) const { 2930 std::string Buf; 2931 llvm::raw_string_ostream TempOut(Buf); 2932 2933 printPretty(TempOut, Helper, Policy); 2934 2935 Out << JsonFormat(TempOut.str(), AddQuotes); 2936 } 2937 2938 //===----------------------------------------------------------------------===// 2939 // PrinterHelper 2940 //===----------------------------------------------------------------------===// 2941 2942 // Implement virtual destructor. 2943 PrinterHelper::~PrinterHelper() = default; 2944