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