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