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