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 Node->getOp() != AtomicExpr::AO__opencl_atomic_load) { 1896 OS << ", "; 1897 PrintExpr(Node->getVal1()); 1898 } 1899 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1900 Node->isCmpXChg()) { 1901 OS << ", "; 1902 PrintExpr(Node->getVal2()); 1903 } 1904 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1905 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1906 OS << ", "; 1907 PrintExpr(Node->getWeak()); 1908 } 1909 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init && 1910 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) { 1911 OS << ", "; 1912 PrintExpr(Node->getOrder()); 1913 } 1914 if (Node->isCmpXChg()) { 1915 OS << ", "; 1916 PrintExpr(Node->getOrderFail()); 1917 } 1918 OS << ")"; 1919 } 1920 1921 // C++ 1922 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1923 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1924 "", 1925 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1926 Spelling, 1927 #include "clang/Basic/OperatorKinds.def" 1928 }; 1929 1930 OverloadedOperatorKind Kind = Node->getOperator(); 1931 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1932 if (Node->getNumArgs() == 1) { 1933 OS << OpStrings[Kind] << ' '; 1934 PrintExpr(Node->getArg(0)); 1935 } else { 1936 PrintExpr(Node->getArg(0)); 1937 OS << ' ' << OpStrings[Kind]; 1938 } 1939 } else if (Kind == OO_Arrow) { 1940 PrintExpr(Node->getArg(0)); 1941 } else if (Kind == OO_Call) { 1942 PrintExpr(Node->getArg(0)); 1943 OS << '('; 1944 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1945 if (ArgIdx > 1) 1946 OS << ", "; 1947 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1948 PrintExpr(Node->getArg(ArgIdx)); 1949 } 1950 OS << ')'; 1951 } else if (Kind == OO_Subscript) { 1952 PrintExpr(Node->getArg(0)); 1953 OS << '['; 1954 PrintExpr(Node->getArg(1)); 1955 OS << ']'; 1956 } else if (Node->getNumArgs() == 1) { 1957 OS << OpStrings[Kind] << ' '; 1958 PrintExpr(Node->getArg(0)); 1959 } else if (Node->getNumArgs() == 2) { 1960 PrintExpr(Node->getArg(0)); 1961 OS << ' ' << OpStrings[Kind] << ' '; 1962 PrintExpr(Node->getArg(1)); 1963 } else { 1964 llvm_unreachable("unknown overloaded operator"); 1965 } 1966 } 1967 1968 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1969 // If we have a conversion operator call only print the argument. 1970 CXXMethodDecl *MD = Node->getMethodDecl(); 1971 if (MD && isa<CXXConversionDecl>(MD)) { 1972 PrintExpr(Node->getImplicitObjectArgument()); 1973 return; 1974 } 1975 VisitCallExpr(cast<CallExpr>(Node)); 1976 } 1977 1978 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1979 PrintExpr(Node->getCallee()); 1980 OS << "<<<"; 1981 PrintCallArgs(Node->getConfig()); 1982 OS << ">>>("; 1983 PrintCallArgs(Node); 1984 OS << ")"; 1985 } 1986 1987 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1988 OS << Node->getCastName() << '<'; 1989 Node->getTypeAsWritten().print(OS, Policy); 1990 OS << ">("; 1991 PrintExpr(Node->getSubExpr()); 1992 OS << ")"; 1993 } 1994 1995 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1996 VisitCXXNamedCastExpr(Node); 1997 } 1998 1999 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 2000 VisitCXXNamedCastExpr(Node); 2001 } 2002 2003 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 2004 VisitCXXNamedCastExpr(Node); 2005 } 2006 2007 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 2008 VisitCXXNamedCastExpr(Node); 2009 } 2010 2011 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 2012 OS << "typeid("; 2013 if (Node->isTypeOperand()) { 2014 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 2015 } else { 2016 PrintExpr(Node->getExprOperand()); 2017 } 2018 OS << ")"; 2019 } 2020 2021 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 2022 OS << "__uuidof("; 2023 if (Node->isTypeOperand()) { 2024 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 2025 } else { 2026 PrintExpr(Node->getExprOperand()); 2027 } 2028 OS << ")"; 2029 } 2030 2031 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 2032 PrintExpr(Node->getBaseExpr()); 2033 if (Node->isArrow()) 2034 OS << "->"; 2035 else 2036 OS << "."; 2037 if (NestedNameSpecifier *Qualifier = 2038 Node->getQualifierLoc().getNestedNameSpecifier()) 2039 Qualifier->print(OS, Policy); 2040 OS << Node->getPropertyDecl()->getDeclName(); 2041 } 2042 2043 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) { 2044 PrintExpr(Node->getBase()); 2045 OS << "["; 2046 PrintExpr(Node->getIdx()); 2047 OS << "]"; 2048 } 2049 2050 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 2051 switch (Node->getLiteralOperatorKind()) { 2052 case UserDefinedLiteral::LOK_Raw: 2053 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 2054 break; 2055 case UserDefinedLiteral::LOK_Template: { 2056 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 2057 const TemplateArgumentList *Args = 2058 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 2059 assert(Args); 2060 2061 if (Args->size() != 1) { 2062 OS << "operator\"\"" << Node->getUDSuffix()->getName(); 2063 TemplateSpecializationType::PrintTemplateArgumentList( 2064 OS, Args->asArray(), Policy); 2065 OS << "()"; 2066 return; 2067 } 2068 2069 const TemplateArgument &Pack = Args->get(0); 2070 for (const auto &P : Pack.pack_elements()) { 2071 char C = (char)P.getAsIntegral().getZExtValue(); 2072 OS << C; 2073 } 2074 break; 2075 } 2076 case UserDefinedLiteral::LOK_Integer: { 2077 // Print integer literal without suffix. 2078 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 2079 OS << Int->getValue().toString(10, /*isSigned*/false); 2080 break; 2081 } 2082 case UserDefinedLiteral::LOK_Floating: { 2083 // Print floating literal without suffix. 2084 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 2085 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 2086 break; 2087 } 2088 case UserDefinedLiteral::LOK_String: 2089 case UserDefinedLiteral::LOK_Character: 2090 PrintExpr(Node->getCookedLiteral()); 2091 break; 2092 } 2093 OS << Node->getUDSuffix()->getName(); 2094 } 2095 2096 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 2097 OS << (Node->getValue() ? "true" : "false"); 2098 } 2099 2100 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 2101 OS << "nullptr"; 2102 } 2103 2104 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 2105 OS << "this"; 2106 } 2107 2108 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 2109 if (!Node->getSubExpr()) 2110 OS << "throw"; 2111 else { 2112 OS << "throw "; 2113 PrintExpr(Node->getSubExpr()); 2114 } 2115 } 2116 2117 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 2118 // Nothing to print: we picked up the default argument. 2119 } 2120 2121 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 2122 // Nothing to print: we picked up the default initializer. 2123 } 2124 2125 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 2126 Node->getType().print(OS, Policy); 2127 // If there are no parens, this is list-initialization, and the braces are 2128 // part of the syntax of the inner construct. 2129 if (Node->getLParenLoc().isValid()) 2130 OS << "("; 2131 PrintExpr(Node->getSubExpr()); 2132 if (Node->getLParenLoc().isValid()) 2133 OS << ")"; 2134 } 2135 2136 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 2137 PrintExpr(Node->getSubExpr()); 2138 } 2139 2140 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 2141 Node->getType().print(OS, Policy); 2142 if (Node->isStdInitListInitialization()) 2143 /* Nothing to do; braces are part of creating the std::initializer_list. */; 2144 else if (Node->isListInitialization()) 2145 OS << "{"; 2146 else 2147 OS << "("; 2148 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 2149 ArgEnd = Node->arg_end(); 2150 Arg != ArgEnd; ++Arg) { 2151 if ((*Arg)->isDefaultArgument()) 2152 break; 2153 if (Arg != Node->arg_begin()) 2154 OS << ", "; 2155 PrintExpr(*Arg); 2156 } 2157 if (Node->isStdInitListInitialization()) 2158 /* See above. */; 2159 else if (Node->isListInitialization()) 2160 OS << "}"; 2161 else 2162 OS << ")"; 2163 } 2164 2165 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 2166 OS << '['; 2167 bool NeedComma = false; 2168 switch (Node->getCaptureDefault()) { 2169 case LCD_None: 2170 break; 2171 2172 case LCD_ByCopy: 2173 OS << '='; 2174 NeedComma = true; 2175 break; 2176 2177 case LCD_ByRef: 2178 OS << '&'; 2179 NeedComma = true; 2180 break; 2181 } 2182 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 2183 CEnd = Node->explicit_capture_end(); 2184 C != CEnd; 2185 ++C) { 2186 if (NeedComma) 2187 OS << ", "; 2188 NeedComma = true; 2189 2190 switch (C->getCaptureKind()) { 2191 case LCK_This: 2192 OS << "this"; 2193 break; 2194 case LCK_StarThis: 2195 OS << "*this"; 2196 break; 2197 case LCK_ByRef: 2198 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C)) 2199 OS << '&'; 2200 OS << C->getCapturedVar()->getName(); 2201 break; 2202 2203 case LCK_ByCopy: 2204 OS << C->getCapturedVar()->getName(); 2205 break; 2206 case LCK_VLAType: 2207 llvm_unreachable("VLA type in explicit captures."); 2208 } 2209 2210 if (Node->isInitCapture(C)) 2211 PrintExpr(C->getCapturedVar()->getInit()); 2212 } 2213 OS << ']'; 2214 2215 if (Node->hasExplicitParameters()) { 2216 OS << " ("; 2217 CXXMethodDecl *Method = Node->getCallOperator(); 2218 NeedComma = false; 2219 for (auto P : Method->parameters()) { 2220 if (NeedComma) { 2221 OS << ", "; 2222 } else { 2223 NeedComma = true; 2224 } 2225 std::string ParamStr = P->getNameAsString(); 2226 P->getOriginalType().print(OS, Policy, ParamStr); 2227 } 2228 if (Method->isVariadic()) { 2229 if (NeedComma) 2230 OS << ", "; 2231 OS << "..."; 2232 } 2233 OS << ')'; 2234 2235 if (Node->isMutable()) 2236 OS << " mutable"; 2237 2238 const FunctionProtoType *Proto 2239 = Method->getType()->getAs<FunctionProtoType>(); 2240 Proto->printExceptionSpecification(OS, Policy); 2241 2242 // FIXME: Attributes 2243 2244 // Print the trailing return type if it was specified in the source. 2245 if (Node->hasExplicitResultType()) { 2246 OS << " -> "; 2247 Proto->getReturnType().print(OS, Policy); 2248 } 2249 } 2250 2251 // Print the body. 2252 CompoundStmt *Body = Node->getBody(); 2253 OS << ' '; 2254 PrintStmt(Body); 2255 } 2256 2257 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 2258 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 2259 TSInfo->getType().print(OS, Policy); 2260 else 2261 Node->getType().print(OS, Policy); 2262 OS << "()"; 2263 } 2264 2265 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 2266 if (E->isGlobalNew()) 2267 OS << "::"; 2268 OS << "new "; 2269 unsigned NumPlace = E->getNumPlacementArgs(); 2270 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 2271 OS << "("; 2272 PrintExpr(E->getPlacementArg(0)); 2273 for (unsigned i = 1; i < NumPlace; ++i) { 2274 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 2275 break; 2276 OS << ", "; 2277 PrintExpr(E->getPlacementArg(i)); 2278 } 2279 OS << ") "; 2280 } 2281 if (E->isParenTypeId()) 2282 OS << "("; 2283 std::string TypeS; 2284 if (Expr *Size = E->getArraySize()) { 2285 llvm::raw_string_ostream s(TypeS); 2286 s << '['; 2287 Size->printPretty(s, Helper, Policy); 2288 s << ']'; 2289 } 2290 E->getAllocatedType().print(OS, Policy, TypeS); 2291 if (E->isParenTypeId()) 2292 OS << ")"; 2293 2294 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 2295 if (InitStyle) { 2296 if (InitStyle == CXXNewExpr::CallInit) 2297 OS << "("; 2298 PrintExpr(E->getInitializer()); 2299 if (InitStyle == CXXNewExpr::CallInit) 2300 OS << ")"; 2301 } 2302 } 2303 2304 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 2305 if (E->isGlobalDelete()) 2306 OS << "::"; 2307 OS << "delete "; 2308 if (E->isArrayForm()) 2309 OS << "[] "; 2310 PrintExpr(E->getArgument()); 2311 } 2312 2313 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 2314 PrintExpr(E->getBase()); 2315 if (E->isArrow()) 2316 OS << "->"; 2317 else 2318 OS << '.'; 2319 if (E->getQualifier()) 2320 E->getQualifier()->print(OS, Policy); 2321 OS << "~"; 2322 2323 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 2324 OS << II->getName(); 2325 else 2326 E->getDestroyedType().print(OS, Policy); 2327 } 2328 2329 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 2330 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2331 OS << "{"; 2332 2333 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 2334 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 2335 // Don't print any defaulted arguments 2336 break; 2337 } 2338 2339 if (i) OS << ", "; 2340 PrintExpr(E->getArg(i)); 2341 } 2342 2343 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2344 OS << "}"; 2345 } 2346 2347 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) { 2348 // Parens are printed by the surrounding context. 2349 OS << "<forwarded>"; 2350 } 2351 2352 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 2353 PrintExpr(E->getSubExpr()); 2354 } 2355 2356 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 2357 // Just forward to the subexpression. 2358 PrintExpr(E->getSubExpr()); 2359 } 2360 2361 void 2362 StmtPrinter::VisitCXXUnresolvedConstructExpr( 2363 CXXUnresolvedConstructExpr *Node) { 2364 Node->getTypeAsWritten().print(OS, Policy); 2365 OS << "("; 2366 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 2367 ArgEnd = Node->arg_end(); 2368 Arg != ArgEnd; ++Arg) { 2369 if (Arg != Node->arg_begin()) 2370 OS << ", "; 2371 PrintExpr(*Arg); 2372 } 2373 OS << ")"; 2374 } 2375 2376 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 2377 CXXDependentScopeMemberExpr *Node) { 2378 if (!Node->isImplicitAccess()) { 2379 PrintExpr(Node->getBase()); 2380 OS << (Node->isArrow() ? "->" : "."); 2381 } 2382 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2383 Qualifier->print(OS, Policy); 2384 if (Node->hasTemplateKeyword()) 2385 OS << "template "; 2386 OS << Node->getMemberNameInfo(); 2387 if (Node->hasExplicitTemplateArgs()) 2388 TemplateSpecializationType::PrintTemplateArgumentList( 2389 OS, Node->template_arguments(), Policy); 2390 } 2391 2392 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 2393 if (!Node->isImplicitAccess()) { 2394 PrintExpr(Node->getBase()); 2395 OS << (Node->isArrow() ? "->" : "."); 2396 } 2397 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2398 Qualifier->print(OS, Policy); 2399 if (Node->hasTemplateKeyword()) 2400 OS << "template "; 2401 OS << Node->getMemberNameInfo(); 2402 if (Node->hasExplicitTemplateArgs()) 2403 TemplateSpecializationType::PrintTemplateArgumentList( 2404 OS, Node->template_arguments(), Policy); 2405 } 2406 2407 static const char *getTypeTraitName(TypeTrait TT) { 2408 switch (TT) { 2409 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 2410 case clang::UTT_##Name: return #Spelling; 2411 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 2412 case clang::BTT_##Name: return #Spelling; 2413 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 2414 case clang::TT_##Name: return #Spelling; 2415 #include "clang/Basic/TokenKinds.def" 2416 } 2417 llvm_unreachable("Type trait not covered by switch"); 2418 } 2419 2420 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 2421 switch (ATT) { 2422 case ATT_ArrayRank: return "__array_rank"; 2423 case ATT_ArrayExtent: return "__array_extent"; 2424 } 2425 llvm_unreachable("Array type trait not covered by switch"); 2426 } 2427 2428 static const char *getExpressionTraitName(ExpressionTrait ET) { 2429 switch (ET) { 2430 case ET_IsLValueExpr: return "__is_lvalue_expr"; 2431 case ET_IsRValueExpr: return "__is_rvalue_expr"; 2432 } 2433 llvm_unreachable("Expression type trait not covered by switch"); 2434 } 2435 2436 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 2437 OS << getTypeTraitName(E->getTrait()) << "("; 2438 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 2439 if (I > 0) 2440 OS << ", "; 2441 E->getArg(I)->getType().print(OS, Policy); 2442 } 2443 OS << ")"; 2444 } 2445 2446 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2447 OS << getTypeTraitName(E->getTrait()) << '('; 2448 E->getQueriedType().print(OS, Policy); 2449 OS << ')'; 2450 } 2451 2452 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2453 OS << getExpressionTraitName(E->getTrait()) << '('; 2454 PrintExpr(E->getQueriedExpression()); 2455 OS << ')'; 2456 } 2457 2458 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 2459 OS << "noexcept("; 2460 PrintExpr(E->getOperand()); 2461 OS << ")"; 2462 } 2463 2464 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 2465 PrintExpr(E->getPattern()); 2466 OS << "..."; 2467 } 2468 2469 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2470 OS << "sizeof...(" << *E->getPack() << ")"; 2471 } 2472 2473 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 2474 SubstNonTypeTemplateParmPackExpr *Node) { 2475 OS << *Node->getParameterPack(); 2476 } 2477 2478 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 2479 SubstNonTypeTemplateParmExpr *Node) { 2480 Visit(Node->getReplacement()); 2481 } 2482 2483 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 2484 OS << *E->getParameterPack(); 2485 } 2486 2487 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 2488 PrintExpr(Node->GetTemporaryExpr()); 2489 } 2490 2491 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 2492 OS << "("; 2493 if (E->getLHS()) { 2494 PrintExpr(E->getLHS()); 2495 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2496 } 2497 OS << "..."; 2498 if (E->getRHS()) { 2499 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2500 PrintExpr(E->getRHS()); 2501 } 2502 OS << ")"; 2503 } 2504 2505 // C++ Coroutines TS 2506 2507 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) { 2508 Visit(S->getBody()); 2509 } 2510 2511 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) { 2512 OS << "co_return"; 2513 if (S->getOperand()) { 2514 OS << " "; 2515 Visit(S->getOperand()); 2516 } 2517 OS << ";"; 2518 } 2519 2520 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) { 2521 OS << "co_await "; 2522 PrintExpr(S->getOperand()); 2523 } 2524 2525 2526 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) { 2527 OS << "co_await "; 2528 PrintExpr(S->getOperand()); 2529 } 2530 2531 2532 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) { 2533 OS << "co_yield "; 2534 PrintExpr(S->getOperand()); 2535 } 2536 2537 // Obj-C 2538 2539 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 2540 OS << "@"; 2541 VisitStringLiteral(Node->getString()); 2542 } 2543 2544 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 2545 OS << "@"; 2546 Visit(E->getSubExpr()); 2547 } 2548 2549 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 2550 OS << "@[ "; 2551 ObjCArrayLiteral::child_range Ch = E->children(); 2552 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) { 2553 if (I != Ch.begin()) 2554 OS << ", "; 2555 Visit(*I); 2556 } 2557 OS << " ]"; 2558 } 2559 2560 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 2561 OS << "@{ "; 2562 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 2563 if (I > 0) 2564 OS << ", "; 2565 2566 ObjCDictionaryElement Element = E->getKeyValueElement(I); 2567 Visit(Element.Key); 2568 OS << " : "; 2569 Visit(Element.Value); 2570 if (Element.isPackExpansion()) 2571 OS << "..."; 2572 } 2573 OS << " }"; 2574 } 2575 2576 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 2577 OS << "@encode("; 2578 Node->getEncodedType().print(OS, Policy); 2579 OS << ')'; 2580 } 2581 2582 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 2583 OS << "@selector("; 2584 Node->getSelector().print(OS); 2585 OS << ')'; 2586 } 2587 2588 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 2589 OS << "@protocol(" << *Node->getProtocol() << ')'; 2590 } 2591 2592 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 2593 OS << "["; 2594 switch (Mess->getReceiverKind()) { 2595 case ObjCMessageExpr::Instance: 2596 PrintExpr(Mess->getInstanceReceiver()); 2597 break; 2598 2599 case ObjCMessageExpr::Class: 2600 Mess->getClassReceiver().print(OS, Policy); 2601 break; 2602 2603 case ObjCMessageExpr::SuperInstance: 2604 case ObjCMessageExpr::SuperClass: 2605 OS << "Super"; 2606 break; 2607 } 2608 2609 OS << ' '; 2610 Selector selector = Mess->getSelector(); 2611 if (selector.isUnarySelector()) { 2612 OS << selector.getNameForSlot(0); 2613 } else { 2614 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 2615 if (i < selector.getNumArgs()) { 2616 if (i > 0) OS << ' '; 2617 if (selector.getIdentifierInfoForSlot(i)) 2618 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 2619 else 2620 OS << ":"; 2621 } 2622 else OS << ", "; // Handle variadic methods. 2623 2624 PrintExpr(Mess->getArg(i)); 2625 } 2626 } 2627 OS << "]"; 2628 } 2629 2630 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 2631 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 2632 } 2633 2634 void 2635 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2636 PrintExpr(E->getSubExpr()); 2637 } 2638 2639 void 2640 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2641 OS << '(' << E->getBridgeKindName(); 2642 E->getType().print(OS, Policy); 2643 OS << ')'; 2644 PrintExpr(E->getSubExpr()); 2645 } 2646 2647 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2648 BlockDecl *BD = Node->getBlockDecl(); 2649 OS << "^"; 2650 2651 const FunctionType *AFT = Node->getFunctionType(); 2652 2653 if (isa<FunctionNoProtoType>(AFT)) { 2654 OS << "()"; 2655 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2656 OS << '('; 2657 for (BlockDecl::param_iterator AI = BD->param_begin(), 2658 E = BD->param_end(); AI != E; ++AI) { 2659 if (AI != BD->param_begin()) OS << ", "; 2660 std::string ParamStr = (*AI)->getNameAsString(); 2661 (*AI)->getType().print(OS, Policy, ParamStr); 2662 } 2663 2664 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2665 if (FT->isVariadic()) { 2666 if (!BD->param_empty()) OS << ", "; 2667 OS << "..."; 2668 } 2669 OS << ')'; 2670 } 2671 OS << "{ }"; 2672 } 2673 2674 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2675 PrintExpr(Node->getSourceExpr()); 2676 } 2677 2678 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 2679 // TODO: Print something reasonable for a TypoExpr, if necessary. 2680 llvm_unreachable("Cannot print TypoExpr nodes"); 2681 } 2682 2683 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2684 OS << "__builtin_astype("; 2685 PrintExpr(Node->getSrcExpr()); 2686 OS << ", "; 2687 Node->getType().print(OS, Policy); 2688 OS << ")"; 2689 } 2690 2691 //===----------------------------------------------------------------------===// 2692 // Stmt method implementations 2693 //===----------------------------------------------------------------------===// 2694 2695 void Stmt::dumpPretty(const ASTContext &Context) const { 2696 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2697 } 2698 2699 void Stmt::printPretty(raw_ostream &OS, 2700 PrinterHelper *Helper, 2701 const PrintingPolicy &Policy, 2702 unsigned Indentation) const { 2703 StmtPrinter P(OS, Helper, Policy, Indentation); 2704 P.Visit(const_cast<Stmt*>(this)); 2705 } 2706 2707 //===----------------------------------------------------------------------===// 2708 // PrinterHelper 2709 //===----------------------------------------------------------------------===// 2710 2711 // Implement virtual destructor. 2712 PrinterHelper::~PrinterHelper() {} 2713