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