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 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) { 1202 Indent() << "#pragma omp target simd "; 1203 PrintOMPExecutableDirective(Node); 1204 } 1205 1206 void StmtPrinter::VisitOMPTeamsDistributeDirective( 1207 OMPTeamsDistributeDirective *Node) { 1208 Indent() << "#pragma omp teams distribute "; 1209 PrintOMPExecutableDirective(Node); 1210 } 1211 1212 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective( 1213 OMPTeamsDistributeSimdDirective *Node) { 1214 Indent() << "#pragma omp teams distribute simd "; 1215 PrintOMPExecutableDirective(Node); 1216 } 1217 1218 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective( 1219 OMPTeamsDistributeParallelForSimdDirective *Node) { 1220 Indent() << "#pragma omp teams distribute parallel for simd "; 1221 PrintOMPExecutableDirective(Node); 1222 } 1223 1224 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective( 1225 OMPTeamsDistributeParallelForDirective *Node) { 1226 Indent() << "#pragma omp teams distribute parallel for "; 1227 PrintOMPExecutableDirective(Node); 1228 } 1229 1230 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) { 1231 Indent() << "#pragma omp target teams "; 1232 PrintOMPExecutableDirective(Node); 1233 } 1234 1235 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective( 1236 OMPTargetTeamsDistributeDirective *Node) { 1237 Indent() << "#pragma omp target teams distribute "; 1238 PrintOMPExecutableDirective(Node); 1239 } 1240 1241 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective( 1242 OMPTargetTeamsDistributeParallelForDirective *Node) { 1243 Indent() << "#pragma omp target teams distribute parallel for "; 1244 PrintOMPExecutableDirective(Node); 1245 } 1246 1247 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective( 1248 OMPTargetTeamsDistributeParallelForSimdDirective *Node) { 1249 Indent() << "#pragma omp target teams distribute parallel for simd "; 1250 PrintOMPExecutableDirective(Node); 1251 } 1252 1253 //===----------------------------------------------------------------------===// 1254 // Expr printing methods. 1255 //===----------------------------------------------------------------------===// 1256 1257 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 1258 if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) { 1259 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy); 1260 return; 1261 } 1262 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1263 Qualifier->print(OS, Policy); 1264 if (Node->hasTemplateKeyword()) 1265 OS << "template "; 1266 OS << Node->getNameInfo(); 1267 if (Node->hasExplicitTemplateArgs()) 1268 TemplateSpecializationType::PrintTemplateArgumentList( 1269 OS, Node->template_arguments(), Policy); 1270 } 1271 1272 void StmtPrinter::VisitDependentScopeDeclRefExpr( 1273 DependentScopeDeclRefExpr *Node) { 1274 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1275 Qualifier->print(OS, Policy); 1276 if (Node->hasTemplateKeyword()) 1277 OS << "template "; 1278 OS << Node->getNameInfo(); 1279 if (Node->hasExplicitTemplateArgs()) 1280 TemplateSpecializationType::PrintTemplateArgumentList( 1281 OS, Node->template_arguments(), Policy); 1282 } 1283 1284 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 1285 if (Node->getQualifier()) 1286 Node->getQualifier()->print(OS, Policy); 1287 if (Node->hasTemplateKeyword()) 1288 OS << "template "; 1289 OS << Node->getNameInfo(); 1290 if (Node->hasExplicitTemplateArgs()) 1291 TemplateSpecializationType::PrintTemplateArgumentList( 1292 OS, Node->template_arguments(), Policy); 1293 } 1294 1295 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 1296 if (Node->getBase()) { 1297 PrintExpr(Node->getBase()); 1298 OS << (Node->isArrow() ? "->" : "."); 1299 } 1300 OS << *Node->getDecl(); 1301 } 1302 1303 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 1304 if (Node->isSuperReceiver()) 1305 OS << "super."; 1306 else if (Node->isObjectReceiver() && Node->getBase()) { 1307 PrintExpr(Node->getBase()); 1308 OS << "."; 1309 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 1310 OS << Node->getClassReceiver()->getName() << "."; 1311 } 1312 1313 if (Node->isImplicitProperty()) 1314 Node->getImplicitPropertyGetter()->getSelector().print(OS); 1315 else 1316 OS << Node->getExplicitProperty()->getName(); 1317 } 1318 1319 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 1320 1321 PrintExpr(Node->getBaseExpr()); 1322 OS << "["; 1323 PrintExpr(Node->getKeyExpr()); 1324 OS << "]"; 1325 } 1326 1327 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 1328 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType()); 1329 } 1330 1331 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 1332 unsigned value = Node->getValue(); 1333 1334 switch (Node->getKind()) { 1335 case CharacterLiteral::Ascii: break; // no prefix. 1336 case CharacterLiteral::Wide: OS << 'L'; break; 1337 case CharacterLiteral::UTF8: OS << "u8"; break; 1338 case CharacterLiteral::UTF16: OS << 'u'; break; 1339 case CharacterLiteral::UTF32: OS << 'U'; break; 1340 } 1341 1342 switch (value) { 1343 case '\\': 1344 OS << "'\\\\'"; 1345 break; 1346 case '\'': 1347 OS << "'\\''"; 1348 break; 1349 case '\a': 1350 // TODO: K&R: the meaning of '\\a' is different in traditional C 1351 OS << "'\\a'"; 1352 break; 1353 case '\b': 1354 OS << "'\\b'"; 1355 break; 1356 // Nonstandard escape sequence. 1357 /*case '\e': 1358 OS << "'\\e'"; 1359 break;*/ 1360 case '\f': 1361 OS << "'\\f'"; 1362 break; 1363 case '\n': 1364 OS << "'\\n'"; 1365 break; 1366 case '\r': 1367 OS << "'\\r'"; 1368 break; 1369 case '\t': 1370 OS << "'\\t'"; 1371 break; 1372 case '\v': 1373 OS << "'\\v'"; 1374 break; 1375 default: 1376 // A character literal might be sign-extended, which 1377 // would result in an invalid \U escape sequence. 1378 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF' 1379 // are not correctly handled. 1380 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii) 1381 value &= 0xFFu; 1382 if (value < 256 && isPrintable((unsigned char)value)) 1383 OS << "'" << (char)value << "'"; 1384 else if (value < 256) 1385 OS << "'\\x" << llvm::format("%02x", value) << "'"; 1386 else if (value <= 0xFFFF) 1387 OS << "'\\u" << llvm::format("%04x", value) << "'"; 1388 else 1389 OS << "'\\U" << llvm::format("%08x", value) << "'"; 1390 } 1391 } 1392 1393 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 1394 bool isSigned = Node->getType()->isSignedIntegerType(); 1395 OS << Node->getValue().toString(10, isSigned); 1396 1397 // Emit suffixes. Integer literals are always a builtin integer type. 1398 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1399 default: llvm_unreachable("Unexpected type for integer literal!"); 1400 case BuiltinType::Char_S: 1401 case BuiltinType::Char_U: OS << "i8"; break; 1402 case BuiltinType::UChar: OS << "Ui8"; break; 1403 case BuiltinType::Short: OS << "i16"; break; 1404 case BuiltinType::UShort: OS << "Ui16"; break; 1405 case BuiltinType::Int: break; // no suffix. 1406 case BuiltinType::UInt: OS << 'U'; break; 1407 case BuiltinType::Long: OS << 'L'; break; 1408 case BuiltinType::ULong: OS << "UL"; break; 1409 case BuiltinType::LongLong: OS << "LL"; break; 1410 case BuiltinType::ULongLong: OS << "ULL"; break; 1411 } 1412 } 1413 1414 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 1415 bool PrintSuffix) { 1416 SmallString<16> Str; 1417 Node->getValue().toString(Str); 1418 OS << Str; 1419 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 1420 OS << '.'; // Trailing dot in order to separate from ints. 1421 1422 if (!PrintSuffix) 1423 return; 1424 1425 // Emit suffixes. Float literals are always a builtin float type. 1426 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1427 default: llvm_unreachable("Unexpected type for float literal!"); 1428 case BuiltinType::Half: break; // FIXME: suffix? 1429 case BuiltinType::Double: break; // no suffix. 1430 case BuiltinType::Float: OS << 'F'; break; 1431 case BuiltinType::LongDouble: OS << 'L'; break; 1432 case BuiltinType::Float128: OS << 'Q'; break; 1433 } 1434 } 1435 1436 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 1437 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 1438 } 1439 1440 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 1441 PrintExpr(Node->getSubExpr()); 1442 OS << "i"; 1443 } 1444 1445 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 1446 Str->outputString(OS); 1447 } 1448 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 1449 OS << "("; 1450 PrintExpr(Node->getSubExpr()); 1451 OS << ")"; 1452 } 1453 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1454 if (!Node->isPostfix()) { 1455 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1456 1457 // Print a space if this is an "identifier operator" like __real, or if 1458 // it might be concatenated incorrectly like '+'. 1459 switch (Node->getOpcode()) { 1460 default: break; 1461 case UO_Real: 1462 case UO_Imag: 1463 case UO_Extension: 1464 OS << ' '; 1465 break; 1466 case UO_Plus: 1467 case UO_Minus: 1468 if (isa<UnaryOperator>(Node->getSubExpr())) 1469 OS << ' '; 1470 break; 1471 } 1472 } 1473 PrintExpr(Node->getSubExpr()); 1474 1475 if (Node->isPostfix()) 1476 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1477 } 1478 1479 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1480 OS << "__builtin_offsetof("; 1481 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1482 OS << ", "; 1483 bool PrintedSomething = false; 1484 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1485 OffsetOfNode ON = Node->getComponent(i); 1486 if (ON.getKind() == OffsetOfNode::Array) { 1487 // Array node 1488 OS << "["; 1489 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1490 OS << "]"; 1491 PrintedSomething = true; 1492 continue; 1493 } 1494 1495 // Skip implicit base indirections. 1496 if (ON.getKind() == OffsetOfNode::Base) 1497 continue; 1498 1499 // Field or identifier node. 1500 IdentifierInfo *Id = ON.getFieldName(); 1501 if (!Id) 1502 continue; 1503 1504 if (PrintedSomething) 1505 OS << "."; 1506 else 1507 PrintedSomething = true; 1508 OS << Id->getName(); 1509 } 1510 OS << ")"; 1511 } 1512 1513 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 1514 switch(Node->getKind()) { 1515 case UETT_SizeOf: 1516 OS << "sizeof"; 1517 break; 1518 case UETT_AlignOf: 1519 if (Policy.Alignof) 1520 OS << "alignof"; 1521 else if (Policy.UnderscoreAlignof) 1522 OS << "_Alignof"; 1523 else 1524 OS << "__alignof"; 1525 break; 1526 case UETT_VecStep: 1527 OS << "vec_step"; 1528 break; 1529 case UETT_OpenMPRequiredSimdAlign: 1530 OS << "__builtin_omp_required_simd_align"; 1531 break; 1532 } 1533 if (Node->isArgumentType()) { 1534 OS << '('; 1535 Node->getArgumentType().print(OS, Policy); 1536 OS << ')'; 1537 } else { 1538 OS << " "; 1539 PrintExpr(Node->getArgumentExpr()); 1540 } 1541 } 1542 1543 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1544 OS << "_Generic("; 1545 PrintExpr(Node->getControllingExpr()); 1546 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 1547 OS << ", "; 1548 QualType T = Node->getAssocType(i); 1549 if (T.isNull()) 1550 OS << "default"; 1551 else 1552 T.print(OS, Policy); 1553 OS << ": "; 1554 PrintExpr(Node->getAssocExpr(i)); 1555 } 1556 OS << ")"; 1557 } 1558 1559 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1560 PrintExpr(Node->getLHS()); 1561 OS << "["; 1562 PrintExpr(Node->getRHS()); 1563 OS << "]"; 1564 } 1565 1566 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) { 1567 PrintExpr(Node->getBase()); 1568 OS << "["; 1569 if (Node->getLowerBound()) 1570 PrintExpr(Node->getLowerBound()); 1571 if (Node->getColonLoc().isValid()) { 1572 OS << ":"; 1573 if (Node->getLength()) 1574 PrintExpr(Node->getLength()); 1575 } 1576 OS << "]"; 1577 } 1578 1579 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1580 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1581 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1582 // Don't print any defaulted arguments 1583 break; 1584 } 1585 1586 if (i) OS << ", "; 1587 PrintExpr(Call->getArg(i)); 1588 } 1589 } 1590 1591 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1592 PrintExpr(Call->getCallee()); 1593 OS << "("; 1594 PrintCallArgs(Call); 1595 OS << ")"; 1596 } 1597 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1598 // FIXME: Suppress printing implicit bases (like "this") 1599 PrintExpr(Node->getBase()); 1600 1601 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1602 FieldDecl *ParentDecl = ParentMember 1603 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr; 1604 1605 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1606 OS << (Node->isArrow() ? "->" : "."); 1607 1608 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1609 if (FD->isAnonymousStructOrUnion()) 1610 return; 1611 1612 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1613 Qualifier->print(OS, Policy); 1614 if (Node->hasTemplateKeyword()) 1615 OS << "template "; 1616 OS << Node->getMemberNameInfo(); 1617 if (Node->hasExplicitTemplateArgs()) 1618 TemplateSpecializationType::PrintTemplateArgumentList( 1619 OS, Node->template_arguments(), Policy); 1620 } 1621 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1622 PrintExpr(Node->getBase()); 1623 OS << (Node->isArrow() ? "->isa" : ".isa"); 1624 } 1625 1626 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1627 PrintExpr(Node->getBase()); 1628 OS << "."; 1629 OS << Node->getAccessor().getName(); 1630 } 1631 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1632 OS << '('; 1633 Node->getTypeAsWritten().print(OS, Policy); 1634 OS << ')'; 1635 PrintExpr(Node->getSubExpr()); 1636 } 1637 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1638 OS << '('; 1639 Node->getType().print(OS, Policy); 1640 OS << ')'; 1641 PrintExpr(Node->getInitializer()); 1642 } 1643 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1644 // No need to print anything, simply forward to the subexpression. 1645 PrintExpr(Node->getSubExpr()); 1646 } 1647 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1648 PrintExpr(Node->getLHS()); 1649 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1650 PrintExpr(Node->getRHS()); 1651 } 1652 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1653 PrintExpr(Node->getLHS()); 1654 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1655 PrintExpr(Node->getRHS()); 1656 } 1657 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1658 PrintExpr(Node->getCond()); 1659 OS << " ? "; 1660 PrintExpr(Node->getLHS()); 1661 OS << " : "; 1662 PrintExpr(Node->getRHS()); 1663 } 1664 1665 // GNU extensions. 1666 1667 void 1668 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1669 PrintExpr(Node->getCommon()); 1670 OS << " ?: "; 1671 PrintExpr(Node->getFalseExpr()); 1672 } 1673 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1674 OS << "&&" << Node->getLabel()->getName(); 1675 } 1676 1677 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1678 OS << "("; 1679 PrintRawCompoundStmt(E->getSubStmt()); 1680 OS << ")"; 1681 } 1682 1683 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1684 OS << "__builtin_choose_expr("; 1685 PrintExpr(Node->getCond()); 1686 OS << ", "; 1687 PrintExpr(Node->getLHS()); 1688 OS << ", "; 1689 PrintExpr(Node->getRHS()); 1690 OS << ")"; 1691 } 1692 1693 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1694 OS << "__null"; 1695 } 1696 1697 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1698 OS << "__builtin_shufflevector("; 1699 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1700 if (i) OS << ", "; 1701 PrintExpr(Node->getExpr(i)); 1702 } 1703 OS << ")"; 1704 } 1705 1706 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1707 OS << "__builtin_convertvector("; 1708 PrintExpr(Node->getSrcExpr()); 1709 OS << ", "; 1710 Node->getType().print(OS, Policy); 1711 OS << ")"; 1712 } 1713 1714 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1715 if (Node->getSyntacticForm()) { 1716 Visit(Node->getSyntacticForm()); 1717 return; 1718 } 1719 1720 OS << "{"; 1721 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1722 if (i) OS << ", "; 1723 if (Node->getInit(i)) 1724 PrintExpr(Node->getInit(i)); 1725 else 1726 OS << "{}"; 1727 } 1728 OS << "}"; 1729 } 1730 1731 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) { 1732 // There's no way to express this expression in any of our supported 1733 // languages, so just emit something terse and (hopefully) clear. 1734 OS << "{"; 1735 PrintExpr(Node->getSubExpr()); 1736 OS << "}"; 1737 } 1738 1739 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) { 1740 OS << "*"; 1741 } 1742 1743 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1744 OS << "("; 1745 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1746 if (i) OS << ", "; 1747 PrintExpr(Node->getExpr(i)); 1748 } 1749 OS << ")"; 1750 } 1751 1752 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1753 bool NeedsEquals = true; 1754 for (const DesignatedInitExpr::Designator &D : Node->designators()) { 1755 if (D.isFieldDesignator()) { 1756 if (D.getDotLoc().isInvalid()) { 1757 if (IdentifierInfo *II = D.getFieldName()) { 1758 OS << II->getName() << ":"; 1759 NeedsEquals = false; 1760 } 1761 } else { 1762 OS << "." << D.getFieldName()->getName(); 1763 } 1764 } else { 1765 OS << "["; 1766 if (D.isArrayDesignator()) { 1767 PrintExpr(Node->getArrayIndex(D)); 1768 } else { 1769 PrintExpr(Node->getArrayRangeStart(D)); 1770 OS << " ... "; 1771 PrintExpr(Node->getArrayRangeEnd(D)); 1772 } 1773 OS << "]"; 1774 } 1775 } 1776 1777 if (NeedsEquals) 1778 OS << " = "; 1779 else 1780 OS << " "; 1781 PrintExpr(Node->getInit()); 1782 } 1783 1784 void StmtPrinter::VisitDesignatedInitUpdateExpr( 1785 DesignatedInitUpdateExpr *Node) { 1786 OS << "{"; 1787 OS << "/*base*/"; 1788 PrintExpr(Node->getBase()); 1789 OS << ", "; 1790 1791 OS << "/*updater*/"; 1792 PrintExpr(Node->getUpdater()); 1793 OS << "}"; 1794 } 1795 1796 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) { 1797 OS << "/*no init*/"; 1798 } 1799 1800 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1801 if (Node->getType()->getAsCXXRecordDecl()) { 1802 OS << "/*implicit*/"; 1803 Node->getType().print(OS, Policy); 1804 OS << "()"; 1805 } else { 1806 OS << "/*implicit*/("; 1807 Node->getType().print(OS, Policy); 1808 OS << ')'; 1809 if (Node->getType()->isRecordType()) 1810 OS << "{}"; 1811 else 1812 OS << 0; 1813 } 1814 } 1815 1816 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1817 OS << "__builtin_va_arg("; 1818 PrintExpr(Node->getSubExpr()); 1819 OS << ", "; 1820 Node->getType().print(OS, Policy); 1821 OS << ")"; 1822 } 1823 1824 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1825 PrintExpr(Node->getSyntacticForm()); 1826 } 1827 1828 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1829 const char *Name = nullptr; 1830 switch (Node->getOp()) { 1831 #define BUILTIN(ID, TYPE, ATTRS) 1832 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1833 case AtomicExpr::AO ## ID: \ 1834 Name = #ID "("; \ 1835 break; 1836 #include "clang/Basic/Builtins.def" 1837 } 1838 OS << Name; 1839 1840 // AtomicExpr stores its subexpressions in a permuted order. 1841 PrintExpr(Node->getPtr()); 1842 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1843 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1844 OS << ", "; 1845 PrintExpr(Node->getVal1()); 1846 } 1847 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1848 Node->isCmpXChg()) { 1849 OS << ", "; 1850 PrintExpr(Node->getVal2()); 1851 } 1852 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1853 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1854 OS << ", "; 1855 PrintExpr(Node->getWeak()); 1856 } 1857 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1858 OS << ", "; 1859 PrintExpr(Node->getOrder()); 1860 } 1861 if (Node->isCmpXChg()) { 1862 OS << ", "; 1863 PrintExpr(Node->getOrderFail()); 1864 } 1865 OS << ")"; 1866 } 1867 1868 // C++ 1869 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1870 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1871 "", 1872 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1873 Spelling, 1874 #include "clang/Basic/OperatorKinds.def" 1875 }; 1876 1877 OverloadedOperatorKind Kind = Node->getOperator(); 1878 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1879 if (Node->getNumArgs() == 1) { 1880 OS << OpStrings[Kind] << ' '; 1881 PrintExpr(Node->getArg(0)); 1882 } else { 1883 PrintExpr(Node->getArg(0)); 1884 OS << ' ' << OpStrings[Kind]; 1885 } 1886 } else if (Kind == OO_Arrow) { 1887 PrintExpr(Node->getArg(0)); 1888 } else if (Kind == OO_Call) { 1889 PrintExpr(Node->getArg(0)); 1890 OS << '('; 1891 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1892 if (ArgIdx > 1) 1893 OS << ", "; 1894 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1895 PrintExpr(Node->getArg(ArgIdx)); 1896 } 1897 OS << ')'; 1898 } else if (Kind == OO_Subscript) { 1899 PrintExpr(Node->getArg(0)); 1900 OS << '['; 1901 PrintExpr(Node->getArg(1)); 1902 OS << ']'; 1903 } else if (Node->getNumArgs() == 1) { 1904 OS << OpStrings[Kind] << ' '; 1905 PrintExpr(Node->getArg(0)); 1906 } else if (Node->getNumArgs() == 2) { 1907 PrintExpr(Node->getArg(0)); 1908 OS << ' ' << OpStrings[Kind] << ' '; 1909 PrintExpr(Node->getArg(1)); 1910 } else { 1911 llvm_unreachable("unknown overloaded operator"); 1912 } 1913 } 1914 1915 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1916 // If we have a conversion operator call only print the argument. 1917 CXXMethodDecl *MD = Node->getMethodDecl(); 1918 if (MD && isa<CXXConversionDecl>(MD)) { 1919 PrintExpr(Node->getImplicitObjectArgument()); 1920 return; 1921 } 1922 VisitCallExpr(cast<CallExpr>(Node)); 1923 } 1924 1925 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1926 PrintExpr(Node->getCallee()); 1927 OS << "<<<"; 1928 PrintCallArgs(Node->getConfig()); 1929 OS << ">>>("; 1930 PrintCallArgs(Node); 1931 OS << ")"; 1932 } 1933 1934 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1935 OS << Node->getCastName() << '<'; 1936 Node->getTypeAsWritten().print(OS, Policy); 1937 OS << ">("; 1938 PrintExpr(Node->getSubExpr()); 1939 OS << ")"; 1940 } 1941 1942 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1943 VisitCXXNamedCastExpr(Node); 1944 } 1945 1946 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1947 VisitCXXNamedCastExpr(Node); 1948 } 1949 1950 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1951 VisitCXXNamedCastExpr(Node); 1952 } 1953 1954 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1955 VisitCXXNamedCastExpr(Node); 1956 } 1957 1958 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1959 OS << "typeid("; 1960 if (Node->isTypeOperand()) { 1961 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1962 } else { 1963 PrintExpr(Node->getExprOperand()); 1964 } 1965 OS << ")"; 1966 } 1967 1968 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1969 OS << "__uuidof("; 1970 if (Node->isTypeOperand()) { 1971 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1972 } else { 1973 PrintExpr(Node->getExprOperand()); 1974 } 1975 OS << ")"; 1976 } 1977 1978 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1979 PrintExpr(Node->getBaseExpr()); 1980 if (Node->isArrow()) 1981 OS << "->"; 1982 else 1983 OS << "."; 1984 if (NestedNameSpecifier *Qualifier = 1985 Node->getQualifierLoc().getNestedNameSpecifier()) 1986 Qualifier->print(OS, Policy); 1987 OS << Node->getPropertyDecl()->getDeclName(); 1988 } 1989 1990 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) { 1991 PrintExpr(Node->getBase()); 1992 OS << "["; 1993 PrintExpr(Node->getIdx()); 1994 OS << "]"; 1995 } 1996 1997 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1998 switch (Node->getLiteralOperatorKind()) { 1999 case UserDefinedLiteral::LOK_Raw: 2000 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 2001 break; 2002 case UserDefinedLiteral::LOK_Template: { 2003 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 2004 const TemplateArgumentList *Args = 2005 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 2006 assert(Args); 2007 2008 if (Args->size() != 1) { 2009 OS << "operator\"\"" << Node->getUDSuffix()->getName(); 2010 TemplateSpecializationType::PrintTemplateArgumentList( 2011 OS, Args->asArray(), Policy); 2012 OS << "()"; 2013 return; 2014 } 2015 2016 const TemplateArgument &Pack = Args->get(0); 2017 for (const auto &P : Pack.pack_elements()) { 2018 char C = (char)P.getAsIntegral().getZExtValue(); 2019 OS << C; 2020 } 2021 break; 2022 } 2023 case UserDefinedLiteral::LOK_Integer: { 2024 // Print integer literal without suffix. 2025 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 2026 OS << Int->getValue().toString(10, /*isSigned*/false); 2027 break; 2028 } 2029 case UserDefinedLiteral::LOK_Floating: { 2030 // Print floating literal without suffix. 2031 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 2032 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 2033 break; 2034 } 2035 case UserDefinedLiteral::LOK_String: 2036 case UserDefinedLiteral::LOK_Character: 2037 PrintExpr(Node->getCookedLiteral()); 2038 break; 2039 } 2040 OS << Node->getUDSuffix()->getName(); 2041 } 2042 2043 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 2044 OS << (Node->getValue() ? "true" : "false"); 2045 } 2046 2047 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 2048 OS << "nullptr"; 2049 } 2050 2051 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 2052 OS << "this"; 2053 } 2054 2055 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 2056 if (!Node->getSubExpr()) 2057 OS << "throw"; 2058 else { 2059 OS << "throw "; 2060 PrintExpr(Node->getSubExpr()); 2061 } 2062 } 2063 2064 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 2065 // Nothing to print: we picked up the default argument. 2066 } 2067 2068 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 2069 // Nothing to print: we picked up the default initializer. 2070 } 2071 2072 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 2073 Node->getType().print(OS, Policy); 2074 // If there are no parens, this is list-initialization, and the braces are 2075 // part of the syntax of the inner construct. 2076 if (Node->getLParenLoc().isValid()) 2077 OS << "("; 2078 PrintExpr(Node->getSubExpr()); 2079 if (Node->getLParenLoc().isValid()) 2080 OS << ")"; 2081 } 2082 2083 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 2084 PrintExpr(Node->getSubExpr()); 2085 } 2086 2087 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 2088 Node->getType().print(OS, Policy); 2089 if (Node->isStdInitListInitialization()) 2090 /* Nothing to do; braces are part of creating the std::initializer_list. */; 2091 else if (Node->isListInitialization()) 2092 OS << "{"; 2093 else 2094 OS << "("; 2095 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 2096 ArgEnd = Node->arg_end(); 2097 Arg != ArgEnd; ++Arg) { 2098 if ((*Arg)->isDefaultArgument()) 2099 break; 2100 if (Arg != Node->arg_begin()) 2101 OS << ", "; 2102 PrintExpr(*Arg); 2103 } 2104 if (Node->isStdInitListInitialization()) 2105 /* See above. */; 2106 else if (Node->isListInitialization()) 2107 OS << "}"; 2108 else 2109 OS << ")"; 2110 } 2111 2112 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 2113 OS << '['; 2114 bool NeedComma = false; 2115 switch (Node->getCaptureDefault()) { 2116 case LCD_None: 2117 break; 2118 2119 case LCD_ByCopy: 2120 OS << '='; 2121 NeedComma = true; 2122 break; 2123 2124 case LCD_ByRef: 2125 OS << '&'; 2126 NeedComma = true; 2127 break; 2128 } 2129 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 2130 CEnd = Node->explicit_capture_end(); 2131 C != CEnd; 2132 ++C) { 2133 if (NeedComma) 2134 OS << ", "; 2135 NeedComma = true; 2136 2137 switch (C->getCaptureKind()) { 2138 case LCK_This: 2139 OS << "this"; 2140 break; 2141 case LCK_StarThis: 2142 OS << "*this"; 2143 break; 2144 case LCK_ByRef: 2145 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C)) 2146 OS << '&'; 2147 OS << C->getCapturedVar()->getName(); 2148 break; 2149 2150 case LCK_ByCopy: 2151 OS << C->getCapturedVar()->getName(); 2152 break; 2153 case LCK_VLAType: 2154 llvm_unreachable("VLA type in explicit captures."); 2155 } 2156 2157 if (Node->isInitCapture(C)) 2158 PrintExpr(C->getCapturedVar()->getInit()); 2159 } 2160 OS << ']'; 2161 2162 if (Node->hasExplicitParameters()) { 2163 OS << " ("; 2164 CXXMethodDecl *Method = Node->getCallOperator(); 2165 NeedComma = false; 2166 for (auto P : Method->parameters()) { 2167 if (NeedComma) { 2168 OS << ", "; 2169 } else { 2170 NeedComma = true; 2171 } 2172 std::string ParamStr = P->getNameAsString(); 2173 P->getOriginalType().print(OS, Policy, ParamStr); 2174 } 2175 if (Method->isVariadic()) { 2176 if (NeedComma) 2177 OS << ", "; 2178 OS << "..."; 2179 } 2180 OS << ')'; 2181 2182 if (Node->isMutable()) 2183 OS << " mutable"; 2184 2185 const FunctionProtoType *Proto 2186 = Method->getType()->getAs<FunctionProtoType>(); 2187 Proto->printExceptionSpecification(OS, Policy); 2188 2189 // FIXME: Attributes 2190 2191 // Print the trailing return type if it was specified in the source. 2192 if (Node->hasExplicitResultType()) { 2193 OS << " -> "; 2194 Proto->getReturnType().print(OS, Policy); 2195 } 2196 } 2197 2198 // Print the body. 2199 CompoundStmt *Body = Node->getBody(); 2200 OS << ' '; 2201 PrintStmt(Body); 2202 } 2203 2204 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 2205 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 2206 TSInfo->getType().print(OS, Policy); 2207 else 2208 Node->getType().print(OS, Policy); 2209 OS << "()"; 2210 } 2211 2212 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 2213 if (E->isGlobalNew()) 2214 OS << "::"; 2215 OS << "new "; 2216 unsigned NumPlace = E->getNumPlacementArgs(); 2217 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 2218 OS << "("; 2219 PrintExpr(E->getPlacementArg(0)); 2220 for (unsigned i = 1; i < NumPlace; ++i) { 2221 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 2222 break; 2223 OS << ", "; 2224 PrintExpr(E->getPlacementArg(i)); 2225 } 2226 OS << ") "; 2227 } 2228 if (E->isParenTypeId()) 2229 OS << "("; 2230 std::string TypeS; 2231 if (Expr *Size = E->getArraySize()) { 2232 llvm::raw_string_ostream s(TypeS); 2233 s << '['; 2234 Size->printPretty(s, Helper, Policy); 2235 s << ']'; 2236 } 2237 E->getAllocatedType().print(OS, Policy, TypeS); 2238 if (E->isParenTypeId()) 2239 OS << ")"; 2240 2241 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 2242 if (InitStyle) { 2243 if (InitStyle == CXXNewExpr::CallInit) 2244 OS << "("; 2245 PrintExpr(E->getInitializer()); 2246 if (InitStyle == CXXNewExpr::CallInit) 2247 OS << ")"; 2248 } 2249 } 2250 2251 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 2252 if (E->isGlobalDelete()) 2253 OS << "::"; 2254 OS << "delete "; 2255 if (E->isArrayForm()) 2256 OS << "[] "; 2257 PrintExpr(E->getArgument()); 2258 } 2259 2260 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 2261 PrintExpr(E->getBase()); 2262 if (E->isArrow()) 2263 OS << "->"; 2264 else 2265 OS << '.'; 2266 if (E->getQualifier()) 2267 E->getQualifier()->print(OS, Policy); 2268 OS << "~"; 2269 2270 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 2271 OS << II->getName(); 2272 else 2273 E->getDestroyedType().print(OS, Policy); 2274 } 2275 2276 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 2277 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2278 OS << "{"; 2279 2280 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 2281 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 2282 // Don't print any defaulted arguments 2283 break; 2284 } 2285 2286 if (i) OS << ", "; 2287 PrintExpr(E->getArg(i)); 2288 } 2289 2290 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2291 OS << "}"; 2292 } 2293 2294 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) { 2295 // Parens are printed by the surrounding context. 2296 OS << "<forwarded>"; 2297 } 2298 2299 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 2300 PrintExpr(E->getSubExpr()); 2301 } 2302 2303 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 2304 // Just forward to the subexpression. 2305 PrintExpr(E->getSubExpr()); 2306 } 2307 2308 void 2309 StmtPrinter::VisitCXXUnresolvedConstructExpr( 2310 CXXUnresolvedConstructExpr *Node) { 2311 Node->getTypeAsWritten().print(OS, Policy); 2312 OS << "("; 2313 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 2314 ArgEnd = Node->arg_end(); 2315 Arg != ArgEnd; ++Arg) { 2316 if (Arg != Node->arg_begin()) 2317 OS << ", "; 2318 PrintExpr(*Arg); 2319 } 2320 OS << ")"; 2321 } 2322 2323 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 2324 CXXDependentScopeMemberExpr *Node) { 2325 if (!Node->isImplicitAccess()) { 2326 PrintExpr(Node->getBase()); 2327 OS << (Node->isArrow() ? "->" : "."); 2328 } 2329 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2330 Qualifier->print(OS, Policy); 2331 if (Node->hasTemplateKeyword()) 2332 OS << "template "; 2333 OS << Node->getMemberNameInfo(); 2334 if (Node->hasExplicitTemplateArgs()) 2335 TemplateSpecializationType::PrintTemplateArgumentList( 2336 OS, Node->template_arguments(), Policy); 2337 } 2338 2339 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 2340 if (!Node->isImplicitAccess()) { 2341 PrintExpr(Node->getBase()); 2342 OS << (Node->isArrow() ? "->" : "."); 2343 } 2344 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2345 Qualifier->print(OS, Policy); 2346 if (Node->hasTemplateKeyword()) 2347 OS << "template "; 2348 OS << Node->getMemberNameInfo(); 2349 if (Node->hasExplicitTemplateArgs()) 2350 TemplateSpecializationType::PrintTemplateArgumentList( 2351 OS, Node->template_arguments(), Policy); 2352 } 2353 2354 static const char *getTypeTraitName(TypeTrait TT) { 2355 switch (TT) { 2356 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 2357 case clang::UTT_##Name: return #Spelling; 2358 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 2359 case clang::BTT_##Name: return #Spelling; 2360 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 2361 case clang::TT_##Name: return #Spelling; 2362 #include "clang/Basic/TokenKinds.def" 2363 } 2364 llvm_unreachable("Type trait not covered by switch"); 2365 } 2366 2367 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 2368 switch (ATT) { 2369 case ATT_ArrayRank: return "__array_rank"; 2370 case ATT_ArrayExtent: return "__array_extent"; 2371 } 2372 llvm_unreachable("Array type trait not covered by switch"); 2373 } 2374 2375 static const char *getExpressionTraitName(ExpressionTrait ET) { 2376 switch (ET) { 2377 case ET_IsLValueExpr: return "__is_lvalue_expr"; 2378 case ET_IsRValueExpr: return "__is_rvalue_expr"; 2379 } 2380 llvm_unreachable("Expression type trait not covered by switch"); 2381 } 2382 2383 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 2384 OS << getTypeTraitName(E->getTrait()) << "("; 2385 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 2386 if (I > 0) 2387 OS << ", "; 2388 E->getArg(I)->getType().print(OS, Policy); 2389 } 2390 OS << ")"; 2391 } 2392 2393 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2394 OS << getTypeTraitName(E->getTrait()) << '('; 2395 E->getQueriedType().print(OS, Policy); 2396 OS << ')'; 2397 } 2398 2399 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2400 OS << getExpressionTraitName(E->getTrait()) << '('; 2401 PrintExpr(E->getQueriedExpression()); 2402 OS << ')'; 2403 } 2404 2405 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 2406 OS << "noexcept("; 2407 PrintExpr(E->getOperand()); 2408 OS << ")"; 2409 } 2410 2411 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 2412 PrintExpr(E->getPattern()); 2413 OS << "..."; 2414 } 2415 2416 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2417 OS << "sizeof...(" << *E->getPack() << ")"; 2418 } 2419 2420 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 2421 SubstNonTypeTemplateParmPackExpr *Node) { 2422 OS << *Node->getParameterPack(); 2423 } 2424 2425 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 2426 SubstNonTypeTemplateParmExpr *Node) { 2427 Visit(Node->getReplacement()); 2428 } 2429 2430 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 2431 OS << *E->getParameterPack(); 2432 } 2433 2434 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 2435 PrintExpr(Node->GetTemporaryExpr()); 2436 } 2437 2438 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 2439 OS << "("; 2440 if (E->getLHS()) { 2441 PrintExpr(E->getLHS()); 2442 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2443 } 2444 OS << "..."; 2445 if (E->getRHS()) { 2446 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2447 PrintExpr(E->getRHS()); 2448 } 2449 OS << ")"; 2450 } 2451 2452 // C++ Coroutines TS 2453 2454 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) { 2455 Visit(S->getBody()); 2456 } 2457 2458 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) { 2459 OS << "co_return"; 2460 if (S->getOperand()) { 2461 OS << " "; 2462 Visit(S->getOperand()); 2463 } 2464 OS << ";"; 2465 } 2466 2467 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) { 2468 OS << "co_await "; 2469 PrintExpr(S->getOperand()); 2470 } 2471 2472 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) { 2473 OS << "co_yield "; 2474 PrintExpr(S->getOperand()); 2475 } 2476 2477 // Obj-C 2478 2479 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 2480 OS << "@"; 2481 VisitStringLiteral(Node->getString()); 2482 } 2483 2484 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 2485 OS << "@"; 2486 Visit(E->getSubExpr()); 2487 } 2488 2489 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 2490 OS << "@[ "; 2491 ObjCArrayLiteral::child_range Ch = E->children(); 2492 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) { 2493 if (I != Ch.begin()) 2494 OS << ", "; 2495 Visit(*I); 2496 } 2497 OS << " ]"; 2498 } 2499 2500 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 2501 OS << "@{ "; 2502 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 2503 if (I > 0) 2504 OS << ", "; 2505 2506 ObjCDictionaryElement Element = E->getKeyValueElement(I); 2507 Visit(Element.Key); 2508 OS << " : "; 2509 Visit(Element.Value); 2510 if (Element.isPackExpansion()) 2511 OS << "..."; 2512 } 2513 OS << " }"; 2514 } 2515 2516 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 2517 OS << "@encode("; 2518 Node->getEncodedType().print(OS, Policy); 2519 OS << ')'; 2520 } 2521 2522 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 2523 OS << "@selector("; 2524 Node->getSelector().print(OS); 2525 OS << ')'; 2526 } 2527 2528 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 2529 OS << "@protocol(" << *Node->getProtocol() << ')'; 2530 } 2531 2532 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 2533 OS << "["; 2534 switch (Mess->getReceiverKind()) { 2535 case ObjCMessageExpr::Instance: 2536 PrintExpr(Mess->getInstanceReceiver()); 2537 break; 2538 2539 case ObjCMessageExpr::Class: 2540 Mess->getClassReceiver().print(OS, Policy); 2541 break; 2542 2543 case ObjCMessageExpr::SuperInstance: 2544 case ObjCMessageExpr::SuperClass: 2545 OS << "Super"; 2546 break; 2547 } 2548 2549 OS << ' '; 2550 Selector selector = Mess->getSelector(); 2551 if (selector.isUnarySelector()) { 2552 OS << selector.getNameForSlot(0); 2553 } else { 2554 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 2555 if (i < selector.getNumArgs()) { 2556 if (i > 0) OS << ' '; 2557 if (selector.getIdentifierInfoForSlot(i)) 2558 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 2559 else 2560 OS << ":"; 2561 } 2562 else OS << ", "; // Handle variadic methods. 2563 2564 PrintExpr(Mess->getArg(i)); 2565 } 2566 } 2567 OS << "]"; 2568 } 2569 2570 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 2571 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 2572 } 2573 2574 void 2575 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2576 PrintExpr(E->getSubExpr()); 2577 } 2578 2579 void 2580 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2581 OS << '(' << E->getBridgeKindName(); 2582 E->getType().print(OS, Policy); 2583 OS << ')'; 2584 PrintExpr(E->getSubExpr()); 2585 } 2586 2587 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2588 BlockDecl *BD = Node->getBlockDecl(); 2589 OS << "^"; 2590 2591 const FunctionType *AFT = Node->getFunctionType(); 2592 2593 if (isa<FunctionNoProtoType>(AFT)) { 2594 OS << "()"; 2595 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2596 OS << '('; 2597 for (BlockDecl::param_iterator AI = BD->param_begin(), 2598 E = BD->param_end(); AI != E; ++AI) { 2599 if (AI != BD->param_begin()) OS << ", "; 2600 std::string ParamStr = (*AI)->getNameAsString(); 2601 (*AI)->getType().print(OS, Policy, ParamStr); 2602 } 2603 2604 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2605 if (FT->isVariadic()) { 2606 if (!BD->param_empty()) OS << ", "; 2607 OS << "..."; 2608 } 2609 OS << ')'; 2610 } 2611 OS << "{ }"; 2612 } 2613 2614 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2615 PrintExpr(Node->getSourceExpr()); 2616 } 2617 2618 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 2619 // TODO: Print something reasonable for a TypoExpr, if necessary. 2620 llvm_unreachable("Cannot print TypoExpr nodes"); 2621 } 2622 2623 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2624 OS << "__builtin_astype("; 2625 PrintExpr(Node->getSrcExpr()); 2626 OS << ", "; 2627 Node->getType().print(OS, Policy); 2628 OS << ")"; 2629 } 2630 2631 //===----------------------------------------------------------------------===// 2632 // Stmt method implementations 2633 //===----------------------------------------------------------------------===// 2634 2635 void Stmt::dumpPretty(const ASTContext &Context) const { 2636 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2637 } 2638 2639 void Stmt::printPretty(raw_ostream &OS, 2640 PrinterHelper *Helper, 2641 const PrintingPolicy &Policy, 2642 unsigned Indentation) const { 2643 StmtPrinter P(OS, Helper, Policy, Indentation); 2644 P.Visit(const_cast<Stmt*>(this)); 2645 } 2646 2647 //===----------------------------------------------------------------------===// 2648 // PrinterHelper 2649 //===----------------------------------------------------------------------===// 2650 2651 // Implement virtual destructor. 2652 PrinterHelper::~PrinterHelper() {} 2653