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