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