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