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