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