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