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