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