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