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