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