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