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