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