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