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