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/DeclTemplate.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/ExprCXX.h" 22 #include "clang/AST/PrettyPrinter.h" 23 #include "clang/AST/StmtVisitor.h" 24 #include "clang/Basic/CharInfo.h" 25 #include "llvm/ADT/SmallString.h" 26 #include "llvm/Support/Format.h" 27 using namespace clang; 28 29 //===----------------------------------------------------------------------===// 30 // StmtPrinter Visitor 31 //===----------------------------------------------------------------------===// 32 33 namespace { 34 class StmtPrinter : public StmtVisitor<StmtPrinter> { 35 raw_ostream &OS; 36 unsigned IndentLevel; 37 clang::PrinterHelper* Helper; 38 PrintingPolicy Policy; 39 40 public: 41 StmtPrinter(raw_ostream &os, PrinterHelper* helper, 42 const PrintingPolicy &Policy, 43 unsigned Indentation = 0) 44 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {} 45 46 void PrintStmt(Stmt *S) { 47 PrintStmt(S, Policy.Indentation); 48 } 49 50 void PrintStmt(Stmt *S, int SubIndent) { 51 IndentLevel += SubIndent; 52 if (S && isa<Expr>(S)) { 53 // If this is an expr used in a stmt context, indent and newline it. 54 Indent(); 55 Visit(S); 56 OS << ";\n"; 57 } else if (S) { 58 Visit(S); 59 } else { 60 Indent() << "<<<NULL STATEMENT>>>\n"; 61 } 62 IndentLevel -= SubIndent; 63 } 64 65 void PrintRawCompoundStmt(CompoundStmt *S); 66 void PrintRawDecl(Decl *D); 67 void PrintRawDeclStmt(const DeclStmt *S); 68 void PrintRawIfStmt(IfStmt *If); 69 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); 70 void PrintCallArgs(CallExpr *E); 71 void PrintRawSEHExceptHandler(SEHExceptStmt *S); 72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S); 73 void PrintOMPExecutableDirective(OMPExecutableDirective *S); 74 75 void PrintExpr(Expr *E) { 76 if (E) 77 Visit(E); 78 else 79 OS << "<null expr>"; 80 } 81 82 raw_ostream &Indent(int Delta = 0) { 83 for (int i = 0, e = IndentLevel+Delta; i < e; ++i) 84 OS << " "; 85 return OS; 86 } 87 88 void Visit(Stmt* S) { 89 if (Helper && Helper->handledStmt(S,OS)) 90 return; 91 else StmtVisitor<StmtPrinter>::Visit(S); 92 } 93 94 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED { 95 Indent() << "<<unknown stmt type>>\n"; 96 } 97 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED { 98 OS << "<<unknown expr type>>"; 99 } 100 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); 101 102 #define ABSTRACT_STMT(CLASS) 103 #define STMT(CLASS, PARENT) \ 104 void Visit##CLASS(CLASS *Node); 105 #include "clang/AST/StmtNodes.inc" 106 }; 107 } 108 109 //===----------------------------------------------------------------------===// 110 // Stmt printing methods. 111 //===----------------------------------------------------------------------===// 112 113 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and 114 /// with no newline after the }. 115 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { 116 OS << "{\n"; 117 for (auto *I : Node->body()) 118 PrintStmt(I); 119 120 Indent() << "}"; 121 } 122 123 void StmtPrinter::PrintRawDecl(Decl *D) { 124 D->print(OS, Policy, IndentLevel); 125 } 126 127 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) { 128 SmallVector<Decl*, 2> Decls(S->decls()); 129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); 130 } 131 132 void StmtPrinter::VisitNullStmt(NullStmt *Node) { 133 Indent() << ";\n"; 134 } 135 136 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { 137 Indent(); 138 PrintRawDeclStmt(Node); 139 OS << ";\n"; 140 } 141 142 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { 143 Indent(); 144 PrintRawCompoundStmt(Node); 145 OS << "\n"; 146 } 147 148 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { 149 Indent(-1) << "case "; 150 PrintExpr(Node->getLHS()); 151 if (Node->getRHS()) { 152 OS << " ... "; 153 PrintExpr(Node->getRHS()); 154 } 155 OS << ":\n"; 156 157 PrintStmt(Node->getSubStmt(), 0); 158 } 159 160 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { 161 Indent(-1) << "default:\n"; 162 PrintStmt(Node->getSubStmt(), 0); 163 } 164 165 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { 166 Indent(-1) << Node->getName() << ":\n"; 167 PrintStmt(Node->getSubStmt(), 0); 168 } 169 170 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) { 171 for (const auto *Attr : Node->getAttrs()) { 172 Attr->printPretty(OS, Policy); 173 } 174 175 PrintStmt(Node->getSubStmt(), 0); 176 } 177 178 void StmtPrinter::PrintRawIfStmt(IfStmt *If) { 179 OS << "if ("; 180 if (const DeclStmt *DS = If->getConditionVariableDeclStmt()) 181 PrintRawDeclStmt(DS); 182 else 183 PrintExpr(If->getCond()); 184 OS << ')'; 185 186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { 187 OS << ' '; 188 PrintRawCompoundStmt(CS); 189 OS << (If->getElse() ? ' ' : '\n'); 190 } else { 191 OS << '\n'; 192 PrintStmt(If->getThen()); 193 if (If->getElse()) Indent(); 194 } 195 196 if (Stmt *Else = If->getElse()) { 197 OS << "else"; 198 199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { 200 OS << ' '; 201 PrintRawCompoundStmt(CS); 202 OS << '\n'; 203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { 204 OS << ' '; 205 PrintRawIfStmt(ElseIf); 206 } else { 207 OS << '\n'; 208 PrintStmt(If->getElse()); 209 } 210 } 211 } 212 213 void StmtPrinter::VisitIfStmt(IfStmt *If) { 214 Indent(); 215 PrintRawIfStmt(If); 216 } 217 218 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { 219 Indent() << "switch ("; 220 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 221 PrintRawDeclStmt(DS); 222 else 223 PrintExpr(Node->getCond()); 224 OS << ")"; 225 226 // Pretty print compoundstmt bodies (very common). 227 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 228 OS << " "; 229 PrintRawCompoundStmt(CS); 230 OS << "\n"; 231 } else { 232 OS << "\n"; 233 PrintStmt(Node->getBody()); 234 } 235 } 236 237 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { 238 Indent() << "while ("; 239 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 240 PrintRawDeclStmt(DS); 241 else 242 PrintExpr(Node->getCond()); 243 OS << ")\n"; 244 PrintStmt(Node->getBody()); 245 } 246 247 void StmtPrinter::VisitDoStmt(DoStmt *Node) { 248 Indent() << "do "; 249 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 250 PrintRawCompoundStmt(CS); 251 OS << " "; 252 } else { 253 OS << "\n"; 254 PrintStmt(Node->getBody()); 255 Indent(); 256 } 257 258 OS << "while ("; 259 PrintExpr(Node->getCond()); 260 OS << ");\n"; 261 } 262 263 void StmtPrinter::VisitForStmt(ForStmt *Node) { 264 Indent() << "for ("; 265 if (Node->getInit()) { 266 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) 267 PrintRawDeclStmt(DS); 268 else 269 PrintExpr(cast<Expr>(Node->getInit())); 270 } 271 OS << ";"; 272 if (Node->getCond()) { 273 OS << " "; 274 PrintExpr(Node->getCond()); 275 } 276 OS << ";"; 277 if (Node->getInc()) { 278 OS << " "; 279 PrintExpr(Node->getInc()); 280 } 281 OS << ") "; 282 283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 284 PrintRawCompoundStmt(CS); 285 OS << "\n"; 286 } else { 287 OS << "\n"; 288 PrintStmt(Node->getBody()); 289 } 290 } 291 292 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { 293 Indent() << "for ("; 294 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) 295 PrintRawDeclStmt(DS); 296 else 297 PrintExpr(cast<Expr>(Node->getElement())); 298 OS << " in "; 299 PrintExpr(Node->getCollection()); 300 OS << ") "; 301 302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 303 PrintRawCompoundStmt(CS); 304 OS << "\n"; 305 } else { 306 OS << "\n"; 307 PrintStmt(Node->getBody()); 308 } 309 } 310 311 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) { 312 Indent() << "for ("; 313 PrintingPolicy SubPolicy(Policy); 314 SubPolicy.SuppressInitializers = true; 315 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel); 316 OS << " : "; 317 PrintExpr(Node->getRangeInit()); 318 OS << ") {\n"; 319 PrintStmt(Node->getBody()); 320 Indent() << "}"; 321 if (Policy.IncludeNewlines) OS << "\n"; 322 } 323 324 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) { 325 Indent(); 326 if (Node->isIfExists()) 327 OS << "__if_exists ("; 328 else 329 OS << "__if_not_exists ("; 330 331 if (NestedNameSpecifier *Qualifier 332 = Node->getQualifierLoc().getNestedNameSpecifier()) 333 Qualifier->print(OS, Policy); 334 335 OS << Node->getNameInfo() << ") "; 336 337 PrintRawCompoundStmt(Node->getSubStmt()); 338 } 339 340 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { 341 Indent() << "goto " << Node->getLabel()->getName() << ";"; 342 if (Policy.IncludeNewlines) OS << "\n"; 343 } 344 345 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 346 Indent() << "goto *"; 347 PrintExpr(Node->getTarget()); 348 OS << ";"; 349 if (Policy.IncludeNewlines) OS << "\n"; 350 } 351 352 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 353 Indent() << "continue;"; 354 if (Policy.IncludeNewlines) OS << "\n"; 355 } 356 357 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 358 Indent() << "break;"; 359 if (Policy.IncludeNewlines) OS << "\n"; 360 } 361 362 363 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 364 Indent() << "return"; 365 if (Node->getRetValue()) { 366 OS << " "; 367 PrintExpr(Node->getRetValue()); 368 } 369 OS << ";"; 370 if (Policy.IncludeNewlines) OS << "\n"; 371 } 372 373 374 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { 375 Indent() << "asm "; 376 377 if (Node->isVolatile()) 378 OS << "volatile "; 379 380 OS << "("; 381 VisitStringLiteral(Node->getAsmString()); 382 383 // Outputs 384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 385 Node->getNumClobbers() != 0) 386 OS << " : "; 387 388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 389 if (i != 0) 390 OS << ", "; 391 392 if (!Node->getOutputName(i).empty()) { 393 OS << '['; 394 OS << Node->getOutputName(i); 395 OS << "] "; 396 } 397 398 VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 399 OS << " "; 400 Visit(Node->getOutputExpr(i)); 401 } 402 403 // Inputs 404 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 405 OS << " : "; 406 407 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 408 if (i != 0) 409 OS << ", "; 410 411 if (!Node->getInputName(i).empty()) { 412 OS << '['; 413 OS << Node->getInputName(i); 414 OS << "] "; 415 } 416 417 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 418 OS << " "; 419 Visit(Node->getInputExpr(i)); 420 } 421 422 // Clobbers 423 if (Node->getNumClobbers() != 0) 424 OS << " : "; 425 426 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 427 if (i != 0) 428 OS << ", "; 429 430 VisitStringLiteral(Node->getClobberStringLiteral(i)); 431 } 432 433 OS << ");"; 434 if (Policy.IncludeNewlines) OS << "\n"; 435 } 436 437 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) { 438 // FIXME: Implement MS style inline asm statement printer. 439 Indent() << "__asm "; 440 if (Node->hasBraces()) 441 OS << "{\n"; 442 OS << Node->getAsmString() << "\n"; 443 if (Node->hasBraces()) 444 Indent() << "}\n"; 445 } 446 447 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) { 448 PrintStmt(Node->getCapturedDecl()->getBody()); 449 } 450 451 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 452 Indent() << "@try"; 453 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 454 PrintRawCompoundStmt(TS); 455 OS << "\n"; 456 } 457 458 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 459 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 460 Indent() << "@catch("; 461 if (catchStmt->getCatchParamDecl()) { 462 if (Decl *DS = catchStmt->getCatchParamDecl()) 463 PrintRawDecl(DS); 464 } 465 OS << ")"; 466 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 467 PrintRawCompoundStmt(CS); 468 OS << "\n"; 469 } 470 } 471 472 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 473 Node->getFinallyStmt())) { 474 Indent() << "@finally"; 475 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 476 OS << "\n"; 477 } 478 } 479 480 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 481 } 482 483 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 484 Indent() << "@catch (...) { /* todo */ } \n"; 485 } 486 487 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 488 Indent() << "@throw"; 489 if (Node->getThrowExpr()) { 490 OS << " "; 491 PrintExpr(Node->getThrowExpr()); 492 } 493 OS << ";\n"; 494 } 495 496 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 497 Indent() << "@synchronized ("; 498 PrintExpr(Node->getSynchExpr()); 499 OS << ")"; 500 PrintRawCompoundStmt(Node->getSynchBody()); 501 OS << "\n"; 502 } 503 504 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 505 Indent() << "@autoreleasepool"; 506 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt())); 507 OS << "\n"; 508 } 509 510 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 511 OS << "catch ("; 512 if (Decl *ExDecl = Node->getExceptionDecl()) 513 PrintRawDecl(ExDecl); 514 else 515 OS << "..."; 516 OS << ") "; 517 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 518 } 519 520 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 521 Indent(); 522 PrintRawCXXCatchStmt(Node); 523 OS << "\n"; 524 } 525 526 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 527 Indent() << "try "; 528 PrintRawCompoundStmt(Node->getTryBlock()); 529 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 530 OS << " "; 531 PrintRawCXXCatchStmt(Node->getHandler(i)); 532 } 533 OS << "\n"; 534 } 535 536 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 537 Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 538 PrintRawCompoundStmt(Node->getTryBlock()); 539 SEHExceptStmt *E = Node->getExceptHandler(); 540 SEHFinallyStmt *F = Node->getFinallyHandler(); 541 if(E) 542 PrintRawSEHExceptHandler(E); 543 else { 544 assert(F && "Must have a finally block..."); 545 PrintRawSEHFinallyStmt(F); 546 } 547 OS << "\n"; 548 } 549 550 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 551 OS << "__finally "; 552 PrintRawCompoundStmt(Node->getBlock()); 553 OS << "\n"; 554 } 555 556 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 557 OS << "__except ("; 558 VisitExpr(Node->getFilterExpr()); 559 OS << ")\n"; 560 PrintRawCompoundStmt(Node->getBlock()); 561 OS << "\n"; 562 } 563 564 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 565 Indent(); 566 PrintRawSEHExceptHandler(Node); 567 OS << "\n"; 568 } 569 570 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 571 Indent(); 572 PrintRawSEHFinallyStmt(Node); 573 OS << "\n"; 574 } 575 576 //===----------------------------------------------------------------------===// 577 // OpenMP clauses printing methods 578 //===----------------------------------------------------------------------===// 579 580 namespace { 581 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> { 582 raw_ostream &OS; 583 const PrintingPolicy &Policy; 584 /// \brief Process clauses with list of variables. 585 template <typename T> 586 void VisitOMPClauseList(T *Node, char StartSym); 587 public: 588 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy) 589 : OS(OS), Policy(Policy) { } 590 #define OPENMP_CLAUSE(Name, Class) \ 591 void Visit##Class(Class *S); 592 #include "clang/Basic/OpenMPKinds.def" 593 }; 594 595 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) { 596 OS << "if("; 597 Node->getCondition()->printPretty(OS, nullptr, Policy, 0); 598 OS << ")"; 599 } 600 601 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) { 602 OS << "num_threads("; 603 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0); 604 OS << ")"; 605 } 606 607 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) { 608 OS << "safelen("; 609 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0); 610 OS << ")"; 611 } 612 613 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) { 614 OS << "collapse("; 615 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0); 616 OS << ")"; 617 } 618 619 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) { 620 OS << "default(" 621 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind()) 622 << ")"; 623 } 624 625 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) { 626 OS << "proc_bind(" 627 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind()) 628 << ")"; 629 } 630 631 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) { 632 OS << "schedule(" 633 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind()); 634 if (Node->getChunkSize()) { 635 OS << ", "; 636 Node->getChunkSize()->printPretty(OS, nullptr, Policy); 637 } 638 OS << ")"; 639 } 640 641 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) { 642 OS << "ordered"; 643 } 644 645 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) { 646 OS << "nowait"; 647 } 648 649 template<typename T> 650 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) { 651 for (typename T::varlist_iterator I = Node->varlist_begin(), 652 E = Node->varlist_end(); 653 I != E; ++I) { 654 assert(*I && "Expected non-null Stmt"); 655 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) { 656 OS << (I == Node->varlist_begin() ? StartSym : ','); 657 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS); 658 } else { 659 OS << (I == Node->varlist_begin() ? StartSym : ','); 660 (*I)->printPretty(OS, nullptr, Policy, 0); 661 } 662 } 663 } 664 665 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) { 666 if (!Node->varlist_empty()) { 667 OS << "private"; 668 VisitOMPClauseList(Node, '('); 669 OS << ")"; 670 } 671 } 672 673 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) { 674 if (!Node->varlist_empty()) { 675 OS << "firstprivate"; 676 VisitOMPClauseList(Node, '('); 677 OS << ")"; 678 } 679 } 680 681 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) { 682 if (!Node->varlist_empty()) { 683 OS << "lastprivate"; 684 VisitOMPClauseList(Node, '('); 685 OS << ")"; 686 } 687 } 688 689 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) { 690 if (!Node->varlist_empty()) { 691 OS << "shared"; 692 VisitOMPClauseList(Node, '('); 693 OS << ")"; 694 } 695 } 696 697 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) { 698 if (!Node->varlist_empty()) { 699 OS << "reduction("; 700 NestedNameSpecifier *QualifierLoc = 701 Node->getQualifierLoc().getNestedNameSpecifier(); 702 OverloadedOperatorKind OOK = 703 Node->getNameInfo().getName().getCXXOverloadedOperator(); 704 if (QualifierLoc == nullptr && OOK != OO_None) { 705 // Print reduction identifier in C format 706 OS << getOperatorSpelling(OOK); 707 } else { 708 // Use C++ format 709 if (QualifierLoc != nullptr) 710 QualifierLoc->print(OS, Policy); 711 OS << Node->getNameInfo(); 712 } 713 OS << ":"; 714 VisitOMPClauseList(Node, ' '); 715 OS << ")"; 716 } 717 } 718 719 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) { 720 if (!Node->varlist_empty()) { 721 OS << "linear"; 722 VisitOMPClauseList(Node, '('); 723 if (Node->getStep() != nullptr) { 724 OS << ": "; 725 Node->getStep()->printPretty(OS, nullptr, Policy, 0); 726 } 727 OS << ")"; 728 } 729 } 730 731 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) { 732 if (!Node->varlist_empty()) { 733 OS << "aligned"; 734 VisitOMPClauseList(Node, '('); 735 if (Node->getAlignment() != nullptr) { 736 OS << ": "; 737 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0); 738 } 739 OS << ")"; 740 } 741 } 742 743 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) { 744 if (!Node->varlist_empty()) { 745 OS << "copyin"; 746 VisitOMPClauseList(Node, '('); 747 OS << ")"; 748 } 749 } 750 751 } 752 753 //===----------------------------------------------------------------------===// 754 // OpenMP directives printing methods 755 //===----------------------------------------------------------------------===// 756 757 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) { 758 OMPClausePrinter Printer(OS, Policy); 759 ArrayRef<OMPClause *> Clauses = S->clauses(); 760 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 761 I != E; ++I) 762 if (*I && !(*I)->isImplicit()) { 763 Printer.Visit(*I); 764 OS << ' '; 765 } 766 OS << "\n"; 767 if (S->getAssociatedStmt()) { 768 assert(isa<CapturedStmt>(S->getAssociatedStmt()) && 769 "Expected captured statement!"); 770 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt(); 771 PrintStmt(CS); 772 } 773 } 774 775 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) { 776 Indent() << "#pragma omp parallel "; 777 PrintOMPExecutableDirective(Node); 778 } 779 780 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) { 781 Indent() << "#pragma omp simd "; 782 PrintOMPExecutableDirective(Node); 783 } 784 785 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) { 786 Indent() << "#pragma omp for "; 787 PrintOMPExecutableDirective(Node); 788 } 789 790 //===----------------------------------------------------------------------===// 791 // Expr printing methods. 792 //===----------------------------------------------------------------------===// 793 794 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 795 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 796 Qualifier->print(OS, Policy); 797 if (Node->hasTemplateKeyword()) 798 OS << "template "; 799 OS << Node->getNameInfo(); 800 if (Node->hasExplicitTemplateArgs()) 801 TemplateSpecializationType::PrintTemplateArgumentList( 802 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 803 } 804 805 void StmtPrinter::VisitDependentScopeDeclRefExpr( 806 DependentScopeDeclRefExpr *Node) { 807 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 808 Qualifier->print(OS, Policy); 809 if (Node->hasTemplateKeyword()) 810 OS << "template "; 811 OS << Node->getNameInfo(); 812 if (Node->hasExplicitTemplateArgs()) 813 TemplateSpecializationType::PrintTemplateArgumentList( 814 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 815 } 816 817 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 818 if (Node->getQualifier()) 819 Node->getQualifier()->print(OS, Policy); 820 if (Node->hasTemplateKeyword()) 821 OS << "template "; 822 OS << Node->getNameInfo(); 823 if (Node->hasExplicitTemplateArgs()) 824 TemplateSpecializationType::PrintTemplateArgumentList( 825 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 826 } 827 828 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 829 if (Node->getBase()) { 830 PrintExpr(Node->getBase()); 831 OS << (Node->isArrow() ? "->" : "."); 832 } 833 OS << *Node->getDecl(); 834 } 835 836 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 837 if (Node->isSuperReceiver()) 838 OS << "super."; 839 else if (Node->isObjectReceiver() && Node->getBase()) { 840 PrintExpr(Node->getBase()); 841 OS << "."; 842 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 843 OS << Node->getClassReceiver()->getName() << "."; 844 } 845 846 if (Node->isImplicitProperty()) 847 Node->getImplicitPropertyGetter()->getSelector().print(OS); 848 else 849 OS << Node->getExplicitProperty()->getName(); 850 } 851 852 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 853 854 PrintExpr(Node->getBaseExpr()); 855 OS << "["; 856 PrintExpr(Node->getKeyExpr()); 857 OS << "]"; 858 } 859 860 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 861 switch (Node->getIdentType()) { 862 default: 863 llvm_unreachable("unknown case"); 864 case PredefinedExpr::Func: 865 OS << "__func__"; 866 break; 867 case PredefinedExpr::Function: 868 OS << "__FUNCTION__"; 869 break; 870 case PredefinedExpr::FuncDName: 871 OS << "__FUNCDNAME__"; 872 break; 873 case PredefinedExpr::FuncSig: 874 OS << "__FUNCSIG__"; 875 break; 876 case PredefinedExpr::LFunction: 877 OS << "L__FUNCTION__"; 878 break; 879 case PredefinedExpr::PrettyFunction: 880 OS << "__PRETTY_FUNCTION__"; 881 break; 882 } 883 } 884 885 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 886 unsigned value = Node->getValue(); 887 888 switch (Node->getKind()) { 889 case CharacterLiteral::Ascii: break; // no prefix. 890 case CharacterLiteral::Wide: OS << 'L'; break; 891 case CharacterLiteral::UTF16: OS << 'u'; break; 892 case CharacterLiteral::UTF32: OS << 'U'; break; 893 } 894 895 switch (value) { 896 case '\\': 897 OS << "'\\\\'"; 898 break; 899 case '\'': 900 OS << "'\\''"; 901 break; 902 case '\a': 903 // TODO: K&R: the meaning of '\\a' is different in traditional C 904 OS << "'\\a'"; 905 break; 906 case '\b': 907 OS << "'\\b'"; 908 break; 909 // Nonstandard escape sequence. 910 /*case '\e': 911 OS << "'\\e'"; 912 break;*/ 913 case '\f': 914 OS << "'\\f'"; 915 break; 916 case '\n': 917 OS << "'\\n'"; 918 break; 919 case '\r': 920 OS << "'\\r'"; 921 break; 922 case '\t': 923 OS << "'\\t'"; 924 break; 925 case '\v': 926 OS << "'\\v'"; 927 break; 928 default: 929 if (value < 256 && isPrintable((unsigned char)value)) 930 OS << "'" << (char)value << "'"; 931 else if (value < 256) 932 OS << "'\\x" << llvm::format("%02x", value) << "'"; 933 else if (value <= 0xFFFF) 934 OS << "'\\u" << llvm::format("%04x", value) << "'"; 935 else 936 OS << "'\\U" << llvm::format("%08x", value) << "'"; 937 } 938 } 939 940 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 941 bool isSigned = Node->getType()->isSignedIntegerType(); 942 OS << Node->getValue().toString(10, isSigned); 943 944 // Emit suffixes. Integer literals are always a builtin integer type. 945 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 946 default: llvm_unreachable("Unexpected type for integer literal!"); 947 case BuiltinType::SChar: OS << "i8"; break; 948 case BuiltinType::UChar: OS << "Ui8"; break; 949 case BuiltinType::Short: OS << "i16"; break; 950 case BuiltinType::UShort: OS << "Ui16"; break; 951 case BuiltinType::Int: break; // no suffix. 952 case BuiltinType::UInt: OS << 'U'; break; 953 case BuiltinType::Long: OS << 'L'; break; 954 case BuiltinType::ULong: OS << "UL"; break; 955 case BuiltinType::LongLong: OS << "LL"; break; 956 case BuiltinType::ULongLong: OS << "ULL"; break; 957 case BuiltinType::Int128: OS << "i128"; break; 958 case BuiltinType::UInt128: OS << "Ui128"; break; 959 } 960 } 961 962 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 963 bool PrintSuffix) { 964 SmallString<16> Str; 965 Node->getValue().toString(Str); 966 OS << Str; 967 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 968 OS << '.'; // Trailing dot in order to separate from ints. 969 970 if (!PrintSuffix) 971 return; 972 973 // Emit suffixes. Float literals are always a builtin float type. 974 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 975 default: llvm_unreachable("Unexpected type for float literal!"); 976 case BuiltinType::Half: break; // FIXME: suffix? 977 case BuiltinType::Double: break; // no suffix. 978 case BuiltinType::Float: OS << 'F'; break; 979 case BuiltinType::LongDouble: OS << 'L'; break; 980 } 981 } 982 983 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 984 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 985 } 986 987 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 988 PrintExpr(Node->getSubExpr()); 989 OS << "i"; 990 } 991 992 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 993 Str->outputString(OS); 994 } 995 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 996 OS << "("; 997 PrintExpr(Node->getSubExpr()); 998 OS << ")"; 999 } 1000 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1001 if (!Node->isPostfix()) { 1002 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1003 1004 // Print a space if this is an "identifier operator" like __real, or if 1005 // it might be concatenated incorrectly like '+'. 1006 switch (Node->getOpcode()) { 1007 default: break; 1008 case UO_Real: 1009 case UO_Imag: 1010 case UO_Extension: 1011 OS << ' '; 1012 break; 1013 case UO_Plus: 1014 case UO_Minus: 1015 if (isa<UnaryOperator>(Node->getSubExpr())) 1016 OS << ' '; 1017 break; 1018 } 1019 } 1020 PrintExpr(Node->getSubExpr()); 1021 1022 if (Node->isPostfix()) 1023 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1024 } 1025 1026 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1027 OS << "__builtin_offsetof("; 1028 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1029 OS << ", "; 1030 bool PrintedSomething = false; 1031 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1032 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 1033 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 1034 // Array node 1035 OS << "["; 1036 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1037 OS << "]"; 1038 PrintedSomething = true; 1039 continue; 1040 } 1041 1042 // Skip implicit base indirections. 1043 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 1044 continue; 1045 1046 // Field or identifier node. 1047 IdentifierInfo *Id = ON.getFieldName(); 1048 if (!Id) 1049 continue; 1050 1051 if (PrintedSomething) 1052 OS << "."; 1053 else 1054 PrintedSomething = true; 1055 OS << Id->getName(); 1056 } 1057 OS << ")"; 1058 } 1059 1060 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 1061 switch(Node->getKind()) { 1062 case UETT_SizeOf: 1063 OS << "sizeof"; 1064 break; 1065 case UETT_AlignOf: 1066 if (Policy.LangOpts.CPlusPlus) 1067 OS << "alignof"; 1068 else if (Policy.LangOpts.C11) 1069 OS << "_Alignof"; 1070 else 1071 OS << "__alignof"; 1072 break; 1073 case UETT_VecStep: 1074 OS << "vec_step"; 1075 break; 1076 } 1077 if (Node->isArgumentType()) { 1078 OS << '('; 1079 Node->getArgumentType().print(OS, Policy); 1080 OS << ')'; 1081 } else { 1082 OS << " "; 1083 PrintExpr(Node->getArgumentExpr()); 1084 } 1085 } 1086 1087 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1088 OS << "_Generic("; 1089 PrintExpr(Node->getControllingExpr()); 1090 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 1091 OS << ", "; 1092 QualType T = Node->getAssocType(i); 1093 if (T.isNull()) 1094 OS << "default"; 1095 else 1096 T.print(OS, Policy); 1097 OS << ": "; 1098 PrintExpr(Node->getAssocExpr(i)); 1099 } 1100 OS << ")"; 1101 } 1102 1103 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1104 PrintExpr(Node->getLHS()); 1105 OS << "["; 1106 PrintExpr(Node->getRHS()); 1107 OS << "]"; 1108 } 1109 1110 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1111 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1112 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1113 // Don't print any defaulted arguments 1114 break; 1115 } 1116 1117 if (i) OS << ", "; 1118 PrintExpr(Call->getArg(i)); 1119 } 1120 } 1121 1122 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1123 PrintExpr(Call->getCallee()); 1124 OS << "("; 1125 PrintCallArgs(Call); 1126 OS << ")"; 1127 } 1128 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1129 // FIXME: Suppress printing implicit bases (like "this") 1130 PrintExpr(Node->getBase()); 1131 1132 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1133 FieldDecl *ParentDecl = ParentMember 1134 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr; 1135 1136 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1137 OS << (Node->isArrow() ? "->" : "."); 1138 1139 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1140 if (FD->isAnonymousStructOrUnion()) 1141 return; 1142 1143 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1144 Qualifier->print(OS, Policy); 1145 if (Node->hasTemplateKeyword()) 1146 OS << "template "; 1147 OS << Node->getMemberNameInfo(); 1148 if (Node->hasExplicitTemplateArgs()) 1149 TemplateSpecializationType::PrintTemplateArgumentList( 1150 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1151 } 1152 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1153 PrintExpr(Node->getBase()); 1154 OS << (Node->isArrow() ? "->isa" : ".isa"); 1155 } 1156 1157 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1158 PrintExpr(Node->getBase()); 1159 OS << "."; 1160 OS << Node->getAccessor().getName(); 1161 } 1162 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1163 OS << '('; 1164 Node->getTypeAsWritten().print(OS, Policy); 1165 OS << ')'; 1166 PrintExpr(Node->getSubExpr()); 1167 } 1168 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1169 OS << '('; 1170 Node->getType().print(OS, Policy); 1171 OS << ')'; 1172 PrintExpr(Node->getInitializer()); 1173 } 1174 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1175 // No need to print anything, simply forward to the subexpression. 1176 PrintExpr(Node->getSubExpr()); 1177 } 1178 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1179 PrintExpr(Node->getLHS()); 1180 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1181 PrintExpr(Node->getRHS()); 1182 } 1183 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1184 PrintExpr(Node->getLHS()); 1185 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1186 PrintExpr(Node->getRHS()); 1187 } 1188 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1189 PrintExpr(Node->getCond()); 1190 OS << " ? "; 1191 PrintExpr(Node->getLHS()); 1192 OS << " : "; 1193 PrintExpr(Node->getRHS()); 1194 } 1195 1196 // GNU extensions. 1197 1198 void 1199 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1200 PrintExpr(Node->getCommon()); 1201 OS << " ?: "; 1202 PrintExpr(Node->getFalseExpr()); 1203 } 1204 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1205 OS << "&&" << Node->getLabel()->getName(); 1206 } 1207 1208 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1209 OS << "("; 1210 PrintRawCompoundStmt(E->getSubStmt()); 1211 OS << ")"; 1212 } 1213 1214 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1215 OS << "__builtin_choose_expr("; 1216 PrintExpr(Node->getCond()); 1217 OS << ", "; 1218 PrintExpr(Node->getLHS()); 1219 OS << ", "; 1220 PrintExpr(Node->getRHS()); 1221 OS << ")"; 1222 } 1223 1224 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1225 OS << "__null"; 1226 } 1227 1228 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1229 OS << "__builtin_shufflevector("; 1230 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1231 if (i) OS << ", "; 1232 PrintExpr(Node->getExpr(i)); 1233 } 1234 OS << ")"; 1235 } 1236 1237 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1238 OS << "__builtin_convertvector("; 1239 PrintExpr(Node->getSrcExpr()); 1240 OS << ", "; 1241 Node->getType().print(OS, Policy); 1242 OS << ")"; 1243 } 1244 1245 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1246 if (Node->getSyntacticForm()) { 1247 Visit(Node->getSyntacticForm()); 1248 return; 1249 } 1250 1251 OS << "{ "; 1252 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1253 if (i) OS << ", "; 1254 if (Node->getInit(i)) 1255 PrintExpr(Node->getInit(i)); 1256 else 1257 OS << "0"; 1258 } 1259 OS << " }"; 1260 } 1261 1262 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1263 OS << "( "; 1264 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1265 if (i) OS << ", "; 1266 PrintExpr(Node->getExpr(i)); 1267 } 1268 OS << " )"; 1269 } 1270 1271 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1272 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1273 DEnd = Node->designators_end(); 1274 D != DEnd; ++D) { 1275 if (D->isFieldDesignator()) { 1276 if (D->getDotLoc().isInvalid()) { 1277 if (IdentifierInfo *II = D->getFieldName()) 1278 OS << II->getName() << ":"; 1279 } else { 1280 OS << "." << D->getFieldName()->getName(); 1281 } 1282 } else { 1283 OS << "["; 1284 if (D->isArrayDesignator()) { 1285 PrintExpr(Node->getArrayIndex(*D)); 1286 } else { 1287 PrintExpr(Node->getArrayRangeStart(*D)); 1288 OS << " ... "; 1289 PrintExpr(Node->getArrayRangeEnd(*D)); 1290 } 1291 OS << "]"; 1292 } 1293 } 1294 1295 OS << " = "; 1296 PrintExpr(Node->getInit()); 1297 } 1298 1299 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1300 if (Policy.LangOpts.CPlusPlus) { 1301 OS << "/*implicit*/"; 1302 Node->getType().print(OS, Policy); 1303 OS << "()"; 1304 } else { 1305 OS << "/*implicit*/("; 1306 Node->getType().print(OS, Policy); 1307 OS << ')'; 1308 if (Node->getType()->isRecordType()) 1309 OS << "{}"; 1310 else 1311 OS << 0; 1312 } 1313 } 1314 1315 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1316 OS << "__builtin_va_arg("; 1317 PrintExpr(Node->getSubExpr()); 1318 OS << ", "; 1319 Node->getType().print(OS, Policy); 1320 OS << ")"; 1321 } 1322 1323 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1324 PrintExpr(Node->getSyntacticForm()); 1325 } 1326 1327 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1328 const char *Name = nullptr; 1329 switch (Node->getOp()) { 1330 #define BUILTIN(ID, TYPE, ATTRS) 1331 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1332 case AtomicExpr::AO ## ID: \ 1333 Name = #ID "("; \ 1334 break; 1335 #include "clang/Basic/Builtins.def" 1336 } 1337 OS << Name; 1338 1339 // AtomicExpr stores its subexpressions in a permuted order. 1340 PrintExpr(Node->getPtr()); 1341 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1342 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1343 OS << ", "; 1344 PrintExpr(Node->getVal1()); 1345 } 1346 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1347 Node->isCmpXChg()) { 1348 OS << ", "; 1349 PrintExpr(Node->getVal2()); 1350 } 1351 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1352 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1353 OS << ", "; 1354 PrintExpr(Node->getWeak()); 1355 } 1356 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1357 OS << ", "; 1358 PrintExpr(Node->getOrder()); 1359 } 1360 if (Node->isCmpXChg()) { 1361 OS << ", "; 1362 PrintExpr(Node->getOrderFail()); 1363 } 1364 OS << ")"; 1365 } 1366 1367 // C++ 1368 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1369 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1370 "", 1371 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1372 Spelling, 1373 #include "clang/Basic/OperatorKinds.def" 1374 }; 1375 1376 OverloadedOperatorKind Kind = Node->getOperator(); 1377 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1378 if (Node->getNumArgs() == 1) { 1379 OS << OpStrings[Kind] << ' '; 1380 PrintExpr(Node->getArg(0)); 1381 } else { 1382 PrintExpr(Node->getArg(0)); 1383 OS << ' ' << OpStrings[Kind]; 1384 } 1385 } else if (Kind == OO_Arrow) { 1386 PrintExpr(Node->getArg(0)); 1387 } else if (Kind == OO_Call) { 1388 PrintExpr(Node->getArg(0)); 1389 OS << '('; 1390 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1391 if (ArgIdx > 1) 1392 OS << ", "; 1393 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1394 PrintExpr(Node->getArg(ArgIdx)); 1395 } 1396 OS << ')'; 1397 } else if (Kind == OO_Subscript) { 1398 PrintExpr(Node->getArg(0)); 1399 OS << '['; 1400 PrintExpr(Node->getArg(1)); 1401 OS << ']'; 1402 } else if (Node->getNumArgs() == 1) { 1403 OS << OpStrings[Kind] << ' '; 1404 PrintExpr(Node->getArg(0)); 1405 } else if (Node->getNumArgs() == 2) { 1406 PrintExpr(Node->getArg(0)); 1407 OS << ' ' << OpStrings[Kind] << ' '; 1408 PrintExpr(Node->getArg(1)); 1409 } else { 1410 llvm_unreachable("unknown overloaded operator"); 1411 } 1412 } 1413 1414 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1415 // If we have a conversion operator call only print the argument. 1416 CXXMethodDecl *MD = Node->getMethodDecl(); 1417 if (MD && isa<CXXConversionDecl>(MD)) { 1418 PrintExpr(Node->getImplicitObjectArgument()); 1419 return; 1420 } 1421 VisitCallExpr(cast<CallExpr>(Node)); 1422 } 1423 1424 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1425 PrintExpr(Node->getCallee()); 1426 OS << "<<<"; 1427 PrintCallArgs(Node->getConfig()); 1428 OS << ">>>("; 1429 PrintCallArgs(Node); 1430 OS << ")"; 1431 } 1432 1433 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1434 OS << Node->getCastName() << '<'; 1435 Node->getTypeAsWritten().print(OS, Policy); 1436 OS << ">("; 1437 PrintExpr(Node->getSubExpr()); 1438 OS << ")"; 1439 } 1440 1441 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1442 VisitCXXNamedCastExpr(Node); 1443 } 1444 1445 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1446 VisitCXXNamedCastExpr(Node); 1447 } 1448 1449 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1450 VisitCXXNamedCastExpr(Node); 1451 } 1452 1453 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1454 VisitCXXNamedCastExpr(Node); 1455 } 1456 1457 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1458 OS << "typeid("; 1459 if (Node->isTypeOperand()) { 1460 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1461 } else { 1462 PrintExpr(Node->getExprOperand()); 1463 } 1464 OS << ")"; 1465 } 1466 1467 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1468 OS << "__uuidof("; 1469 if (Node->isTypeOperand()) { 1470 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1471 } else { 1472 PrintExpr(Node->getExprOperand()); 1473 } 1474 OS << ")"; 1475 } 1476 1477 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1478 PrintExpr(Node->getBaseExpr()); 1479 if (Node->isArrow()) 1480 OS << "->"; 1481 else 1482 OS << "."; 1483 if (NestedNameSpecifier *Qualifier = 1484 Node->getQualifierLoc().getNestedNameSpecifier()) 1485 Qualifier->print(OS, Policy); 1486 OS << Node->getPropertyDecl()->getDeclName(); 1487 } 1488 1489 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1490 switch (Node->getLiteralOperatorKind()) { 1491 case UserDefinedLiteral::LOK_Raw: 1492 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1493 break; 1494 case UserDefinedLiteral::LOK_Template: { 1495 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1496 const TemplateArgumentList *Args = 1497 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1498 assert(Args); 1499 const TemplateArgument &Pack = Args->get(0); 1500 for (TemplateArgument::pack_iterator I = Pack.pack_begin(), 1501 E = Pack.pack_end(); I != E; ++I) { 1502 char C = (char)I->getAsIntegral().getZExtValue(); 1503 OS << C; 1504 } 1505 break; 1506 } 1507 case UserDefinedLiteral::LOK_Integer: { 1508 // Print integer literal without suffix. 1509 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1510 OS << Int->getValue().toString(10, /*isSigned*/false); 1511 break; 1512 } 1513 case UserDefinedLiteral::LOK_Floating: { 1514 // Print floating literal without suffix. 1515 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1516 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1517 break; 1518 } 1519 case UserDefinedLiteral::LOK_String: 1520 case UserDefinedLiteral::LOK_Character: 1521 PrintExpr(Node->getCookedLiteral()); 1522 break; 1523 } 1524 OS << Node->getUDSuffix()->getName(); 1525 } 1526 1527 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1528 OS << (Node->getValue() ? "true" : "false"); 1529 } 1530 1531 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1532 OS << "nullptr"; 1533 } 1534 1535 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1536 OS << "this"; 1537 } 1538 1539 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1540 if (!Node->getSubExpr()) 1541 OS << "throw"; 1542 else { 1543 OS << "throw "; 1544 PrintExpr(Node->getSubExpr()); 1545 } 1546 } 1547 1548 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1549 // Nothing to print: we picked up the default argument. 1550 } 1551 1552 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1553 // Nothing to print: we picked up the default initializer. 1554 } 1555 1556 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1557 Node->getType().print(OS, Policy); 1558 OS << "("; 1559 PrintExpr(Node->getSubExpr()); 1560 OS << ")"; 1561 } 1562 1563 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1564 PrintExpr(Node->getSubExpr()); 1565 } 1566 1567 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1568 Node->getType().print(OS, Policy); 1569 OS << "("; 1570 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1571 ArgEnd = Node->arg_end(); 1572 Arg != ArgEnd; ++Arg) { 1573 if (Arg->isDefaultArgument()) 1574 break; 1575 if (Arg != Node->arg_begin()) 1576 OS << ", "; 1577 PrintExpr(*Arg); 1578 } 1579 OS << ")"; 1580 } 1581 1582 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1583 OS << '['; 1584 bool NeedComma = false; 1585 switch (Node->getCaptureDefault()) { 1586 case LCD_None: 1587 break; 1588 1589 case LCD_ByCopy: 1590 OS << '='; 1591 NeedComma = true; 1592 break; 1593 1594 case LCD_ByRef: 1595 OS << '&'; 1596 NeedComma = true; 1597 break; 1598 } 1599 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1600 CEnd = Node->explicit_capture_end(); 1601 C != CEnd; 1602 ++C) { 1603 if (NeedComma) 1604 OS << ", "; 1605 NeedComma = true; 1606 1607 switch (C->getCaptureKind()) { 1608 case LCK_This: 1609 OS << "this"; 1610 break; 1611 1612 case LCK_ByRef: 1613 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture()) 1614 OS << '&'; 1615 OS << C->getCapturedVar()->getName(); 1616 break; 1617 1618 case LCK_ByCopy: 1619 OS << C->getCapturedVar()->getName(); 1620 break; 1621 } 1622 1623 if (C->isInitCapture()) 1624 PrintExpr(C->getCapturedVar()->getInit()); 1625 } 1626 OS << ']'; 1627 1628 if (Node->hasExplicitParameters()) { 1629 OS << " ("; 1630 CXXMethodDecl *Method = Node->getCallOperator(); 1631 NeedComma = false; 1632 for (auto P : Method->params()) { 1633 if (NeedComma) { 1634 OS << ", "; 1635 } else { 1636 NeedComma = true; 1637 } 1638 std::string ParamStr = P->getNameAsString(); 1639 P->getOriginalType().print(OS, Policy, ParamStr); 1640 } 1641 if (Method->isVariadic()) { 1642 if (NeedComma) 1643 OS << ", "; 1644 OS << "..."; 1645 } 1646 OS << ')'; 1647 1648 if (Node->isMutable()) 1649 OS << " mutable"; 1650 1651 const FunctionProtoType *Proto 1652 = Method->getType()->getAs<FunctionProtoType>(); 1653 Proto->printExceptionSpecification(OS, Policy); 1654 1655 // FIXME: Attributes 1656 1657 // Print the trailing return type if it was specified in the source. 1658 if (Node->hasExplicitResultType()) { 1659 OS << " -> "; 1660 Proto->getReturnType().print(OS, Policy); 1661 } 1662 } 1663 1664 // Print the body. 1665 CompoundStmt *Body = Node->getBody(); 1666 OS << ' '; 1667 PrintStmt(Body); 1668 } 1669 1670 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1671 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1672 TSInfo->getType().print(OS, Policy); 1673 else 1674 Node->getType().print(OS, Policy); 1675 OS << "()"; 1676 } 1677 1678 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1679 if (E->isGlobalNew()) 1680 OS << "::"; 1681 OS << "new "; 1682 unsigned NumPlace = E->getNumPlacementArgs(); 1683 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1684 OS << "("; 1685 PrintExpr(E->getPlacementArg(0)); 1686 for (unsigned i = 1; i < NumPlace; ++i) { 1687 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1688 break; 1689 OS << ", "; 1690 PrintExpr(E->getPlacementArg(i)); 1691 } 1692 OS << ") "; 1693 } 1694 if (E->isParenTypeId()) 1695 OS << "("; 1696 std::string TypeS; 1697 if (Expr *Size = E->getArraySize()) { 1698 llvm::raw_string_ostream s(TypeS); 1699 s << '['; 1700 Size->printPretty(s, Helper, Policy); 1701 s << ']'; 1702 } 1703 E->getAllocatedType().print(OS, Policy, TypeS); 1704 if (E->isParenTypeId()) 1705 OS << ")"; 1706 1707 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1708 if (InitStyle) { 1709 if (InitStyle == CXXNewExpr::CallInit) 1710 OS << "("; 1711 PrintExpr(E->getInitializer()); 1712 if (InitStyle == CXXNewExpr::CallInit) 1713 OS << ")"; 1714 } 1715 } 1716 1717 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1718 if (E->isGlobalDelete()) 1719 OS << "::"; 1720 OS << "delete "; 1721 if (E->isArrayForm()) 1722 OS << "[] "; 1723 PrintExpr(E->getArgument()); 1724 } 1725 1726 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1727 PrintExpr(E->getBase()); 1728 if (E->isArrow()) 1729 OS << "->"; 1730 else 1731 OS << '.'; 1732 if (E->getQualifier()) 1733 E->getQualifier()->print(OS, Policy); 1734 OS << "~"; 1735 1736 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1737 OS << II->getName(); 1738 else 1739 E->getDestroyedType().print(OS, Policy); 1740 } 1741 1742 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1743 if (E->isListInitialization()) 1744 OS << "{ "; 1745 1746 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1747 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1748 // Don't print any defaulted arguments 1749 break; 1750 } 1751 1752 if (i) OS << ", "; 1753 PrintExpr(E->getArg(i)); 1754 } 1755 1756 if (E->isListInitialization()) 1757 OS << " }"; 1758 } 1759 1760 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 1761 PrintExpr(E->getSubExpr()); 1762 } 1763 1764 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1765 // Just forward to the subexpression. 1766 PrintExpr(E->getSubExpr()); 1767 } 1768 1769 void 1770 StmtPrinter::VisitCXXUnresolvedConstructExpr( 1771 CXXUnresolvedConstructExpr *Node) { 1772 Node->getTypeAsWritten().print(OS, Policy); 1773 OS << "("; 1774 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1775 ArgEnd = Node->arg_end(); 1776 Arg != ArgEnd; ++Arg) { 1777 if (Arg != Node->arg_begin()) 1778 OS << ", "; 1779 PrintExpr(*Arg); 1780 } 1781 OS << ")"; 1782 } 1783 1784 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1785 CXXDependentScopeMemberExpr *Node) { 1786 if (!Node->isImplicitAccess()) { 1787 PrintExpr(Node->getBase()); 1788 OS << (Node->isArrow() ? "->" : "."); 1789 } 1790 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1791 Qualifier->print(OS, Policy); 1792 if (Node->hasTemplateKeyword()) 1793 OS << "template "; 1794 OS << Node->getMemberNameInfo(); 1795 if (Node->hasExplicitTemplateArgs()) 1796 TemplateSpecializationType::PrintTemplateArgumentList( 1797 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1798 } 1799 1800 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1801 if (!Node->isImplicitAccess()) { 1802 PrintExpr(Node->getBase()); 1803 OS << (Node->isArrow() ? "->" : "."); 1804 } 1805 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1806 Qualifier->print(OS, Policy); 1807 if (Node->hasTemplateKeyword()) 1808 OS << "template "; 1809 OS << Node->getMemberNameInfo(); 1810 if (Node->hasExplicitTemplateArgs()) 1811 TemplateSpecializationType::PrintTemplateArgumentList( 1812 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1813 } 1814 1815 static const char *getTypeTraitName(TypeTrait TT) { 1816 switch (TT) { 1817 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 1818 case clang::UTT_##Name: return #Spelling; 1819 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 1820 case clang::BTT_##Name: return #Spelling; 1821 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 1822 case clang::TT_##Name: return #Spelling; 1823 #include "clang/Basic/TokenKinds.def" 1824 } 1825 llvm_unreachable("Type trait not covered by switch"); 1826 } 1827 1828 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1829 switch (ATT) { 1830 case ATT_ArrayRank: return "__array_rank"; 1831 case ATT_ArrayExtent: return "__array_extent"; 1832 } 1833 llvm_unreachable("Array type trait not covered by switch"); 1834 } 1835 1836 static const char *getExpressionTraitName(ExpressionTrait ET) { 1837 switch (ET) { 1838 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1839 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1840 } 1841 llvm_unreachable("Expression type trait not covered by switch"); 1842 } 1843 1844 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 1845 OS << getTypeTraitName(E->getTrait()) << "("; 1846 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 1847 if (I > 0) 1848 OS << ", "; 1849 E->getArg(I)->getType().print(OS, Policy); 1850 } 1851 OS << ")"; 1852 } 1853 1854 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1855 OS << getTypeTraitName(E->getTrait()) << '('; 1856 E->getQueriedType().print(OS, Policy); 1857 OS << ')'; 1858 } 1859 1860 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1861 OS << getExpressionTraitName(E->getTrait()) << '('; 1862 PrintExpr(E->getQueriedExpression()); 1863 OS << ')'; 1864 } 1865 1866 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1867 OS << "noexcept("; 1868 PrintExpr(E->getOperand()); 1869 OS << ")"; 1870 } 1871 1872 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1873 PrintExpr(E->getPattern()); 1874 OS << "..."; 1875 } 1876 1877 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1878 OS << "sizeof...(" << *E->getPack() << ")"; 1879 } 1880 1881 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1882 SubstNonTypeTemplateParmPackExpr *Node) { 1883 OS << *Node->getParameterPack(); 1884 } 1885 1886 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1887 SubstNonTypeTemplateParmExpr *Node) { 1888 Visit(Node->getReplacement()); 1889 } 1890 1891 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 1892 OS << *E->getParameterPack(); 1893 } 1894 1895 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1896 PrintExpr(Node->GetTemporaryExpr()); 1897 } 1898 1899 // Obj-C 1900 1901 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1902 OS << "@"; 1903 VisitStringLiteral(Node->getString()); 1904 } 1905 1906 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 1907 OS << "@"; 1908 Visit(E->getSubExpr()); 1909 } 1910 1911 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 1912 OS << "@[ "; 1913 StmtRange ch = E->children(); 1914 if (ch.first != ch.second) { 1915 while (1) { 1916 Visit(*ch.first); 1917 ++ch.first; 1918 if (ch.first == ch.second) break; 1919 OS << ", "; 1920 } 1921 } 1922 OS << " ]"; 1923 } 1924 1925 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 1926 OS << "@{ "; 1927 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 1928 if (I > 0) 1929 OS << ", "; 1930 1931 ObjCDictionaryElement Element = E->getKeyValueElement(I); 1932 Visit(Element.Key); 1933 OS << " : "; 1934 Visit(Element.Value); 1935 if (Element.isPackExpansion()) 1936 OS << "..."; 1937 } 1938 OS << " }"; 1939 } 1940 1941 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1942 OS << "@encode("; 1943 Node->getEncodedType().print(OS, Policy); 1944 OS << ')'; 1945 } 1946 1947 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1948 OS << "@selector("; 1949 Node->getSelector().print(OS); 1950 OS << ')'; 1951 } 1952 1953 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1954 OS << "@protocol(" << *Node->getProtocol() << ')'; 1955 } 1956 1957 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1958 OS << "["; 1959 switch (Mess->getReceiverKind()) { 1960 case ObjCMessageExpr::Instance: 1961 PrintExpr(Mess->getInstanceReceiver()); 1962 break; 1963 1964 case ObjCMessageExpr::Class: 1965 Mess->getClassReceiver().print(OS, Policy); 1966 break; 1967 1968 case ObjCMessageExpr::SuperInstance: 1969 case ObjCMessageExpr::SuperClass: 1970 OS << "Super"; 1971 break; 1972 } 1973 1974 OS << ' '; 1975 Selector selector = Mess->getSelector(); 1976 if (selector.isUnarySelector()) { 1977 OS << selector.getNameForSlot(0); 1978 } else { 1979 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1980 if (i < selector.getNumArgs()) { 1981 if (i > 0) OS << ' '; 1982 if (selector.getIdentifierInfoForSlot(i)) 1983 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1984 else 1985 OS << ":"; 1986 } 1987 else OS << ", "; // Handle variadic methods. 1988 1989 PrintExpr(Mess->getArg(i)); 1990 } 1991 } 1992 OS << "]"; 1993 } 1994 1995 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 1996 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1997 } 1998 1999 void 2000 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2001 PrintExpr(E->getSubExpr()); 2002 } 2003 2004 void 2005 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2006 OS << '(' << E->getBridgeKindName(); 2007 E->getType().print(OS, Policy); 2008 OS << ')'; 2009 PrintExpr(E->getSubExpr()); 2010 } 2011 2012 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2013 BlockDecl *BD = Node->getBlockDecl(); 2014 OS << "^"; 2015 2016 const FunctionType *AFT = Node->getFunctionType(); 2017 2018 if (isa<FunctionNoProtoType>(AFT)) { 2019 OS << "()"; 2020 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2021 OS << '('; 2022 for (BlockDecl::param_iterator AI = BD->param_begin(), 2023 E = BD->param_end(); AI != E; ++AI) { 2024 if (AI != BD->param_begin()) OS << ", "; 2025 std::string ParamStr = (*AI)->getNameAsString(); 2026 (*AI)->getType().print(OS, Policy, ParamStr); 2027 } 2028 2029 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2030 if (FT->isVariadic()) { 2031 if (!BD->param_empty()) OS << ", "; 2032 OS << "..."; 2033 } 2034 OS << ')'; 2035 } 2036 OS << "{ }"; 2037 } 2038 2039 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2040 PrintExpr(Node->getSourceExpr()); 2041 } 2042 2043 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2044 OS << "__builtin_astype("; 2045 PrintExpr(Node->getSrcExpr()); 2046 OS << ", "; 2047 Node->getType().print(OS, Policy); 2048 OS << ")"; 2049 } 2050 2051 //===----------------------------------------------------------------------===// 2052 // Stmt method implementations 2053 //===----------------------------------------------------------------------===// 2054 2055 void Stmt::dumpPretty(const ASTContext &Context) const { 2056 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2057 } 2058 2059 void Stmt::printPretty(raw_ostream &OS, 2060 PrinterHelper *Helper, 2061 const PrintingPolicy &Policy, 2062 unsigned Indentation) const { 2063 StmtPrinter P(OS, Helper, Policy, Indentation); 2064 P.Visit(const_cast<Stmt*>(this)); 2065 } 2066 2067 //===----------------------------------------------------------------------===// 2068 // PrinterHelper 2069 //===----------------------------------------------------------------------===// 2070 2071 // Implement virtual destructor. 2072 PrinterHelper::~PrinterHelper() {} 2073