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