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