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::VisitOMPParallelSectionsDirective( 753 OMPParallelSectionsDirective *Node) { 754 Indent() << "#pragma omp parallel sections"; 755 PrintOMPExecutableDirective(Node); 756 } 757 758 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) { 759 Indent() << "#pragma omp task"; 760 PrintOMPExecutableDirective(Node); 761 } 762 763 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) { 764 Indent() << "#pragma omp taskyield"; 765 PrintOMPExecutableDirective(Node); 766 } 767 768 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) { 769 Indent() << "#pragma omp barrier"; 770 PrintOMPExecutableDirective(Node); 771 } 772 773 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) { 774 Indent() << "#pragma omp taskwait"; 775 PrintOMPExecutableDirective(Node); 776 } 777 778 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) { 779 Indent() << "#pragma omp taskgroup"; 780 PrintOMPExecutableDirective(Node); 781 } 782 783 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) { 784 Indent() << "#pragma omp flush"; 785 PrintOMPExecutableDirective(Node); 786 } 787 788 void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) { 789 Indent() << "#pragma omp depobj"; 790 PrintOMPExecutableDirective(Node); 791 } 792 793 void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) { 794 Indent() << "#pragma omp scan"; 795 PrintOMPExecutableDirective(Node); 796 } 797 798 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) { 799 Indent() << "#pragma omp ordered"; 800 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>()); 801 } 802 803 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) { 804 Indent() << "#pragma omp atomic"; 805 PrintOMPExecutableDirective(Node); 806 } 807 808 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) { 809 Indent() << "#pragma omp target"; 810 PrintOMPExecutableDirective(Node); 811 } 812 813 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) { 814 Indent() << "#pragma omp target data"; 815 PrintOMPExecutableDirective(Node); 816 } 817 818 void StmtPrinter::VisitOMPTargetEnterDataDirective( 819 OMPTargetEnterDataDirective *Node) { 820 Indent() << "#pragma omp target enter data"; 821 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true); 822 } 823 824 void StmtPrinter::VisitOMPTargetExitDataDirective( 825 OMPTargetExitDataDirective *Node) { 826 Indent() << "#pragma omp target exit data"; 827 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true); 828 } 829 830 void StmtPrinter::VisitOMPTargetParallelDirective( 831 OMPTargetParallelDirective *Node) { 832 Indent() << "#pragma omp target parallel"; 833 PrintOMPExecutableDirective(Node); 834 } 835 836 void StmtPrinter::VisitOMPTargetParallelForDirective( 837 OMPTargetParallelForDirective *Node) { 838 Indent() << "#pragma omp target parallel for"; 839 PrintOMPExecutableDirective(Node); 840 } 841 842 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) { 843 Indent() << "#pragma omp teams"; 844 PrintOMPExecutableDirective(Node); 845 } 846 847 void StmtPrinter::VisitOMPCancellationPointDirective( 848 OMPCancellationPointDirective *Node) { 849 Indent() << "#pragma omp cancellation point " 850 << getOpenMPDirectiveName(Node->getCancelRegion()); 851 PrintOMPExecutableDirective(Node); 852 } 853 854 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) { 855 Indent() << "#pragma omp cancel " 856 << getOpenMPDirectiveName(Node->getCancelRegion()); 857 PrintOMPExecutableDirective(Node); 858 } 859 860 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) { 861 Indent() << "#pragma omp taskloop"; 862 PrintOMPExecutableDirective(Node); 863 } 864 865 void StmtPrinter::VisitOMPTaskLoopSimdDirective( 866 OMPTaskLoopSimdDirective *Node) { 867 Indent() << "#pragma omp taskloop simd"; 868 PrintOMPExecutableDirective(Node); 869 } 870 871 void StmtPrinter::VisitOMPMasterTaskLoopDirective( 872 OMPMasterTaskLoopDirective *Node) { 873 Indent() << "#pragma omp master taskloop"; 874 PrintOMPExecutableDirective(Node); 875 } 876 877 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective( 878 OMPMasterTaskLoopSimdDirective *Node) { 879 Indent() << "#pragma omp master taskloop simd"; 880 PrintOMPExecutableDirective(Node); 881 } 882 883 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective( 884 OMPParallelMasterTaskLoopDirective *Node) { 885 Indent() << "#pragma omp parallel master taskloop"; 886 PrintOMPExecutableDirective(Node); 887 } 888 889 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective( 890 OMPParallelMasterTaskLoopSimdDirective *Node) { 891 Indent() << "#pragma omp parallel master taskloop simd"; 892 PrintOMPExecutableDirective(Node); 893 } 894 895 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) { 896 Indent() << "#pragma omp distribute"; 897 PrintOMPExecutableDirective(Node); 898 } 899 900 void StmtPrinter::VisitOMPTargetUpdateDirective( 901 OMPTargetUpdateDirective *Node) { 902 Indent() << "#pragma omp target update"; 903 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true); 904 } 905 906 void StmtPrinter::VisitOMPDistributeParallelForDirective( 907 OMPDistributeParallelForDirective *Node) { 908 Indent() << "#pragma omp distribute parallel for"; 909 PrintOMPExecutableDirective(Node); 910 } 911 912 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective( 913 OMPDistributeParallelForSimdDirective *Node) { 914 Indent() << "#pragma omp distribute parallel for simd"; 915 PrintOMPExecutableDirective(Node); 916 } 917 918 void StmtPrinter::VisitOMPDistributeSimdDirective( 919 OMPDistributeSimdDirective *Node) { 920 Indent() << "#pragma omp distribute simd"; 921 PrintOMPExecutableDirective(Node); 922 } 923 924 void StmtPrinter::VisitOMPTargetParallelForSimdDirective( 925 OMPTargetParallelForSimdDirective *Node) { 926 Indent() << "#pragma omp target parallel for simd"; 927 PrintOMPExecutableDirective(Node); 928 } 929 930 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) { 931 Indent() << "#pragma omp target simd"; 932 PrintOMPExecutableDirective(Node); 933 } 934 935 void StmtPrinter::VisitOMPTeamsDistributeDirective( 936 OMPTeamsDistributeDirective *Node) { 937 Indent() << "#pragma omp teams distribute"; 938 PrintOMPExecutableDirective(Node); 939 } 940 941 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective( 942 OMPTeamsDistributeSimdDirective *Node) { 943 Indent() << "#pragma omp teams distribute simd"; 944 PrintOMPExecutableDirective(Node); 945 } 946 947 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective( 948 OMPTeamsDistributeParallelForSimdDirective *Node) { 949 Indent() << "#pragma omp teams distribute parallel for simd"; 950 PrintOMPExecutableDirective(Node); 951 } 952 953 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective( 954 OMPTeamsDistributeParallelForDirective *Node) { 955 Indent() << "#pragma omp teams distribute parallel for"; 956 PrintOMPExecutableDirective(Node); 957 } 958 959 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) { 960 Indent() << "#pragma omp target teams"; 961 PrintOMPExecutableDirective(Node); 962 } 963 964 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective( 965 OMPTargetTeamsDistributeDirective *Node) { 966 Indent() << "#pragma omp target teams distribute"; 967 PrintOMPExecutableDirective(Node); 968 } 969 970 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective( 971 OMPTargetTeamsDistributeParallelForDirective *Node) { 972 Indent() << "#pragma omp target teams distribute parallel for"; 973 PrintOMPExecutableDirective(Node); 974 } 975 976 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective( 977 OMPTargetTeamsDistributeParallelForSimdDirective *Node) { 978 Indent() << "#pragma omp target teams distribute parallel for simd"; 979 PrintOMPExecutableDirective(Node); 980 } 981 982 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective( 983 OMPTargetTeamsDistributeSimdDirective *Node) { 984 Indent() << "#pragma omp target teams distribute simd"; 985 PrintOMPExecutableDirective(Node); 986 } 987 988 void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) { 989 Indent() << "#pragma omp interop"; 990 PrintOMPExecutableDirective(Node); 991 } 992 993 void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) { 994 Indent() << "#pragma omp dispatch"; 995 PrintOMPExecutableDirective(Node); 996 } 997 998 void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) { 999 Indent() << "#pragma omp masked"; 1000 PrintOMPExecutableDirective(Node); 1001 } 1002 1003 void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) { 1004 Indent() << "#pragma omp loop"; 1005 PrintOMPExecutableDirective(Node); 1006 } 1007 1008 void StmtPrinter::VisitOMPTeamsGenericLoopDirective( 1009 OMPTeamsGenericLoopDirective *Node) { 1010 Indent() << "#pragma omp teams loop"; 1011 PrintOMPExecutableDirective(Node); 1012 } 1013 1014 void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective( 1015 OMPTargetTeamsGenericLoopDirective *Node) { 1016 Indent() << "#pragma omp target teams loop"; 1017 PrintOMPExecutableDirective(Node); 1018 } 1019 1020 void StmtPrinter::VisitOMPParallelGenericLoopDirective( 1021 OMPParallelGenericLoopDirective *Node) { 1022 Indent() << "#pragma omp parallel loop"; 1023 PrintOMPExecutableDirective(Node); 1024 } 1025 1026 //===----------------------------------------------------------------------===// 1027 // Expr printing methods. 1028 //===----------------------------------------------------------------------===// 1029 1030 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) { 1031 OS << Node->getBuiltinStr() << "()"; 1032 } 1033 1034 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) { 1035 PrintExpr(Node->getSubExpr()); 1036 } 1037 1038 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 1039 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) { 1040 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy); 1041 return; 1042 } 1043 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Node->getDecl())) { 1044 TPOD->printAsExpr(OS, Policy); 1045 return; 1046 } 1047 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1048 Qualifier->print(OS, Policy); 1049 if (Node->hasTemplateKeyword()) 1050 OS << "template "; 1051 if (Policy.CleanUglifiedParameters && 1052 isa<ParmVarDecl, NonTypeTemplateParmDecl>(Node->getDecl()) && 1053 Node->getDecl()->getIdentifier()) 1054 OS << Node->getDecl()->getIdentifier()->deuglifiedName(); 1055 else 1056 Node->getNameInfo().printName(OS, Policy); 1057 if (Node->hasExplicitTemplateArgs()) { 1058 const TemplateParameterList *TPL = nullptr; 1059 if (!Node->hadMultipleCandidates()) 1060 if (auto *TD = dyn_cast<TemplateDecl>(Node->getDecl())) 1061 TPL = TD->getTemplateParameters(); 1062 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL); 1063 } 1064 } 1065 1066 void StmtPrinter::VisitDependentScopeDeclRefExpr( 1067 DependentScopeDeclRefExpr *Node) { 1068 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1069 Qualifier->print(OS, Policy); 1070 if (Node->hasTemplateKeyword()) 1071 OS << "template "; 1072 OS << Node->getNameInfo(); 1073 if (Node->hasExplicitTemplateArgs()) 1074 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 1075 } 1076 1077 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 1078 if (Node->getQualifier()) 1079 Node->getQualifier()->print(OS, Policy); 1080 if (Node->hasTemplateKeyword()) 1081 OS << "template "; 1082 OS << Node->getNameInfo(); 1083 if (Node->hasExplicitTemplateArgs()) 1084 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 1085 } 1086 1087 static bool isImplicitSelf(const Expr *E) { 1088 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 1089 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) { 1090 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf && 1091 DRE->getBeginLoc().isInvalid()) 1092 return true; 1093 } 1094 } 1095 return false; 1096 } 1097 1098 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 1099 if (Node->getBase()) { 1100 if (!Policy.SuppressImplicitBase || 1101 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) { 1102 PrintExpr(Node->getBase()); 1103 OS << (Node->isArrow() ? "->" : "."); 1104 } 1105 } 1106 OS << *Node->getDecl(); 1107 } 1108 1109 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 1110 if (Node->isSuperReceiver()) 1111 OS << "super."; 1112 else if (Node->isObjectReceiver() && Node->getBase()) { 1113 PrintExpr(Node->getBase()); 1114 OS << "."; 1115 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 1116 OS << Node->getClassReceiver()->getName() << "."; 1117 } 1118 1119 if (Node->isImplicitProperty()) { 1120 if (const auto *Getter = Node->getImplicitPropertyGetter()) 1121 Getter->getSelector().print(OS); 1122 else 1123 OS << SelectorTable::getPropertyNameFromSetterSelector( 1124 Node->getImplicitPropertySetter()->getSelector()); 1125 } else 1126 OS << Node->getExplicitProperty()->getName(); 1127 } 1128 1129 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 1130 PrintExpr(Node->getBaseExpr()); 1131 OS << "["; 1132 PrintExpr(Node->getKeyExpr()); 1133 OS << "]"; 1134 } 1135 1136 void StmtPrinter::VisitSYCLUniqueStableNameExpr( 1137 SYCLUniqueStableNameExpr *Node) { 1138 OS << "__builtin_sycl_unique_stable_name("; 1139 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1140 OS << ")"; 1141 } 1142 1143 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 1144 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind()); 1145 } 1146 1147 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 1148 CharacterLiteral::print(Node->getValue(), Node->getKind(), OS); 1149 } 1150 1151 /// Prints the given expression using the original source text. Returns true on 1152 /// success, false otherwise. 1153 static bool printExprAsWritten(raw_ostream &OS, Expr *E, 1154 const ASTContext *Context) { 1155 if (!Context) 1156 return false; 1157 bool Invalid = false; 1158 StringRef Source = Lexer::getSourceText( 1159 CharSourceRange::getTokenRange(E->getSourceRange()), 1160 Context->getSourceManager(), Context->getLangOpts(), &Invalid); 1161 if (!Invalid) { 1162 OS << Source; 1163 return true; 1164 } 1165 return false; 1166 } 1167 1168 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 1169 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context)) 1170 return; 1171 bool isSigned = Node->getType()->isSignedIntegerType(); 1172 OS << toString(Node->getValue(), 10, isSigned); 1173 1174 if (isa<BitIntType>(Node->getType())) { 1175 OS << (isSigned ? "wb" : "uwb"); 1176 return; 1177 } 1178 1179 // Emit suffixes. Integer literals are always a builtin integer type. 1180 switch (Node->getType()->castAs<BuiltinType>()->getKind()) { 1181 default: llvm_unreachable("Unexpected type for integer literal!"); 1182 case BuiltinType::Char_S: 1183 case BuiltinType::Char_U: OS << "i8"; break; 1184 case BuiltinType::UChar: OS << "Ui8"; break; 1185 case BuiltinType::Short: OS << "i16"; break; 1186 case BuiltinType::UShort: OS << "Ui16"; break; 1187 case BuiltinType::Int: break; // no suffix. 1188 case BuiltinType::UInt: OS << 'U'; break; 1189 case BuiltinType::Long: OS << 'L'; break; 1190 case BuiltinType::ULong: OS << "UL"; break; 1191 case BuiltinType::LongLong: OS << "LL"; break; 1192 case BuiltinType::ULongLong: OS << "ULL"; break; 1193 case BuiltinType::Int128: 1194 break; // no suffix. 1195 case BuiltinType::UInt128: 1196 break; // no suffix. 1197 } 1198 } 1199 1200 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) { 1201 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context)) 1202 return; 1203 OS << Node->getValueAsString(/*Radix=*/10); 1204 1205 switch (Node->getType()->castAs<BuiltinType>()->getKind()) { 1206 default: llvm_unreachable("Unexpected type for fixed point literal!"); 1207 case BuiltinType::ShortFract: OS << "hr"; break; 1208 case BuiltinType::ShortAccum: OS << "hk"; break; 1209 case BuiltinType::UShortFract: OS << "uhr"; break; 1210 case BuiltinType::UShortAccum: OS << "uhk"; break; 1211 case BuiltinType::Fract: OS << "r"; break; 1212 case BuiltinType::Accum: OS << "k"; break; 1213 case BuiltinType::UFract: OS << "ur"; break; 1214 case BuiltinType::UAccum: OS << "uk"; break; 1215 case BuiltinType::LongFract: OS << "lr"; break; 1216 case BuiltinType::LongAccum: OS << "lk"; break; 1217 case BuiltinType::ULongFract: OS << "ulr"; break; 1218 case BuiltinType::ULongAccum: OS << "ulk"; break; 1219 } 1220 } 1221 1222 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 1223 bool PrintSuffix) { 1224 SmallString<16> Str; 1225 Node->getValue().toString(Str); 1226 OS << Str; 1227 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 1228 OS << '.'; // Trailing dot in order to separate from ints. 1229 1230 if (!PrintSuffix) 1231 return; 1232 1233 // Emit suffixes. Float literals are always a builtin float type. 1234 switch (Node->getType()->castAs<BuiltinType>()->getKind()) { 1235 default: llvm_unreachable("Unexpected type for float literal!"); 1236 case BuiltinType::Half: break; // FIXME: suffix? 1237 case BuiltinType::Ibm128: break; // FIXME: No suffix for ibm128 literal 1238 case BuiltinType::Double: break; // no suffix. 1239 case BuiltinType::Float16: OS << "F16"; break; 1240 case BuiltinType::Float: OS << 'F'; break; 1241 case BuiltinType::LongDouble: OS << 'L'; break; 1242 case BuiltinType::Float128: OS << 'Q'; break; 1243 } 1244 } 1245 1246 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 1247 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context)) 1248 return; 1249 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 1250 } 1251 1252 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 1253 PrintExpr(Node->getSubExpr()); 1254 OS << "i"; 1255 } 1256 1257 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 1258 Str->outputString(OS); 1259 } 1260 1261 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 1262 OS << "("; 1263 PrintExpr(Node->getSubExpr()); 1264 OS << ")"; 1265 } 1266 1267 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1268 if (!Node->isPostfix()) { 1269 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1270 1271 // Print a space if this is an "identifier operator" like __real, or if 1272 // it might be concatenated incorrectly like '+'. 1273 switch (Node->getOpcode()) { 1274 default: break; 1275 case UO_Real: 1276 case UO_Imag: 1277 case UO_Extension: 1278 OS << ' '; 1279 break; 1280 case UO_Plus: 1281 case UO_Minus: 1282 if (isa<UnaryOperator>(Node->getSubExpr())) 1283 OS << ' '; 1284 break; 1285 } 1286 } 1287 PrintExpr(Node->getSubExpr()); 1288 1289 if (Node->isPostfix()) 1290 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1291 } 1292 1293 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1294 OS << "__builtin_offsetof("; 1295 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1296 OS << ", "; 1297 bool PrintedSomething = false; 1298 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1299 OffsetOfNode ON = Node->getComponent(i); 1300 if (ON.getKind() == OffsetOfNode::Array) { 1301 // Array node 1302 OS << "["; 1303 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1304 OS << "]"; 1305 PrintedSomething = true; 1306 continue; 1307 } 1308 1309 // Skip implicit base indirections. 1310 if (ON.getKind() == OffsetOfNode::Base) 1311 continue; 1312 1313 // Field or identifier node. 1314 IdentifierInfo *Id = ON.getFieldName(); 1315 if (!Id) 1316 continue; 1317 1318 if (PrintedSomething) 1319 OS << "."; 1320 else 1321 PrintedSomething = true; 1322 OS << Id->getName(); 1323 } 1324 OS << ")"; 1325 } 1326 1327 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr( 1328 UnaryExprOrTypeTraitExpr *Node) { 1329 const char *Spelling = getTraitSpelling(Node->getKind()); 1330 if (Node->getKind() == UETT_AlignOf) { 1331 if (Policy.Alignof) 1332 Spelling = "alignof"; 1333 else if (Policy.UnderscoreAlignof) 1334 Spelling = "_Alignof"; 1335 else 1336 Spelling = "__alignof"; 1337 } 1338 1339 OS << Spelling; 1340 1341 if (Node->isArgumentType()) { 1342 OS << '('; 1343 Node->getArgumentType().print(OS, Policy); 1344 OS << ')'; 1345 } else { 1346 OS << " "; 1347 PrintExpr(Node->getArgumentExpr()); 1348 } 1349 } 1350 1351 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1352 OS << "_Generic("; 1353 PrintExpr(Node->getControllingExpr()); 1354 for (const GenericSelectionExpr::Association Assoc : Node->associations()) { 1355 OS << ", "; 1356 QualType T = Assoc.getType(); 1357 if (T.isNull()) 1358 OS << "default"; 1359 else 1360 T.print(OS, Policy); 1361 OS << ": "; 1362 PrintExpr(Assoc.getAssociationExpr()); 1363 } 1364 OS << ")"; 1365 } 1366 1367 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1368 PrintExpr(Node->getLHS()); 1369 OS << "["; 1370 PrintExpr(Node->getRHS()); 1371 OS << "]"; 1372 } 1373 1374 void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) { 1375 PrintExpr(Node->getBase()); 1376 OS << "["; 1377 PrintExpr(Node->getRowIdx()); 1378 OS << "]"; 1379 OS << "["; 1380 PrintExpr(Node->getColumnIdx()); 1381 OS << "]"; 1382 } 1383 1384 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) { 1385 PrintExpr(Node->getBase()); 1386 OS << "["; 1387 if (Node->getLowerBound()) 1388 PrintExpr(Node->getLowerBound()); 1389 if (Node->getColonLocFirst().isValid()) { 1390 OS << ":"; 1391 if (Node->getLength()) 1392 PrintExpr(Node->getLength()); 1393 } 1394 if (Node->getColonLocSecond().isValid()) { 1395 OS << ":"; 1396 if (Node->getStride()) 1397 PrintExpr(Node->getStride()); 1398 } 1399 OS << "]"; 1400 } 1401 1402 void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) { 1403 OS << "("; 1404 for (Expr *E : Node->getDimensions()) { 1405 OS << "["; 1406 PrintExpr(E); 1407 OS << "]"; 1408 } 1409 OS << ")"; 1410 PrintExpr(Node->getBase()); 1411 } 1412 1413 void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) { 1414 OS << "iterator("; 1415 for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) { 1416 auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I)); 1417 VD->getType().print(OS, Policy); 1418 const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I); 1419 OS << " " << VD->getName() << " = "; 1420 PrintExpr(Range.Begin); 1421 OS << ":"; 1422 PrintExpr(Range.End); 1423 if (Range.Step) { 1424 OS << ":"; 1425 PrintExpr(Range.Step); 1426 } 1427 if (I < E - 1) 1428 OS << ", "; 1429 } 1430 OS << ")"; 1431 } 1432 1433 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1434 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1435 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1436 // Don't print any defaulted arguments 1437 break; 1438 } 1439 1440 if (i) OS << ", "; 1441 PrintExpr(Call->getArg(i)); 1442 } 1443 } 1444 1445 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1446 PrintExpr(Call->getCallee()); 1447 OS << "("; 1448 PrintCallArgs(Call); 1449 OS << ")"; 1450 } 1451 1452 static bool isImplicitThis(const Expr *E) { 1453 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) 1454 return TE->isImplicit(); 1455 return false; 1456 } 1457 1458 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1459 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) { 1460 PrintExpr(Node->getBase()); 1461 1462 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1463 FieldDecl *ParentDecl = 1464 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) 1465 : nullptr; 1466 1467 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1468 OS << (Node->isArrow() ? "->" : "."); 1469 } 1470 1471 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1472 if (FD->isAnonymousStructOrUnion()) 1473 return; 1474 1475 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1476 Qualifier->print(OS, Policy); 1477 if (Node->hasTemplateKeyword()) 1478 OS << "template "; 1479 OS << Node->getMemberNameInfo(); 1480 const TemplateParameterList *TPL = nullptr; 1481 if (auto *FD = dyn_cast<FunctionDecl>(Node->getMemberDecl())) { 1482 if (!Node->hadMultipleCandidates()) 1483 if (auto *FTD = FD->getPrimaryTemplate()) 1484 TPL = FTD->getTemplateParameters(); 1485 } else if (auto *VTSD = 1486 dyn_cast<VarTemplateSpecializationDecl>(Node->getMemberDecl())) 1487 TPL = VTSD->getSpecializedTemplate()->getTemplateParameters(); 1488 if (Node->hasExplicitTemplateArgs()) 1489 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL); 1490 } 1491 1492 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1493 PrintExpr(Node->getBase()); 1494 OS << (Node->isArrow() ? "->isa" : ".isa"); 1495 } 1496 1497 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1498 PrintExpr(Node->getBase()); 1499 OS << "."; 1500 OS << Node->getAccessor().getName(); 1501 } 1502 1503 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1504 OS << '('; 1505 Node->getTypeAsWritten().print(OS, Policy); 1506 OS << ')'; 1507 PrintExpr(Node->getSubExpr()); 1508 } 1509 1510 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1511 OS << '('; 1512 Node->getType().print(OS, Policy); 1513 OS << ')'; 1514 PrintExpr(Node->getInitializer()); 1515 } 1516 1517 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1518 // No need to print anything, simply forward to the subexpression. 1519 PrintExpr(Node->getSubExpr()); 1520 } 1521 1522 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1523 PrintExpr(Node->getLHS()); 1524 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1525 PrintExpr(Node->getRHS()); 1526 } 1527 1528 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1529 PrintExpr(Node->getLHS()); 1530 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1531 PrintExpr(Node->getRHS()); 1532 } 1533 1534 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1535 PrintExpr(Node->getCond()); 1536 OS << " ? "; 1537 PrintExpr(Node->getLHS()); 1538 OS << " : "; 1539 PrintExpr(Node->getRHS()); 1540 } 1541 1542 // GNU extensions. 1543 1544 void 1545 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1546 PrintExpr(Node->getCommon()); 1547 OS << " ?: "; 1548 PrintExpr(Node->getFalseExpr()); 1549 } 1550 1551 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1552 OS << "&&" << Node->getLabel()->getName(); 1553 } 1554 1555 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1556 OS << "("; 1557 PrintRawCompoundStmt(E->getSubStmt()); 1558 OS << ")"; 1559 } 1560 1561 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1562 OS << "__builtin_choose_expr("; 1563 PrintExpr(Node->getCond()); 1564 OS << ", "; 1565 PrintExpr(Node->getLHS()); 1566 OS << ", "; 1567 PrintExpr(Node->getRHS()); 1568 OS << ")"; 1569 } 1570 1571 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1572 OS << "__null"; 1573 } 1574 1575 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1576 OS << "__builtin_shufflevector("; 1577 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1578 if (i) OS << ", "; 1579 PrintExpr(Node->getExpr(i)); 1580 } 1581 OS << ")"; 1582 } 1583 1584 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1585 OS << "__builtin_convertvector("; 1586 PrintExpr(Node->getSrcExpr()); 1587 OS << ", "; 1588 Node->getType().print(OS, Policy); 1589 OS << ")"; 1590 } 1591 1592 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1593 if (Node->getSyntacticForm()) { 1594 Visit(Node->getSyntacticForm()); 1595 return; 1596 } 1597 1598 OS << "{"; 1599 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1600 if (i) OS << ", "; 1601 if (Node->getInit(i)) 1602 PrintExpr(Node->getInit(i)); 1603 else 1604 OS << "{}"; 1605 } 1606 OS << "}"; 1607 } 1608 1609 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) { 1610 // There's no way to express this expression in any of our supported 1611 // languages, so just emit something terse and (hopefully) clear. 1612 OS << "{"; 1613 PrintExpr(Node->getSubExpr()); 1614 OS << "}"; 1615 } 1616 1617 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) { 1618 OS << "*"; 1619 } 1620 1621 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1622 OS << "("; 1623 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1624 if (i) OS << ", "; 1625 PrintExpr(Node->getExpr(i)); 1626 } 1627 OS << ")"; 1628 } 1629 1630 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1631 bool NeedsEquals = true; 1632 for (const DesignatedInitExpr::Designator &D : Node->designators()) { 1633 if (D.isFieldDesignator()) { 1634 if (D.getDotLoc().isInvalid()) { 1635 if (IdentifierInfo *II = D.getFieldName()) { 1636 OS << II->getName() << ":"; 1637 NeedsEquals = false; 1638 } 1639 } else { 1640 OS << "." << D.getFieldName()->getName(); 1641 } 1642 } else { 1643 OS << "["; 1644 if (D.isArrayDesignator()) { 1645 PrintExpr(Node->getArrayIndex(D)); 1646 } else { 1647 PrintExpr(Node->getArrayRangeStart(D)); 1648 OS << " ... "; 1649 PrintExpr(Node->getArrayRangeEnd(D)); 1650 } 1651 OS << "]"; 1652 } 1653 } 1654 1655 if (NeedsEquals) 1656 OS << " = "; 1657 else 1658 OS << " "; 1659 PrintExpr(Node->getInit()); 1660 } 1661 1662 void StmtPrinter::VisitDesignatedInitUpdateExpr( 1663 DesignatedInitUpdateExpr *Node) { 1664 OS << "{"; 1665 OS << "/*base*/"; 1666 PrintExpr(Node->getBase()); 1667 OS << ", "; 1668 1669 OS << "/*updater*/"; 1670 PrintExpr(Node->getUpdater()); 1671 OS << "}"; 1672 } 1673 1674 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) { 1675 OS << "/*no init*/"; 1676 } 1677 1678 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1679 if (Node->getType()->getAsCXXRecordDecl()) { 1680 OS << "/*implicit*/"; 1681 Node->getType().print(OS, Policy); 1682 OS << "()"; 1683 } else { 1684 OS << "/*implicit*/("; 1685 Node->getType().print(OS, Policy); 1686 OS << ')'; 1687 if (Node->getType()->isRecordType()) 1688 OS << "{}"; 1689 else 1690 OS << 0; 1691 } 1692 } 1693 1694 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1695 OS << "__builtin_va_arg("; 1696 PrintExpr(Node->getSubExpr()); 1697 OS << ", "; 1698 Node->getType().print(OS, Policy); 1699 OS << ")"; 1700 } 1701 1702 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1703 PrintExpr(Node->getSyntacticForm()); 1704 } 1705 1706 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1707 const char *Name = nullptr; 1708 switch (Node->getOp()) { 1709 #define BUILTIN(ID, TYPE, ATTRS) 1710 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1711 case AtomicExpr::AO ## ID: \ 1712 Name = #ID "("; \ 1713 break; 1714 #include "clang/Basic/Builtins.def" 1715 } 1716 OS << Name; 1717 1718 // AtomicExpr stores its subexpressions in a permuted order. 1719 PrintExpr(Node->getPtr()); 1720 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1721 Node->getOp() != AtomicExpr::AO__atomic_load_n && 1722 Node->getOp() != AtomicExpr::AO__opencl_atomic_load && 1723 Node->getOp() != AtomicExpr::AO__hip_atomic_load) { 1724 OS << ", "; 1725 PrintExpr(Node->getVal1()); 1726 } 1727 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1728 Node->isCmpXChg()) { 1729 OS << ", "; 1730 PrintExpr(Node->getVal2()); 1731 } 1732 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1733 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1734 OS << ", "; 1735 PrintExpr(Node->getWeak()); 1736 } 1737 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init && 1738 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) { 1739 OS << ", "; 1740 PrintExpr(Node->getOrder()); 1741 } 1742 if (Node->isCmpXChg()) { 1743 OS << ", "; 1744 PrintExpr(Node->getOrderFail()); 1745 } 1746 OS << ")"; 1747 } 1748 1749 // C++ 1750 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1751 OverloadedOperatorKind Kind = Node->getOperator(); 1752 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1753 if (Node->getNumArgs() == 1) { 1754 OS << getOperatorSpelling(Kind) << ' '; 1755 PrintExpr(Node->getArg(0)); 1756 } else { 1757 PrintExpr(Node->getArg(0)); 1758 OS << ' ' << getOperatorSpelling(Kind); 1759 } 1760 } else if (Kind == OO_Arrow) { 1761 PrintExpr(Node->getArg(0)); 1762 } else if (Kind == OO_Call || Kind == OO_Subscript) { 1763 PrintExpr(Node->getArg(0)); 1764 OS << (Kind == OO_Call ? '(' : '['); 1765 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1766 if (ArgIdx > 1) 1767 OS << ", "; 1768 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1769 PrintExpr(Node->getArg(ArgIdx)); 1770 } 1771 OS << (Kind == OO_Call ? ')' : ']'); 1772 } else if (Node->getNumArgs() == 1) { 1773 OS << getOperatorSpelling(Kind) << ' '; 1774 PrintExpr(Node->getArg(0)); 1775 } else if (Node->getNumArgs() == 2) { 1776 PrintExpr(Node->getArg(0)); 1777 OS << ' ' << getOperatorSpelling(Kind) << ' '; 1778 PrintExpr(Node->getArg(1)); 1779 } else { 1780 llvm_unreachable("unknown overloaded operator"); 1781 } 1782 } 1783 1784 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1785 // If we have a conversion operator call only print the argument. 1786 CXXMethodDecl *MD = Node->getMethodDecl(); 1787 if (MD && isa<CXXConversionDecl>(MD)) { 1788 PrintExpr(Node->getImplicitObjectArgument()); 1789 return; 1790 } 1791 VisitCallExpr(cast<CallExpr>(Node)); 1792 } 1793 1794 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1795 PrintExpr(Node->getCallee()); 1796 OS << "<<<"; 1797 PrintCallArgs(Node->getConfig()); 1798 OS << ">>>("; 1799 PrintCallArgs(Node); 1800 OS << ")"; 1801 } 1802 1803 void StmtPrinter::VisitCXXRewrittenBinaryOperator( 1804 CXXRewrittenBinaryOperator *Node) { 1805 CXXRewrittenBinaryOperator::DecomposedForm Decomposed = 1806 Node->getDecomposedForm(); 1807 PrintExpr(const_cast<Expr*>(Decomposed.LHS)); 1808 OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' '; 1809 PrintExpr(const_cast<Expr*>(Decomposed.RHS)); 1810 } 1811 1812 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1813 OS << Node->getCastName() << '<'; 1814 Node->getTypeAsWritten().print(OS, Policy); 1815 OS << ">("; 1816 PrintExpr(Node->getSubExpr()); 1817 OS << ")"; 1818 } 1819 1820 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1821 VisitCXXNamedCastExpr(Node); 1822 } 1823 1824 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1825 VisitCXXNamedCastExpr(Node); 1826 } 1827 1828 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1829 VisitCXXNamedCastExpr(Node); 1830 } 1831 1832 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1833 VisitCXXNamedCastExpr(Node); 1834 } 1835 1836 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) { 1837 OS << "__builtin_bit_cast("; 1838 Node->getTypeInfoAsWritten()->getType().print(OS, Policy); 1839 OS << ", "; 1840 PrintExpr(Node->getSubExpr()); 1841 OS << ")"; 1842 } 1843 1844 void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) { 1845 VisitCXXNamedCastExpr(Node); 1846 } 1847 1848 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1849 OS << "typeid("; 1850 if (Node->isTypeOperand()) { 1851 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1852 } else { 1853 PrintExpr(Node->getExprOperand()); 1854 } 1855 OS << ")"; 1856 } 1857 1858 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1859 OS << "__uuidof("; 1860 if (Node->isTypeOperand()) { 1861 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1862 } else { 1863 PrintExpr(Node->getExprOperand()); 1864 } 1865 OS << ")"; 1866 } 1867 1868 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1869 PrintExpr(Node->getBaseExpr()); 1870 if (Node->isArrow()) 1871 OS << "->"; 1872 else 1873 OS << "."; 1874 if (NestedNameSpecifier *Qualifier = 1875 Node->getQualifierLoc().getNestedNameSpecifier()) 1876 Qualifier->print(OS, Policy); 1877 OS << Node->getPropertyDecl()->getDeclName(); 1878 } 1879 1880 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) { 1881 PrintExpr(Node->getBase()); 1882 OS << "["; 1883 PrintExpr(Node->getIdx()); 1884 OS << "]"; 1885 } 1886 1887 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1888 switch (Node->getLiteralOperatorKind()) { 1889 case UserDefinedLiteral::LOK_Raw: 1890 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1891 break; 1892 case UserDefinedLiteral::LOK_Template: { 1893 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1894 const TemplateArgumentList *Args = 1895 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1896 assert(Args); 1897 1898 if (Args->size() != 1) { 1899 const TemplateParameterList *TPL = nullptr; 1900 if (!DRE->hadMultipleCandidates()) 1901 if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl())) 1902 TPL = TD->getTemplateParameters(); 1903 OS << "operator\"\"" << Node->getUDSuffix()->getName(); 1904 printTemplateArgumentList(OS, Args->asArray(), Policy, TPL); 1905 OS << "()"; 1906 return; 1907 } 1908 1909 const TemplateArgument &Pack = Args->get(0); 1910 for (const auto &P : Pack.pack_elements()) { 1911 char C = (char)P.getAsIntegral().getZExtValue(); 1912 OS << C; 1913 } 1914 break; 1915 } 1916 case UserDefinedLiteral::LOK_Integer: { 1917 // Print integer literal without suffix. 1918 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1919 OS << toString(Int->getValue(), 10, /*isSigned*/false); 1920 break; 1921 } 1922 case UserDefinedLiteral::LOK_Floating: { 1923 // Print floating literal without suffix. 1924 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1925 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1926 break; 1927 } 1928 case UserDefinedLiteral::LOK_String: 1929 case UserDefinedLiteral::LOK_Character: 1930 PrintExpr(Node->getCookedLiteral()); 1931 break; 1932 } 1933 OS << Node->getUDSuffix()->getName(); 1934 } 1935 1936 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1937 OS << (Node->getValue() ? "true" : "false"); 1938 } 1939 1940 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1941 OS << "nullptr"; 1942 } 1943 1944 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1945 OS << "this"; 1946 } 1947 1948 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1949 if (!Node->getSubExpr()) 1950 OS << "throw"; 1951 else { 1952 OS << "throw "; 1953 PrintExpr(Node->getSubExpr()); 1954 } 1955 } 1956 1957 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1958 // Nothing to print: we picked up the default argument. 1959 } 1960 1961 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1962 // Nothing to print: we picked up the default initializer. 1963 } 1964 1965 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1966 auto TargetType = Node->getType(); 1967 auto *Auto = TargetType->getContainedDeducedType(); 1968 bool Bare = Auto && Auto->isDeduced(); 1969 1970 // Parenthesize deduced casts. 1971 if (Bare) 1972 OS << '('; 1973 TargetType.print(OS, Policy); 1974 if (Bare) 1975 OS << ')'; 1976 1977 // No extra braces surrounding the inner construct. 1978 if (!Node->isListInitialization()) 1979 OS << '('; 1980 PrintExpr(Node->getSubExpr()); 1981 if (!Node->isListInitialization()) 1982 OS << ')'; 1983 } 1984 1985 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1986 PrintExpr(Node->getSubExpr()); 1987 } 1988 1989 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1990 Node->getType().print(OS, Policy); 1991 if (Node->isStdInitListInitialization()) 1992 /* Nothing to do; braces are part of creating the std::initializer_list. */; 1993 else if (Node->isListInitialization()) 1994 OS << "{"; 1995 else 1996 OS << "("; 1997 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1998 ArgEnd = Node->arg_end(); 1999 Arg != ArgEnd; ++Arg) { 2000 if ((*Arg)->isDefaultArgument()) 2001 break; 2002 if (Arg != Node->arg_begin()) 2003 OS << ", "; 2004 PrintExpr(*Arg); 2005 } 2006 if (Node->isStdInitListInitialization()) 2007 /* See above. */; 2008 else if (Node->isListInitialization()) 2009 OS << "}"; 2010 else 2011 OS << ")"; 2012 } 2013 2014 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 2015 OS << '['; 2016 bool NeedComma = false; 2017 switch (Node->getCaptureDefault()) { 2018 case LCD_None: 2019 break; 2020 2021 case LCD_ByCopy: 2022 OS << '='; 2023 NeedComma = true; 2024 break; 2025 2026 case LCD_ByRef: 2027 OS << '&'; 2028 NeedComma = true; 2029 break; 2030 } 2031 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 2032 CEnd = Node->explicit_capture_end(); 2033 C != CEnd; 2034 ++C) { 2035 if (C->capturesVLAType()) 2036 continue; 2037 2038 if (NeedComma) 2039 OS << ", "; 2040 NeedComma = true; 2041 2042 switch (C->getCaptureKind()) { 2043 case LCK_This: 2044 OS << "this"; 2045 break; 2046 2047 case LCK_StarThis: 2048 OS << "*this"; 2049 break; 2050 2051 case LCK_ByRef: 2052 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C)) 2053 OS << '&'; 2054 OS << C->getCapturedVar()->getName(); 2055 break; 2056 2057 case LCK_ByCopy: 2058 OS << C->getCapturedVar()->getName(); 2059 break; 2060 2061 case LCK_VLAType: 2062 llvm_unreachable("VLA type in explicit captures."); 2063 } 2064 2065 if (C->isPackExpansion()) 2066 OS << "..."; 2067 2068 if (Node->isInitCapture(C)) { 2069 VarDecl *D = C->getCapturedVar(); 2070 2071 llvm::StringRef Pre; 2072 llvm::StringRef Post; 2073 if (D->getInitStyle() == VarDecl::CallInit && 2074 !isa<ParenListExpr>(D->getInit())) { 2075 Pre = "("; 2076 Post = ")"; 2077 } else if (D->getInitStyle() == VarDecl::CInit) { 2078 Pre = " = "; 2079 } 2080 2081 OS << Pre; 2082 PrintExpr(D->getInit()); 2083 OS << Post; 2084 } 2085 } 2086 OS << ']'; 2087 2088 if (!Node->getExplicitTemplateParameters().empty()) { 2089 Node->getTemplateParameterList()->print( 2090 OS, Node->getLambdaClass()->getASTContext(), 2091 /*OmitTemplateKW*/true); 2092 } 2093 2094 if (Node->hasExplicitParameters()) { 2095 OS << '('; 2096 CXXMethodDecl *Method = Node->getCallOperator(); 2097 NeedComma = false; 2098 for (const auto *P : Method->parameters()) { 2099 if (NeedComma) { 2100 OS << ", "; 2101 } else { 2102 NeedComma = true; 2103 } 2104 std::string ParamStr = 2105 (Policy.CleanUglifiedParameters && P->getIdentifier()) 2106 ? P->getIdentifier()->deuglifiedName().str() 2107 : P->getNameAsString(); 2108 P->getOriginalType().print(OS, Policy, ParamStr); 2109 } 2110 if (Method->isVariadic()) { 2111 if (NeedComma) 2112 OS << ", "; 2113 OS << "..."; 2114 } 2115 OS << ')'; 2116 2117 if (Node->isMutable()) 2118 OS << " mutable"; 2119 2120 auto *Proto = Method->getType()->castAs<FunctionProtoType>(); 2121 Proto->printExceptionSpecification(OS, Policy); 2122 2123 // FIXME: Attributes 2124 2125 // Print the trailing return type if it was specified in the source. 2126 if (Node->hasExplicitResultType()) { 2127 OS << " -> "; 2128 Proto->getReturnType().print(OS, Policy); 2129 } 2130 } 2131 2132 // Print the body. 2133 OS << ' '; 2134 if (Policy.TerseOutput) 2135 OS << "{}"; 2136 else 2137 PrintRawCompoundStmt(Node->getCompoundStmtBody()); 2138 } 2139 2140 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 2141 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 2142 TSInfo->getType().print(OS, Policy); 2143 else 2144 Node->getType().print(OS, Policy); 2145 OS << "()"; 2146 } 2147 2148 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 2149 if (E->isGlobalNew()) 2150 OS << "::"; 2151 OS << "new "; 2152 unsigned NumPlace = E->getNumPlacementArgs(); 2153 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 2154 OS << "("; 2155 PrintExpr(E->getPlacementArg(0)); 2156 for (unsigned i = 1; i < NumPlace; ++i) { 2157 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 2158 break; 2159 OS << ", "; 2160 PrintExpr(E->getPlacementArg(i)); 2161 } 2162 OS << ") "; 2163 } 2164 if (E->isParenTypeId()) 2165 OS << "("; 2166 std::string TypeS; 2167 if (E->isArray()) { 2168 llvm::raw_string_ostream s(TypeS); 2169 s << '['; 2170 if (Optional<Expr *> Size = E->getArraySize()) 2171 (*Size)->printPretty(s, Helper, Policy); 2172 s << ']'; 2173 } 2174 E->getAllocatedType().print(OS, Policy, TypeS); 2175 if (E->isParenTypeId()) 2176 OS << ")"; 2177 2178 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 2179 if (InitStyle != CXXNewExpr::NoInit) { 2180 bool Bare = InitStyle == CXXNewExpr::CallInit && 2181 !isa<ParenListExpr>(E->getInitializer()); 2182 if (Bare) 2183 OS << "("; 2184 PrintExpr(E->getInitializer()); 2185 if (Bare) 2186 OS << ")"; 2187 } 2188 } 2189 2190 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 2191 if (E->isGlobalDelete()) 2192 OS << "::"; 2193 OS << "delete "; 2194 if (E->isArrayForm()) 2195 OS << "[] "; 2196 PrintExpr(E->getArgument()); 2197 } 2198 2199 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 2200 PrintExpr(E->getBase()); 2201 if (E->isArrow()) 2202 OS << "->"; 2203 else 2204 OS << '.'; 2205 if (E->getQualifier()) 2206 E->getQualifier()->print(OS, Policy); 2207 OS << "~"; 2208 2209 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 2210 OS << II->getName(); 2211 else 2212 E->getDestroyedType().print(OS, Policy); 2213 } 2214 2215 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 2216 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2217 OS << "{"; 2218 2219 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 2220 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 2221 // Don't print any defaulted arguments 2222 break; 2223 } 2224 2225 if (i) OS << ", "; 2226 PrintExpr(E->getArg(i)); 2227 } 2228 2229 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2230 OS << "}"; 2231 } 2232 2233 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) { 2234 // Parens are printed by the surrounding context. 2235 OS << "<forwarded>"; 2236 } 2237 2238 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 2239 PrintExpr(E->getSubExpr()); 2240 } 2241 2242 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 2243 // Just forward to the subexpression. 2244 PrintExpr(E->getSubExpr()); 2245 } 2246 2247 void StmtPrinter::VisitCXXUnresolvedConstructExpr( 2248 CXXUnresolvedConstructExpr *Node) { 2249 Node->getTypeAsWritten().print(OS, Policy); 2250 if (!Node->isListInitialization()) 2251 OS << '('; 2252 for (auto Arg = Node->arg_begin(), ArgEnd = Node->arg_end(); Arg != ArgEnd; 2253 ++Arg) { 2254 if (Arg != Node->arg_begin()) 2255 OS << ", "; 2256 PrintExpr(*Arg); 2257 } 2258 if (!Node->isListInitialization()) 2259 OS << ')'; 2260 } 2261 2262 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 2263 CXXDependentScopeMemberExpr *Node) { 2264 if (!Node->isImplicitAccess()) { 2265 PrintExpr(Node->getBase()); 2266 OS << (Node->isArrow() ? "->" : "."); 2267 } 2268 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2269 Qualifier->print(OS, Policy); 2270 if (Node->hasTemplateKeyword()) 2271 OS << "template "; 2272 OS << Node->getMemberNameInfo(); 2273 if (Node->hasExplicitTemplateArgs()) 2274 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 2275 } 2276 2277 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 2278 if (!Node->isImplicitAccess()) { 2279 PrintExpr(Node->getBase()); 2280 OS << (Node->isArrow() ? "->" : "."); 2281 } 2282 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2283 Qualifier->print(OS, Policy); 2284 if (Node->hasTemplateKeyword()) 2285 OS << "template "; 2286 OS << Node->getMemberNameInfo(); 2287 if (Node->hasExplicitTemplateArgs()) 2288 printTemplateArgumentList(OS, Node->template_arguments(), Policy); 2289 } 2290 2291 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 2292 OS << getTraitSpelling(E->getTrait()) << "("; 2293 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 2294 if (I > 0) 2295 OS << ", "; 2296 E->getArg(I)->getType().print(OS, Policy); 2297 } 2298 OS << ")"; 2299 } 2300 2301 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2302 OS << getTraitSpelling(E->getTrait()) << '('; 2303 E->getQueriedType().print(OS, Policy); 2304 OS << ')'; 2305 } 2306 2307 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2308 OS << getTraitSpelling(E->getTrait()) << '('; 2309 PrintExpr(E->getQueriedExpression()); 2310 OS << ')'; 2311 } 2312 2313 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 2314 OS << "noexcept("; 2315 PrintExpr(E->getOperand()); 2316 OS << ")"; 2317 } 2318 2319 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 2320 PrintExpr(E->getPattern()); 2321 OS << "..."; 2322 } 2323 2324 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2325 OS << "sizeof...(" << *E->getPack() << ")"; 2326 } 2327 2328 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 2329 SubstNonTypeTemplateParmPackExpr *Node) { 2330 OS << *Node->getParameterPack(); 2331 } 2332 2333 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 2334 SubstNonTypeTemplateParmExpr *Node) { 2335 Visit(Node->getReplacement()); 2336 } 2337 2338 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 2339 OS << *E->getParameterPack(); 2340 } 2341 2342 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 2343 PrintExpr(Node->getSubExpr()); 2344 } 2345 2346 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 2347 OS << "("; 2348 if (E->getLHS()) { 2349 PrintExpr(E->getLHS()); 2350 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2351 } 2352 OS << "..."; 2353 if (E->getRHS()) { 2354 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2355 PrintExpr(E->getRHS()); 2356 } 2357 OS << ")"; 2358 } 2359 2360 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) { 2361 NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc(); 2362 if (NNS) 2363 NNS.getNestedNameSpecifier()->print(OS, Policy); 2364 if (E->getTemplateKWLoc().isValid()) 2365 OS << "template "; 2366 OS << E->getFoundDecl()->getName(); 2367 printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(), 2368 Policy, 2369 E->getNamedConcept()->getTemplateParameters()); 2370 } 2371 2372 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) { 2373 OS << "requires "; 2374 auto LocalParameters = E->getLocalParameters(); 2375 if (!LocalParameters.empty()) { 2376 OS << "("; 2377 for (ParmVarDecl *LocalParam : LocalParameters) { 2378 PrintRawDecl(LocalParam); 2379 if (LocalParam != LocalParameters.back()) 2380 OS << ", "; 2381 } 2382 2383 OS << ") "; 2384 } 2385 OS << "{ "; 2386 auto Requirements = E->getRequirements(); 2387 for (concepts::Requirement *Req : Requirements) { 2388 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) { 2389 if (TypeReq->isSubstitutionFailure()) 2390 OS << "<<error-type>>"; 2391 else 2392 TypeReq->getType()->getType().print(OS, Policy); 2393 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) { 2394 if (ExprReq->isCompound()) 2395 OS << "{ "; 2396 if (ExprReq->isExprSubstitutionFailure()) 2397 OS << "<<error-expression>>"; 2398 else 2399 PrintExpr(ExprReq->getExpr()); 2400 if (ExprReq->isCompound()) { 2401 OS << " }"; 2402 if (ExprReq->getNoexceptLoc().isValid()) 2403 OS << " noexcept"; 2404 const auto &RetReq = ExprReq->getReturnTypeRequirement(); 2405 if (!RetReq.isEmpty()) { 2406 OS << " -> "; 2407 if (RetReq.isSubstitutionFailure()) 2408 OS << "<<error-type>>"; 2409 else if (RetReq.isTypeConstraint()) 2410 RetReq.getTypeConstraint()->print(OS, Policy); 2411 } 2412 } 2413 } else { 2414 auto *NestedReq = cast<concepts::NestedRequirement>(Req); 2415 OS << "requires "; 2416 if (NestedReq->isSubstitutionFailure()) 2417 OS << "<<error-expression>>"; 2418 else 2419 PrintExpr(NestedReq->getConstraintExpr()); 2420 } 2421 OS << "; "; 2422 } 2423 OS << "}"; 2424 } 2425 2426 // C++ Coroutines TS 2427 2428 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) { 2429 Visit(S->getBody()); 2430 } 2431 2432 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) { 2433 OS << "co_return"; 2434 if (S->getOperand()) { 2435 OS << " "; 2436 Visit(S->getOperand()); 2437 } 2438 OS << ";"; 2439 } 2440 2441 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) { 2442 OS << "co_await "; 2443 PrintExpr(S->getOperand()); 2444 } 2445 2446 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) { 2447 OS << "co_await "; 2448 PrintExpr(S->getOperand()); 2449 } 2450 2451 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) { 2452 OS << "co_yield "; 2453 PrintExpr(S->getOperand()); 2454 } 2455 2456 // Obj-C 2457 2458 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 2459 OS << "@"; 2460 VisitStringLiteral(Node->getString()); 2461 } 2462 2463 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 2464 OS << "@"; 2465 Visit(E->getSubExpr()); 2466 } 2467 2468 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 2469 OS << "@[ "; 2470 ObjCArrayLiteral::child_range Ch = E->children(); 2471 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) { 2472 if (I != Ch.begin()) 2473 OS << ", "; 2474 Visit(*I); 2475 } 2476 OS << " ]"; 2477 } 2478 2479 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 2480 OS << "@{ "; 2481 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 2482 if (I > 0) 2483 OS << ", "; 2484 2485 ObjCDictionaryElement Element = E->getKeyValueElement(I); 2486 Visit(Element.Key); 2487 OS << " : "; 2488 Visit(Element.Value); 2489 if (Element.isPackExpansion()) 2490 OS << "..."; 2491 } 2492 OS << " }"; 2493 } 2494 2495 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 2496 OS << "@encode("; 2497 Node->getEncodedType().print(OS, Policy); 2498 OS << ')'; 2499 } 2500 2501 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 2502 OS << "@selector("; 2503 Node->getSelector().print(OS); 2504 OS << ')'; 2505 } 2506 2507 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 2508 OS << "@protocol(" << *Node->getProtocol() << ')'; 2509 } 2510 2511 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 2512 OS << "["; 2513 switch (Mess->getReceiverKind()) { 2514 case ObjCMessageExpr::Instance: 2515 PrintExpr(Mess->getInstanceReceiver()); 2516 break; 2517 2518 case ObjCMessageExpr::Class: 2519 Mess->getClassReceiver().print(OS, Policy); 2520 break; 2521 2522 case ObjCMessageExpr::SuperInstance: 2523 case ObjCMessageExpr::SuperClass: 2524 OS << "Super"; 2525 break; 2526 } 2527 2528 OS << ' '; 2529 Selector selector = Mess->getSelector(); 2530 if (selector.isUnarySelector()) { 2531 OS << selector.getNameForSlot(0); 2532 } else { 2533 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 2534 if (i < selector.getNumArgs()) { 2535 if (i > 0) OS << ' '; 2536 if (selector.getIdentifierInfoForSlot(i)) 2537 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 2538 else 2539 OS << ":"; 2540 } 2541 else OS << ", "; // Handle variadic methods. 2542 2543 PrintExpr(Mess->getArg(i)); 2544 } 2545 } 2546 OS << "]"; 2547 } 2548 2549 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 2550 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 2551 } 2552 2553 void 2554 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2555 PrintExpr(E->getSubExpr()); 2556 } 2557 2558 void 2559 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2560 OS << '(' << E->getBridgeKindName(); 2561 E->getType().print(OS, Policy); 2562 OS << ')'; 2563 PrintExpr(E->getSubExpr()); 2564 } 2565 2566 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2567 BlockDecl *BD = Node->getBlockDecl(); 2568 OS << "^"; 2569 2570 const FunctionType *AFT = Node->getFunctionType(); 2571 2572 if (isa<FunctionNoProtoType>(AFT)) { 2573 OS << "()"; 2574 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2575 OS << '('; 2576 for (BlockDecl::param_iterator AI = BD->param_begin(), 2577 E = BD->param_end(); AI != E; ++AI) { 2578 if (AI != BD->param_begin()) OS << ", "; 2579 std::string ParamStr = (*AI)->getNameAsString(); 2580 (*AI)->getType().print(OS, Policy, ParamStr); 2581 } 2582 2583 const auto *FT = cast<FunctionProtoType>(AFT); 2584 if (FT->isVariadic()) { 2585 if (!BD->param_empty()) OS << ", "; 2586 OS << "..."; 2587 } 2588 OS << ')'; 2589 } 2590 OS << "{ }"; 2591 } 2592 2593 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2594 PrintExpr(Node->getSourceExpr()); 2595 } 2596 2597 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 2598 // TODO: Print something reasonable for a TypoExpr, if necessary. 2599 llvm_unreachable("Cannot print TypoExpr nodes"); 2600 } 2601 2602 void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) { 2603 OS << "<recovery-expr>("; 2604 const char *Sep = ""; 2605 for (Expr *E : Node->subExpressions()) { 2606 OS << Sep; 2607 PrintExpr(E); 2608 Sep = ", "; 2609 } 2610 OS << ')'; 2611 } 2612 2613 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2614 OS << "__builtin_astype("; 2615 PrintExpr(Node->getSrcExpr()); 2616 OS << ", "; 2617 Node->getType().print(OS, Policy); 2618 OS << ")"; 2619 } 2620 2621 //===----------------------------------------------------------------------===// 2622 // Stmt method implementations 2623 //===----------------------------------------------------------------------===// 2624 2625 void Stmt::dumpPretty(const ASTContext &Context) const { 2626 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2627 } 2628 2629 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper, 2630 const PrintingPolicy &Policy, unsigned Indentation, 2631 StringRef NL, const ASTContext *Context) const { 2632 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context); 2633 P.Visit(const_cast<Stmt *>(this)); 2634 } 2635 2636 void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper, 2637 const PrintingPolicy &Policy, 2638 unsigned Indentation, StringRef NL, 2639 const ASTContext *Context) const { 2640 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context); 2641 P.PrintControlledStmt(const_cast<Stmt *>(this)); 2642 } 2643 2644 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper, 2645 const PrintingPolicy &Policy, bool AddQuotes) const { 2646 std::string Buf; 2647 llvm::raw_string_ostream TempOut(Buf); 2648 2649 printPretty(TempOut, Helper, Policy); 2650 2651 Out << JsonFormat(TempOut.str(), AddQuotes); 2652 } 2653 2654 //===----------------------------------------------------------------------===// 2655 // PrinterHelper 2656 //===----------------------------------------------------------------------===// 2657 2658 // Implement virtual destructor. 2659 PrinterHelper::~PrinterHelper() = default; 2660