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