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 // FIXME: The Short and UShort cases are to handle cases where a short 948 // integeral literal is formed during template instantiation. They should 949 // be removed when template instantiation no longer needs integer literals. 950 case BuiltinType::Short: 951 case BuiltinType::UShort: 952 case BuiltinType::Int: break; // no suffix. 953 case BuiltinType::UInt: OS << 'U'; break; 954 case BuiltinType::Long: OS << 'L'; break; 955 case BuiltinType::ULong: OS << "UL"; break; 956 case BuiltinType::LongLong: OS << "LL"; break; 957 case BuiltinType::ULongLong: OS << "ULL"; break; 958 case BuiltinType::Int128: OS << "i128"; break; 959 case BuiltinType::UInt128: OS << "Ui128"; break; 960 } 961 } 962 963 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 964 bool PrintSuffix) { 965 SmallString<16> Str; 966 Node->getValue().toString(Str); 967 OS << Str; 968 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 969 OS << '.'; // Trailing dot in order to separate from ints. 970 971 if (!PrintSuffix) 972 return; 973 974 // Emit suffixes. Float literals are always a builtin float type. 975 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 976 default: llvm_unreachable("Unexpected type for float literal!"); 977 case BuiltinType::Half: break; // FIXME: suffix? 978 case BuiltinType::Double: break; // no suffix. 979 case BuiltinType::Float: OS << 'F'; break; 980 case BuiltinType::LongDouble: OS << 'L'; break; 981 } 982 } 983 984 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 985 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 986 } 987 988 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 989 PrintExpr(Node->getSubExpr()); 990 OS << "i"; 991 } 992 993 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 994 Str->outputString(OS); 995 } 996 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 997 OS << "("; 998 PrintExpr(Node->getSubExpr()); 999 OS << ")"; 1000 } 1001 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1002 if (!Node->isPostfix()) { 1003 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1004 1005 // Print a space if this is an "identifier operator" like __real, or if 1006 // it might be concatenated incorrectly like '+'. 1007 switch (Node->getOpcode()) { 1008 default: break; 1009 case UO_Real: 1010 case UO_Imag: 1011 case UO_Extension: 1012 OS << ' '; 1013 break; 1014 case UO_Plus: 1015 case UO_Minus: 1016 if (isa<UnaryOperator>(Node->getSubExpr())) 1017 OS << ' '; 1018 break; 1019 } 1020 } 1021 PrintExpr(Node->getSubExpr()); 1022 1023 if (Node->isPostfix()) 1024 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1025 } 1026 1027 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1028 OS << "__builtin_offsetof("; 1029 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1030 OS << ", "; 1031 bool PrintedSomething = false; 1032 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1033 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 1034 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 1035 // Array node 1036 OS << "["; 1037 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1038 OS << "]"; 1039 PrintedSomething = true; 1040 continue; 1041 } 1042 1043 // Skip implicit base indirections. 1044 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 1045 continue; 1046 1047 // Field or identifier node. 1048 IdentifierInfo *Id = ON.getFieldName(); 1049 if (!Id) 1050 continue; 1051 1052 if (PrintedSomething) 1053 OS << "."; 1054 else 1055 PrintedSomething = true; 1056 OS << Id->getName(); 1057 } 1058 OS << ")"; 1059 } 1060 1061 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 1062 switch(Node->getKind()) { 1063 case UETT_SizeOf: 1064 OS << "sizeof"; 1065 break; 1066 case UETT_AlignOf: 1067 if (Policy.LangOpts.CPlusPlus) 1068 OS << "alignof"; 1069 else if (Policy.LangOpts.C11) 1070 OS << "_Alignof"; 1071 else 1072 OS << "__alignof"; 1073 break; 1074 case UETT_VecStep: 1075 OS << "vec_step"; 1076 break; 1077 } 1078 if (Node->isArgumentType()) { 1079 OS << '('; 1080 Node->getArgumentType().print(OS, Policy); 1081 OS << ')'; 1082 } else { 1083 OS << " "; 1084 PrintExpr(Node->getArgumentExpr()); 1085 } 1086 } 1087 1088 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1089 OS << "_Generic("; 1090 PrintExpr(Node->getControllingExpr()); 1091 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 1092 OS << ", "; 1093 QualType T = Node->getAssocType(i); 1094 if (T.isNull()) 1095 OS << "default"; 1096 else 1097 T.print(OS, Policy); 1098 OS << ": "; 1099 PrintExpr(Node->getAssocExpr(i)); 1100 } 1101 OS << ")"; 1102 } 1103 1104 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1105 PrintExpr(Node->getLHS()); 1106 OS << "["; 1107 PrintExpr(Node->getRHS()); 1108 OS << "]"; 1109 } 1110 1111 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1112 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1113 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1114 // Don't print any defaulted arguments 1115 break; 1116 } 1117 1118 if (i) OS << ", "; 1119 PrintExpr(Call->getArg(i)); 1120 } 1121 } 1122 1123 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1124 PrintExpr(Call->getCallee()); 1125 OS << "("; 1126 PrintCallArgs(Call); 1127 OS << ")"; 1128 } 1129 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1130 // FIXME: Suppress printing implicit bases (like "this") 1131 PrintExpr(Node->getBase()); 1132 1133 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1134 FieldDecl *ParentDecl = ParentMember 1135 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr; 1136 1137 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1138 OS << (Node->isArrow() ? "->" : "."); 1139 1140 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1141 if (FD->isAnonymousStructOrUnion()) 1142 return; 1143 1144 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1145 Qualifier->print(OS, Policy); 1146 if (Node->hasTemplateKeyword()) 1147 OS << "template "; 1148 OS << Node->getMemberNameInfo(); 1149 if (Node->hasExplicitTemplateArgs()) 1150 TemplateSpecializationType::PrintTemplateArgumentList( 1151 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1152 } 1153 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1154 PrintExpr(Node->getBase()); 1155 OS << (Node->isArrow() ? "->isa" : ".isa"); 1156 } 1157 1158 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1159 PrintExpr(Node->getBase()); 1160 OS << "."; 1161 OS << Node->getAccessor().getName(); 1162 } 1163 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1164 OS << '('; 1165 Node->getTypeAsWritten().print(OS, Policy); 1166 OS << ')'; 1167 PrintExpr(Node->getSubExpr()); 1168 } 1169 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1170 OS << '('; 1171 Node->getType().print(OS, Policy); 1172 OS << ')'; 1173 PrintExpr(Node->getInitializer()); 1174 } 1175 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1176 // No need to print anything, simply forward to the subexpression. 1177 PrintExpr(Node->getSubExpr()); 1178 } 1179 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1180 PrintExpr(Node->getLHS()); 1181 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1182 PrintExpr(Node->getRHS()); 1183 } 1184 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1185 PrintExpr(Node->getLHS()); 1186 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1187 PrintExpr(Node->getRHS()); 1188 } 1189 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1190 PrintExpr(Node->getCond()); 1191 OS << " ? "; 1192 PrintExpr(Node->getLHS()); 1193 OS << " : "; 1194 PrintExpr(Node->getRHS()); 1195 } 1196 1197 // GNU extensions. 1198 1199 void 1200 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1201 PrintExpr(Node->getCommon()); 1202 OS << " ?: "; 1203 PrintExpr(Node->getFalseExpr()); 1204 } 1205 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1206 OS << "&&" << Node->getLabel()->getName(); 1207 } 1208 1209 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1210 OS << "("; 1211 PrintRawCompoundStmt(E->getSubStmt()); 1212 OS << ")"; 1213 } 1214 1215 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1216 OS << "__builtin_choose_expr("; 1217 PrintExpr(Node->getCond()); 1218 OS << ", "; 1219 PrintExpr(Node->getLHS()); 1220 OS << ", "; 1221 PrintExpr(Node->getRHS()); 1222 OS << ")"; 1223 } 1224 1225 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1226 OS << "__null"; 1227 } 1228 1229 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1230 OS << "__builtin_shufflevector("; 1231 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1232 if (i) OS << ", "; 1233 PrintExpr(Node->getExpr(i)); 1234 } 1235 OS << ")"; 1236 } 1237 1238 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1239 OS << "__builtin_convertvector("; 1240 PrintExpr(Node->getSrcExpr()); 1241 OS << ", "; 1242 Node->getType().print(OS, Policy); 1243 OS << ")"; 1244 } 1245 1246 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1247 if (Node->getSyntacticForm()) { 1248 Visit(Node->getSyntacticForm()); 1249 return; 1250 } 1251 1252 OS << "{ "; 1253 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1254 if (i) OS << ", "; 1255 if (Node->getInit(i)) 1256 PrintExpr(Node->getInit(i)); 1257 else 1258 OS << "0"; 1259 } 1260 OS << " }"; 1261 } 1262 1263 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1264 OS << "( "; 1265 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1266 if (i) OS << ", "; 1267 PrintExpr(Node->getExpr(i)); 1268 } 1269 OS << " )"; 1270 } 1271 1272 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1273 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1274 DEnd = Node->designators_end(); 1275 D != DEnd; ++D) { 1276 if (D->isFieldDesignator()) { 1277 if (D->getDotLoc().isInvalid()) 1278 OS << D->getFieldName()->getName() << ":"; 1279 else 1280 OS << "." << D->getFieldName()->getName(); 1281 } else { 1282 OS << "["; 1283 if (D->isArrayDesignator()) { 1284 PrintExpr(Node->getArrayIndex(*D)); 1285 } else { 1286 PrintExpr(Node->getArrayRangeStart(*D)); 1287 OS << " ... "; 1288 PrintExpr(Node->getArrayRangeEnd(*D)); 1289 } 1290 OS << "]"; 1291 } 1292 } 1293 1294 OS << " = "; 1295 PrintExpr(Node->getInit()); 1296 } 1297 1298 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1299 if (Policy.LangOpts.CPlusPlus) { 1300 OS << "/*implicit*/"; 1301 Node->getType().print(OS, Policy); 1302 OS << "()"; 1303 } else { 1304 OS << "/*implicit*/("; 1305 Node->getType().print(OS, Policy); 1306 OS << ')'; 1307 if (Node->getType()->isRecordType()) 1308 OS << "{}"; 1309 else 1310 OS << 0; 1311 } 1312 } 1313 1314 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1315 OS << "__builtin_va_arg("; 1316 PrintExpr(Node->getSubExpr()); 1317 OS << ", "; 1318 Node->getType().print(OS, Policy); 1319 OS << ")"; 1320 } 1321 1322 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1323 PrintExpr(Node->getSyntacticForm()); 1324 } 1325 1326 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1327 const char *Name = nullptr; 1328 switch (Node->getOp()) { 1329 #define BUILTIN(ID, TYPE, ATTRS) 1330 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1331 case AtomicExpr::AO ## ID: \ 1332 Name = #ID "("; \ 1333 break; 1334 #include "clang/Basic/Builtins.def" 1335 } 1336 OS << Name; 1337 1338 // AtomicExpr stores its subexpressions in a permuted order. 1339 PrintExpr(Node->getPtr()); 1340 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1341 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1342 OS << ", "; 1343 PrintExpr(Node->getVal1()); 1344 } 1345 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1346 Node->isCmpXChg()) { 1347 OS << ", "; 1348 PrintExpr(Node->getVal2()); 1349 } 1350 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1351 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1352 OS << ", "; 1353 PrintExpr(Node->getWeak()); 1354 } 1355 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1356 OS << ", "; 1357 PrintExpr(Node->getOrder()); 1358 } 1359 if (Node->isCmpXChg()) { 1360 OS << ", "; 1361 PrintExpr(Node->getOrderFail()); 1362 } 1363 OS << ")"; 1364 } 1365 1366 // C++ 1367 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1368 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1369 "", 1370 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1371 Spelling, 1372 #include "clang/Basic/OperatorKinds.def" 1373 }; 1374 1375 OverloadedOperatorKind Kind = Node->getOperator(); 1376 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1377 if (Node->getNumArgs() == 1) { 1378 OS << OpStrings[Kind] << ' '; 1379 PrintExpr(Node->getArg(0)); 1380 } else { 1381 PrintExpr(Node->getArg(0)); 1382 OS << ' ' << OpStrings[Kind]; 1383 } 1384 } else if (Kind == OO_Arrow) { 1385 PrintExpr(Node->getArg(0)); 1386 } else if (Kind == OO_Call) { 1387 PrintExpr(Node->getArg(0)); 1388 OS << '('; 1389 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1390 if (ArgIdx > 1) 1391 OS << ", "; 1392 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1393 PrintExpr(Node->getArg(ArgIdx)); 1394 } 1395 OS << ')'; 1396 } else if (Kind == OO_Subscript) { 1397 PrintExpr(Node->getArg(0)); 1398 OS << '['; 1399 PrintExpr(Node->getArg(1)); 1400 OS << ']'; 1401 } else if (Node->getNumArgs() == 1) { 1402 OS << OpStrings[Kind] << ' '; 1403 PrintExpr(Node->getArg(0)); 1404 } else if (Node->getNumArgs() == 2) { 1405 PrintExpr(Node->getArg(0)); 1406 OS << ' ' << OpStrings[Kind] << ' '; 1407 PrintExpr(Node->getArg(1)); 1408 } else { 1409 llvm_unreachable("unknown overloaded operator"); 1410 } 1411 } 1412 1413 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1414 // If we have a conversion operator call only print the argument. 1415 CXXMethodDecl *MD = Node->getMethodDecl(); 1416 if (MD && isa<CXXConversionDecl>(MD)) { 1417 PrintExpr(Node->getImplicitObjectArgument()); 1418 return; 1419 } 1420 VisitCallExpr(cast<CallExpr>(Node)); 1421 } 1422 1423 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1424 PrintExpr(Node->getCallee()); 1425 OS << "<<<"; 1426 PrintCallArgs(Node->getConfig()); 1427 OS << ">>>("; 1428 PrintCallArgs(Node); 1429 OS << ")"; 1430 } 1431 1432 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1433 OS << Node->getCastName() << '<'; 1434 Node->getTypeAsWritten().print(OS, Policy); 1435 OS << ">("; 1436 PrintExpr(Node->getSubExpr()); 1437 OS << ")"; 1438 } 1439 1440 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1441 VisitCXXNamedCastExpr(Node); 1442 } 1443 1444 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1445 VisitCXXNamedCastExpr(Node); 1446 } 1447 1448 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1449 VisitCXXNamedCastExpr(Node); 1450 } 1451 1452 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1453 VisitCXXNamedCastExpr(Node); 1454 } 1455 1456 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1457 OS << "typeid("; 1458 if (Node->isTypeOperand()) { 1459 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1460 } else { 1461 PrintExpr(Node->getExprOperand()); 1462 } 1463 OS << ")"; 1464 } 1465 1466 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1467 OS << "__uuidof("; 1468 if (Node->isTypeOperand()) { 1469 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1470 } else { 1471 PrintExpr(Node->getExprOperand()); 1472 } 1473 OS << ")"; 1474 } 1475 1476 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1477 PrintExpr(Node->getBaseExpr()); 1478 if (Node->isArrow()) 1479 OS << "->"; 1480 else 1481 OS << "."; 1482 if (NestedNameSpecifier *Qualifier = 1483 Node->getQualifierLoc().getNestedNameSpecifier()) 1484 Qualifier->print(OS, Policy); 1485 OS << Node->getPropertyDecl()->getDeclName(); 1486 } 1487 1488 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1489 switch (Node->getLiteralOperatorKind()) { 1490 case UserDefinedLiteral::LOK_Raw: 1491 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1492 break; 1493 case UserDefinedLiteral::LOK_Template: { 1494 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1495 const TemplateArgumentList *Args = 1496 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1497 assert(Args); 1498 const TemplateArgument &Pack = Args->get(0); 1499 for (TemplateArgument::pack_iterator I = Pack.pack_begin(), 1500 E = Pack.pack_end(); I != E; ++I) { 1501 char C = (char)I->getAsIntegral().getZExtValue(); 1502 OS << C; 1503 } 1504 break; 1505 } 1506 case UserDefinedLiteral::LOK_Integer: { 1507 // Print integer literal without suffix. 1508 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1509 OS << Int->getValue().toString(10, /*isSigned*/false); 1510 break; 1511 } 1512 case UserDefinedLiteral::LOK_Floating: { 1513 // Print floating literal without suffix. 1514 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1515 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1516 break; 1517 } 1518 case UserDefinedLiteral::LOK_String: 1519 case UserDefinedLiteral::LOK_Character: 1520 PrintExpr(Node->getCookedLiteral()); 1521 break; 1522 } 1523 OS << Node->getUDSuffix()->getName(); 1524 } 1525 1526 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1527 OS << (Node->getValue() ? "true" : "false"); 1528 } 1529 1530 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1531 OS << "nullptr"; 1532 } 1533 1534 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1535 OS << "this"; 1536 } 1537 1538 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1539 if (!Node->getSubExpr()) 1540 OS << "throw"; 1541 else { 1542 OS << "throw "; 1543 PrintExpr(Node->getSubExpr()); 1544 } 1545 } 1546 1547 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1548 // Nothing to print: we picked up the default argument. 1549 } 1550 1551 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1552 // Nothing to print: we picked up the default initializer. 1553 } 1554 1555 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1556 Node->getType().print(OS, Policy); 1557 OS << "("; 1558 PrintExpr(Node->getSubExpr()); 1559 OS << ")"; 1560 } 1561 1562 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1563 PrintExpr(Node->getSubExpr()); 1564 } 1565 1566 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1567 Node->getType().print(OS, Policy); 1568 OS << "("; 1569 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1570 ArgEnd = Node->arg_end(); 1571 Arg != ArgEnd; ++Arg) { 1572 if (Arg->isDefaultArgument()) 1573 break; 1574 if (Arg != Node->arg_begin()) 1575 OS << ", "; 1576 PrintExpr(*Arg); 1577 } 1578 OS << ")"; 1579 } 1580 1581 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1582 OS << '['; 1583 bool NeedComma = false; 1584 switch (Node->getCaptureDefault()) { 1585 case LCD_None: 1586 break; 1587 1588 case LCD_ByCopy: 1589 OS << '='; 1590 NeedComma = true; 1591 break; 1592 1593 case LCD_ByRef: 1594 OS << '&'; 1595 NeedComma = true; 1596 break; 1597 } 1598 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1599 CEnd = Node->explicit_capture_end(); 1600 C != CEnd; 1601 ++C) { 1602 if (NeedComma) 1603 OS << ", "; 1604 NeedComma = true; 1605 1606 switch (C->getCaptureKind()) { 1607 case LCK_This: 1608 OS << "this"; 1609 break; 1610 1611 case LCK_ByRef: 1612 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture()) 1613 OS << '&'; 1614 OS << C->getCapturedVar()->getName(); 1615 break; 1616 1617 case LCK_ByCopy: 1618 OS << C->getCapturedVar()->getName(); 1619 break; 1620 } 1621 1622 if (C->isInitCapture()) 1623 PrintExpr(C->getCapturedVar()->getInit()); 1624 } 1625 OS << ']'; 1626 1627 if (Node->hasExplicitParameters()) { 1628 OS << " ("; 1629 CXXMethodDecl *Method = Node->getCallOperator(); 1630 NeedComma = false; 1631 for (auto P : Method->params()) { 1632 if (NeedComma) { 1633 OS << ", "; 1634 } else { 1635 NeedComma = true; 1636 } 1637 std::string ParamStr = P->getNameAsString(); 1638 P->getOriginalType().print(OS, Policy, ParamStr); 1639 } 1640 if (Method->isVariadic()) { 1641 if (NeedComma) 1642 OS << ", "; 1643 OS << "..."; 1644 } 1645 OS << ')'; 1646 1647 if (Node->isMutable()) 1648 OS << " mutable"; 1649 1650 const FunctionProtoType *Proto 1651 = Method->getType()->getAs<FunctionProtoType>(); 1652 Proto->printExceptionSpecification(OS, Policy); 1653 1654 // FIXME: Attributes 1655 1656 // Print the trailing return type if it was specified in the source. 1657 if (Node->hasExplicitResultType()) { 1658 OS << " -> "; 1659 Proto->getReturnType().print(OS, Policy); 1660 } 1661 } 1662 1663 // Print the body. 1664 CompoundStmt *Body = Node->getBody(); 1665 OS << ' '; 1666 PrintStmt(Body); 1667 } 1668 1669 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1670 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1671 TSInfo->getType().print(OS, Policy); 1672 else 1673 Node->getType().print(OS, Policy); 1674 OS << "()"; 1675 } 1676 1677 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1678 if (E->isGlobalNew()) 1679 OS << "::"; 1680 OS << "new "; 1681 unsigned NumPlace = E->getNumPlacementArgs(); 1682 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1683 OS << "("; 1684 PrintExpr(E->getPlacementArg(0)); 1685 for (unsigned i = 1; i < NumPlace; ++i) { 1686 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1687 break; 1688 OS << ", "; 1689 PrintExpr(E->getPlacementArg(i)); 1690 } 1691 OS << ") "; 1692 } 1693 if (E->isParenTypeId()) 1694 OS << "("; 1695 std::string TypeS; 1696 if (Expr *Size = E->getArraySize()) { 1697 llvm::raw_string_ostream s(TypeS); 1698 s << '['; 1699 Size->printPretty(s, Helper, Policy); 1700 s << ']'; 1701 } 1702 E->getAllocatedType().print(OS, Policy, TypeS); 1703 if (E->isParenTypeId()) 1704 OS << ")"; 1705 1706 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1707 if (InitStyle) { 1708 if (InitStyle == CXXNewExpr::CallInit) 1709 OS << "("; 1710 PrintExpr(E->getInitializer()); 1711 if (InitStyle == CXXNewExpr::CallInit) 1712 OS << ")"; 1713 } 1714 } 1715 1716 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1717 if (E->isGlobalDelete()) 1718 OS << "::"; 1719 OS << "delete "; 1720 if (E->isArrayForm()) 1721 OS << "[] "; 1722 PrintExpr(E->getArgument()); 1723 } 1724 1725 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1726 PrintExpr(E->getBase()); 1727 if (E->isArrow()) 1728 OS << "->"; 1729 else 1730 OS << '.'; 1731 if (E->getQualifier()) 1732 E->getQualifier()->print(OS, Policy); 1733 OS << "~"; 1734 1735 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1736 OS << II->getName(); 1737 else 1738 E->getDestroyedType().print(OS, Policy); 1739 } 1740 1741 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1742 if (E->isListInitialization()) 1743 OS << "{ "; 1744 1745 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1746 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1747 // Don't print any defaulted arguments 1748 break; 1749 } 1750 1751 if (i) OS << ", "; 1752 PrintExpr(E->getArg(i)); 1753 } 1754 1755 if (E->isListInitialization()) 1756 OS << " }"; 1757 } 1758 1759 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 1760 PrintExpr(E->getSubExpr()); 1761 } 1762 1763 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1764 // Just forward to the subexpression. 1765 PrintExpr(E->getSubExpr()); 1766 } 1767 1768 void 1769 StmtPrinter::VisitCXXUnresolvedConstructExpr( 1770 CXXUnresolvedConstructExpr *Node) { 1771 Node->getTypeAsWritten().print(OS, Policy); 1772 OS << "("; 1773 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1774 ArgEnd = Node->arg_end(); 1775 Arg != ArgEnd; ++Arg) { 1776 if (Arg != Node->arg_begin()) 1777 OS << ", "; 1778 PrintExpr(*Arg); 1779 } 1780 OS << ")"; 1781 } 1782 1783 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1784 CXXDependentScopeMemberExpr *Node) { 1785 if (!Node->isImplicitAccess()) { 1786 PrintExpr(Node->getBase()); 1787 OS << (Node->isArrow() ? "->" : "."); 1788 } 1789 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1790 Qualifier->print(OS, Policy); 1791 if (Node->hasTemplateKeyword()) 1792 OS << "template "; 1793 OS << Node->getMemberNameInfo(); 1794 if (Node->hasExplicitTemplateArgs()) 1795 TemplateSpecializationType::PrintTemplateArgumentList( 1796 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1797 } 1798 1799 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1800 if (!Node->isImplicitAccess()) { 1801 PrintExpr(Node->getBase()); 1802 OS << (Node->isArrow() ? "->" : "."); 1803 } 1804 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1805 Qualifier->print(OS, Policy); 1806 if (Node->hasTemplateKeyword()) 1807 OS << "template "; 1808 OS << Node->getMemberNameInfo(); 1809 if (Node->hasExplicitTemplateArgs()) 1810 TemplateSpecializationType::PrintTemplateArgumentList( 1811 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1812 } 1813 1814 static const char *getTypeTraitName(TypeTrait TT) { 1815 switch (TT) { 1816 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 1817 case clang::UTT_##Name: return #Spelling; 1818 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 1819 case clang::BTT_##Name: return #Spelling; 1820 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 1821 case clang::TT_##Name: return #Spelling; 1822 #include "clang/Basic/TokenKinds.def" 1823 } 1824 llvm_unreachable("Type trait not covered by switch"); 1825 } 1826 1827 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1828 switch (ATT) { 1829 case ATT_ArrayRank: return "__array_rank"; 1830 case ATT_ArrayExtent: return "__array_extent"; 1831 } 1832 llvm_unreachable("Array type trait not covered by switch"); 1833 } 1834 1835 static const char *getExpressionTraitName(ExpressionTrait ET) { 1836 switch (ET) { 1837 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1838 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1839 } 1840 llvm_unreachable("Expression type trait not covered by switch"); 1841 } 1842 1843 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 1844 OS << getTypeTraitName(E->getTrait()) << "("; 1845 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 1846 if (I > 0) 1847 OS << ", "; 1848 E->getArg(I)->getType().print(OS, Policy); 1849 } 1850 OS << ")"; 1851 } 1852 1853 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1854 OS << getTypeTraitName(E->getTrait()) << '('; 1855 E->getQueriedType().print(OS, Policy); 1856 OS << ')'; 1857 } 1858 1859 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1860 OS << getExpressionTraitName(E->getTrait()) << '('; 1861 PrintExpr(E->getQueriedExpression()); 1862 OS << ')'; 1863 } 1864 1865 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1866 OS << "noexcept("; 1867 PrintExpr(E->getOperand()); 1868 OS << ")"; 1869 } 1870 1871 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1872 PrintExpr(E->getPattern()); 1873 OS << "..."; 1874 } 1875 1876 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1877 OS << "sizeof...(" << *E->getPack() << ")"; 1878 } 1879 1880 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1881 SubstNonTypeTemplateParmPackExpr *Node) { 1882 OS << *Node->getParameterPack(); 1883 } 1884 1885 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1886 SubstNonTypeTemplateParmExpr *Node) { 1887 Visit(Node->getReplacement()); 1888 } 1889 1890 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 1891 OS << *E->getParameterPack(); 1892 } 1893 1894 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1895 PrintExpr(Node->GetTemporaryExpr()); 1896 } 1897 1898 // Obj-C 1899 1900 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1901 OS << "@"; 1902 VisitStringLiteral(Node->getString()); 1903 } 1904 1905 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 1906 OS << "@"; 1907 Visit(E->getSubExpr()); 1908 } 1909 1910 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 1911 OS << "@[ "; 1912 StmtRange ch = E->children(); 1913 if (ch.first != ch.second) { 1914 while (1) { 1915 Visit(*ch.first); 1916 ++ch.first; 1917 if (ch.first == ch.second) break; 1918 OS << ", "; 1919 } 1920 } 1921 OS << " ]"; 1922 } 1923 1924 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 1925 OS << "@{ "; 1926 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 1927 if (I > 0) 1928 OS << ", "; 1929 1930 ObjCDictionaryElement Element = E->getKeyValueElement(I); 1931 Visit(Element.Key); 1932 OS << " : "; 1933 Visit(Element.Value); 1934 if (Element.isPackExpansion()) 1935 OS << "..."; 1936 } 1937 OS << " }"; 1938 } 1939 1940 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1941 OS << "@encode("; 1942 Node->getEncodedType().print(OS, Policy); 1943 OS << ')'; 1944 } 1945 1946 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1947 OS << "@selector("; 1948 Node->getSelector().print(OS); 1949 OS << ')'; 1950 } 1951 1952 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1953 OS << "@protocol(" << *Node->getProtocol() << ')'; 1954 } 1955 1956 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1957 OS << "["; 1958 switch (Mess->getReceiverKind()) { 1959 case ObjCMessageExpr::Instance: 1960 PrintExpr(Mess->getInstanceReceiver()); 1961 break; 1962 1963 case ObjCMessageExpr::Class: 1964 Mess->getClassReceiver().print(OS, Policy); 1965 break; 1966 1967 case ObjCMessageExpr::SuperInstance: 1968 case ObjCMessageExpr::SuperClass: 1969 OS << "Super"; 1970 break; 1971 } 1972 1973 OS << ' '; 1974 Selector selector = Mess->getSelector(); 1975 if (selector.isUnarySelector()) { 1976 OS << selector.getNameForSlot(0); 1977 } else { 1978 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1979 if (i < selector.getNumArgs()) { 1980 if (i > 0) OS << ' '; 1981 if (selector.getIdentifierInfoForSlot(i)) 1982 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1983 else 1984 OS << ":"; 1985 } 1986 else OS << ", "; // Handle variadic methods. 1987 1988 PrintExpr(Mess->getArg(i)); 1989 } 1990 } 1991 OS << "]"; 1992 } 1993 1994 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 1995 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1996 } 1997 1998 void 1999 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2000 PrintExpr(E->getSubExpr()); 2001 } 2002 2003 void 2004 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2005 OS << '(' << E->getBridgeKindName(); 2006 E->getType().print(OS, Policy); 2007 OS << ')'; 2008 PrintExpr(E->getSubExpr()); 2009 } 2010 2011 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2012 BlockDecl *BD = Node->getBlockDecl(); 2013 OS << "^"; 2014 2015 const FunctionType *AFT = Node->getFunctionType(); 2016 2017 if (isa<FunctionNoProtoType>(AFT)) { 2018 OS << "()"; 2019 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2020 OS << '('; 2021 for (BlockDecl::param_iterator AI = BD->param_begin(), 2022 E = BD->param_end(); AI != E; ++AI) { 2023 if (AI != BD->param_begin()) OS << ", "; 2024 std::string ParamStr = (*AI)->getNameAsString(); 2025 (*AI)->getType().print(OS, Policy, ParamStr); 2026 } 2027 2028 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2029 if (FT->isVariadic()) { 2030 if (!BD->param_empty()) OS << ", "; 2031 OS << "..."; 2032 } 2033 OS << ')'; 2034 } 2035 OS << "{ }"; 2036 } 2037 2038 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2039 PrintExpr(Node->getSourceExpr()); 2040 } 2041 2042 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2043 OS << "__builtin_astype("; 2044 PrintExpr(Node->getSrcExpr()); 2045 OS << ", "; 2046 Node->getType().print(OS, Policy); 2047 OS << ")"; 2048 } 2049 2050 //===----------------------------------------------------------------------===// 2051 // Stmt method implementations 2052 //===----------------------------------------------------------------------===// 2053 2054 void Stmt::dumpPretty(const ASTContext &Context) const { 2055 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2056 } 2057 2058 void Stmt::printPretty(raw_ostream &OS, 2059 PrinterHelper *Helper, 2060 const PrintingPolicy &Policy, 2061 unsigned Indentation) const { 2062 StmtPrinter P(OS, Helper, Policy, Indentation); 2063 P.Visit(const_cast<Stmt*>(this)); 2064 } 2065 2066 //===----------------------------------------------------------------------===// 2067 // PrinterHelper 2068 //===----------------------------------------------------------------------===// 2069 2070 // Implement virtual destructor. 2071 PrinterHelper::~PrinterHelper() {} 2072