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