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