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 if (IdentifierInfo *II = D->getFieldName()) 1279 OS << II->getName() << ":"; 1280 } else { 1281 OS << "." << D->getFieldName()->getName(); 1282 } 1283 } else { 1284 OS << "["; 1285 if (D->isArrayDesignator()) { 1286 PrintExpr(Node->getArrayIndex(*D)); 1287 } else { 1288 PrintExpr(Node->getArrayRangeStart(*D)); 1289 OS << " ... "; 1290 PrintExpr(Node->getArrayRangeEnd(*D)); 1291 } 1292 OS << "]"; 1293 } 1294 } 1295 1296 OS << " = "; 1297 PrintExpr(Node->getInit()); 1298 } 1299 1300 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1301 if (Policy.LangOpts.CPlusPlus) { 1302 OS << "/*implicit*/"; 1303 Node->getType().print(OS, Policy); 1304 OS << "()"; 1305 } else { 1306 OS << "/*implicit*/("; 1307 Node->getType().print(OS, Policy); 1308 OS << ')'; 1309 if (Node->getType()->isRecordType()) 1310 OS << "{}"; 1311 else 1312 OS << 0; 1313 } 1314 } 1315 1316 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1317 OS << "__builtin_va_arg("; 1318 PrintExpr(Node->getSubExpr()); 1319 OS << ", "; 1320 Node->getType().print(OS, Policy); 1321 OS << ")"; 1322 } 1323 1324 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1325 PrintExpr(Node->getSyntacticForm()); 1326 } 1327 1328 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1329 const char *Name = nullptr; 1330 switch (Node->getOp()) { 1331 #define BUILTIN(ID, TYPE, ATTRS) 1332 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1333 case AtomicExpr::AO ## ID: \ 1334 Name = #ID "("; \ 1335 break; 1336 #include "clang/Basic/Builtins.def" 1337 } 1338 OS << Name; 1339 1340 // AtomicExpr stores its subexpressions in a permuted order. 1341 PrintExpr(Node->getPtr()); 1342 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1343 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1344 OS << ", "; 1345 PrintExpr(Node->getVal1()); 1346 } 1347 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1348 Node->isCmpXChg()) { 1349 OS << ", "; 1350 PrintExpr(Node->getVal2()); 1351 } 1352 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1353 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1354 OS << ", "; 1355 PrintExpr(Node->getWeak()); 1356 } 1357 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1358 OS << ", "; 1359 PrintExpr(Node->getOrder()); 1360 } 1361 if (Node->isCmpXChg()) { 1362 OS << ", "; 1363 PrintExpr(Node->getOrderFail()); 1364 } 1365 OS << ")"; 1366 } 1367 1368 // C++ 1369 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1370 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1371 "", 1372 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1373 Spelling, 1374 #include "clang/Basic/OperatorKinds.def" 1375 }; 1376 1377 OverloadedOperatorKind Kind = Node->getOperator(); 1378 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1379 if (Node->getNumArgs() == 1) { 1380 OS << OpStrings[Kind] << ' '; 1381 PrintExpr(Node->getArg(0)); 1382 } else { 1383 PrintExpr(Node->getArg(0)); 1384 OS << ' ' << OpStrings[Kind]; 1385 } 1386 } else if (Kind == OO_Arrow) { 1387 PrintExpr(Node->getArg(0)); 1388 } else if (Kind == OO_Call) { 1389 PrintExpr(Node->getArg(0)); 1390 OS << '('; 1391 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1392 if (ArgIdx > 1) 1393 OS << ", "; 1394 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1395 PrintExpr(Node->getArg(ArgIdx)); 1396 } 1397 OS << ')'; 1398 } else if (Kind == OO_Subscript) { 1399 PrintExpr(Node->getArg(0)); 1400 OS << '['; 1401 PrintExpr(Node->getArg(1)); 1402 OS << ']'; 1403 } else if (Node->getNumArgs() == 1) { 1404 OS << OpStrings[Kind] << ' '; 1405 PrintExpr(Node->getArg(0)); 1406 } else if (Node->getNumArgs() == 2) { 1407 PrintExpr(Node->getArg(0)); 1408 OS << ' ' << OpStrings[Kind] << ' '; 1409 PrintExpr(Node->getArg(1)); 1410 } else { 1411 llvm_unreachable("unknown overloaded operator"); 1412 } 1413 } 1414 1415 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1416 // If we have a conversion operator call only print the argument. 1417 CXXMethodDecl *MD = Node->getMethodDecl(); 1418 if (MD && isa<CXXConversionDecl>(MD)) { 1419 PrintExpr(Node->getImplicitObjectArgument()); 1420 return; 1421 } 1422 VisitCallExpr(cast<CallExpr>(Node)); 1423 } 1424 1425 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1426 PrintExpr(Node->getCallee()); 1427 OS << "<<<"; 1428 PrintCallArgs(Node->getConfig()); 1429 OS << ">>>("; 1430 PrintCallArgs(Node); 1431 OS << ")"; 1432 } 1433 1434 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1435 OS << Node->getCastName() << '<'; 1436 Node->getTypeAsWritten().print(OS, Policy); 1437 OS << ">("; 1438 PrintExpr(Node->getSubExpr()); 1439 OS << ")"; 1440 } 1441 1442 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1443 VisitCXXNamedCastExpr(Node); 1444 } 1445 1446 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1447 VisitCXXNamedCastExpr(Node); 1448 } 1449 1450 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1451 VisitCXXNamedCastExpr(Node); 1452 } 1453 1454 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1455 VisitCXXNamedCastExpr(Node); 1456 } 1457 1458 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1459 OS << "typeid("; 1460 if (Node->isTypeOperand()) { 1461 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1462 } else { 1463 PrintExpr(Node->getExprOperand()); 1464 } 1465 OS << ")"; 1466 } 1467 1468 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1469 OS << "__uuidof("; 1470 if (Node->isTypeOperand()) { 1471 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1472 } else { 1473 PrintExpr(Node->getExprOperand()); 1474 } 1475 OS << ")"; 1476 } 1477 1478 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1479 PrintExpr(Node->getBaseExpr()); 1480 if (Node->isArrow()) 1481 OS << "->"; 1482 else 1483 OS << "."; 1484 if (NestedNameSpecifier *Qualifier = 1485 Node->getQualifierLoc().getNestedNameSpecifier()) 1486 Qualifier->print(OS, Policy); 1487 OS << Node->getPropertyDecl()->getDeclName(); 1488 } 1489 1490 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1491 switch (Node->getLiteralOperatorKind()) { 1492 case UserDefinedLiteral::LOK_Raw: 1493 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1494 break; 1495 case UserDefinedLiteral::LOK_Template: { 1496 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1497 const TemplateArgumentList *Args = 1498 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1499 assert(Args); 1500 const TemplateArgument &Pack = Args->get(0); 1501 for (TemplateArgument::pack_iterator I = Pack.pack_begin(), 1502 E = Pack.pack_end(); I != E; ++I) { 1503 char C = (char)I->getAsIntegral().getZExtValue(); 1504 OS << C; 1505 } 1506 break; 1507 } 1508 case UserDefinedLiteral::LOK_Integer: { 1509 // Print integer literal without suffix. 1510 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1511 OS << Int->getValue().toString(10, /*isSigned*/false); 1512 break; 1513 } 1514 case UserDefinedLiteral::LOK_Floating: { 1515 // Print floating literal without suffix. 1516 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1517 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1518 break; 1519 } 1520 case UserDefinedLiteral::LOK_String: 1521 case UserDefinedLiteral::LOK_Character: 1522 PrintExpr(Node->getCookedLiteral()); 1523 break; 1524 } 1525 OS << Node->getUDSuffix()->getName(); 1526 } 1527 1528 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1529 OS << (Node->getValue() ? "true" : "false"); 1530 } 1531 1532 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1533 OS << "nullptr"; 1534 } 1535 1536 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1537 OS << "this"; 1538 } 1539 1540 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1541 if (!Node->getSubExpr()) 1542 OS << "throw"; 1543 else { 1544 OS << "throw "; 1545 PrintExpr(Node->getSubExpr()); 1546 } 1547 } 1548 1549 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1550 // Nothing to print: we picked up the default argument. 1551 } 1552 1553 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1554 // Nothing to print: we picked up the default initializer. 1555 } 1556 1557 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1558 Node->getType().print(OS, Policy); 1559 OS << "("; 1560 PrintExpr(Node->getSubExpr()); 1561 OS << ")"; 1562 } 1563 1564 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1565 PrintExpr(Node->getSubExpr()); 1566 } 1567 1568 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1569 Node->getType().print(OS, Policy); 1570 OS << "("; 1571 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1572 ArgEnd = Node->arg_end(); 1573 Arg != ArgEnd; ++Arg) { 1574 if (Arg->isDefaultArgument()) 1575 break; 1576 if (Arg != Node->arg_begin()) 1577 OS << ", "; 1578 PrintExpr(*Arg); 1579 } 1580 OS << ")"; 1581 } 1582 1583 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1584 OS << '['; 1585 bool NeedComma = false; 1586 switch (Node->getCaptureDefault()) { 1587 case LCD_None: 1588 break; 1589 1590 case LCD_ByCopy: 1591 OS << '='; 1592 NeedComma = true; 1593 break; 1594 1595 case LCD_ByRef: 1596 OS << '&'; 1597 NeedComma = true; 1598 break; 1599 } 1600 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1601 CEnd = Node->explicit_capture_end(); 1602 C != CEnd; 1603 ++C) { 1604 if (NeedComma) 1605 OS << ", "; 1606 NeedComma = true; 1607 1608 switch (C->getCaptureKind()) { 1609 case LCK_This: 1610 OS << "this"; 1611 break; 1612 1613 case LCK_ByRef: 1614 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture()) 1615 OS << '&'; 1616 OS << C->getCapturedVar()->getName(); 1617 break; 1618 1619 case LCK_ByCopy: 1620 OS << C->getCapturedVar()->getName(); 1621 break; 1622 } 1623 1624 if (C->isInitCapture()) 1625 PrintExpr(C->getCapturedVar()->getInit()); 1626 } 1627 OS << ']'; 1628 1629 if (Node->hasExplicitParameters()) { 1630 OS << " ("; 1631 CXXMethodDecl *Method = Node->getCallOperator(); 1632 NeedComma = false; 1633 for (auto P : Method->params()) { 1634 if (NeedComma) { 1635 OS << ", "; 1636 } else { 1637 NeedComma = true; 1638 } 1639 std::string ParamStr = P->getNameAsString(); 1640 P->getOriginalType().print(OS, Policy, ParamStr); 1641 } 1642 if (Method->isVariadic()) { 1643 if (NeedComma) 1644 OS << ", "; 1645 OS << "..."; 1646 } 1647 OS << ')'; 1648 1649 if (Node->isMutable()) 1650 OS << " mutable"; 1651 1652 const FunctionProtoType *Proto 1653 = Method->getType()->getAs<FunctionProtoType>(); 1654 Proto->printExceptionSpecification(OS, Policy); 1655 1656 // FIXME: Attributes 1657 1658 // Print the trailing return type if it was specified in the source. 1659 if (Node->hasExplicitResultType()) { 1660 OS << " -> "; 1661 Proto->getReturnType().print(OS, Policy); 1662 } 1663 } 1664 1665 // Print the body. 1666 CompoundStmt *Body = Node->getBody(); 1667 OS << ' '; 1668 PrintStmt(Body); 1669 } 1670 1671 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1672 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1673 TSInfo->getType().print(OS, Policy); 1674 else 1675 Node->getType().print(OS, Policy); 1676 OS << "()"; 1677 } 1678 1679 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1680 if (E->isGlobalNew()) 1681 OS << "::"; 1682 OS << "new "; 1683 unsigned NumPlace = E->getNumPlacementArgs(); 1684 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1685 OS << "("; 1686 PrintExpr(E->getPlacementArg(0)); 1687 for (unsigned i = 1; i < NumPlace; ++i) { 1688 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1689 break; 1690 OS << ", "; 1691 PrintExpr(E->getPlacementArg(i)); 1692 } 1693 OS << ") "; 1694 } 1695 if (E->isParenTypeId()) 1696 OS << "("; 1697 std::string TypeS; 1698 if (Expr *Size = E->getArraySize()) { 1699 llvm::raw_string_ostream s(TypeS); 1700 s << '['; 1701 Size->printPretty(s, Helper, Policy); 1702 s << ']'; 1703 } 1704 E->getAllocatedType().print(OS, Policy, TypeS); 1705 if (E->isParenTypeId()) 1706 OS << ")"; 1707 1708 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1709 if (InitStyle) { 1710 if (InitStyle == CXXNewExpr::CallInit) 1711 OS << "("; 1712 PrintExpr(E->getInitializer()); 1713 if (InitStyle == CXXNewExpr::CallInit) 1714 OS << ")"; 1715 } 1716 } 1717 1718 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1719 if (E->isGlobalDelete()) 1720 OS << "::"; 1721 OS << "delete "; 1722 if (E->isArrayForm()) 1723 OS << "[] "; 1724 PrintExpr(E->getArgument()); 1725 } 1726 1727 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1728 PrintExpr(E->getBase()); 1729 if (E->isArrow()) 1730 OS << "->"; 1731 else 1732 OS << '.'; 1733 if (E->getQualifier()) 1734 E->getQualifier()->print(OS, Policy); 1735 OS << "~"; 1736 1737 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1738 OS << II->getName(); 1739 else 1740 E->getDestroyedType().print(OS, Policy); 1741 } 1742 1743 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1744 if (E->isListInitialization()) 1745 OS << "{ "; 1746 1747 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1748 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1749 // Don't print any defaulted arguments 1750 break; 1751 } 1752 1753 if (i) OS << ", "; 1754 PrintExpr(E->getArg(i)); 1755 } 1756 1757 if (E->isListInitialization()) 1758 OS << " }"; 1759 } 1760 1761 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 1762 PrintExpr(E->getSubExpr()); 1763 } 1764 1765 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1766 // Just forward to the subexpression. 1767 PrintExpr(E->getSubExpr()); 1768 } 1769 1770 void 1771 StmtPrinter::VisitCXXUnresolvedConstructExpr( 1772 CXXUnresolvedConstructExpr *Node) { 1773 Node->getTypeAsWritten().print(OS, Policy); 1774 OS << "("; 1775 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1776 ArgEnd = Node->arg_end(); 1777 Arg != ArgEnd; ++Arg) { 1778 if (Arg != Node->arg_begin()) 1779 OS << ", "; 1780 PrintExpr(*Arg); 1781 } 1782 OS << ")"; 1783 } 1784 1785 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1786 CXXDependentScopeMemberExpr *Node) { 1787 if (!Node->isImplicitAccess()) { 1788 PrintExpr(Node->getBase()); 1789 OS << (Node->isArrow() ? "->" : "."); 1790 } 1791 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1792 Qualifier->print(OS, Policy); 1793 if (Node->hasTemplateKeyword()) 1794 OS << "template "; 1795 OS << Node->getMemberNameInfo(); 1796 if (Node->hasExplicitTemplateArgs()) 1797 TemplateSpecializationType::PrintTemplateArgumentList( 1798 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1799 } 1800 1801 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1802 if (!Node->isImplicitAccess()) { 1803 PrintExpr(Node->getBase()); 1804 OS << (Node->isArrow() ? "->" : "."); 1805 } 1806 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1807 Qualifier->print(OS, Policy); 1808 if (Node->hasTemplateKeyword()) 1809 OS << "template "; 1810 OS << Node->getMemberNameInfo(); 1811 if (Node->hasExplicitTemplateArgs()) 1812 TemplateSpecializationType::PrintTemplateArgumentList( 1813 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1814 } 1815 1816 static const char *getTypeTraitName(TypeTrait TT) { 1817 switch (TT) { 1818 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 1819 case clang::UTT_##Name: return #Spelling; 1820 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 1821 case clang::BTT_##Name: return #Spelling; 1822 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 1823 case clang::TT_##Name: return #Spelling; 1824 #include "clang/Basic/TokenKinds.def" 1825 } 1826 llvm_unreachable("Type trait not covered by switch"); 1827 } 1828 1829 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1830 switch (ATT) { 1831 case ATT_ArrayRank: return "__array_rank"; 1832 case ATT_ArrayExtent: return "__array_extent"; 1833 } 1834 llvm_unreachable("Array type trait not covered by switch"); 1835 } 1836 1837 static const char *getExpressionTraitName(ExpressionTrait ET) { 1838 switch (ET) { 1839 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1840 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1841 } 1842 llvm_unreachable("Expression type trait not covered by switch"); 1843 } 1844 1845 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 1846 OS << getTypeTraitName(E->getTrait()) << "("; 1847 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 1848 if (I > 0) 1849 OS << ", "; 1850 E->getArg(I)->getType().print(OS, Policy); 1851 } 1852 OS << ")"; 1853 } 1854 1855 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1856 OS << getTypeTraitName(E->getTrait()) << '('; 1857 E->getQueriedType().print(OS, Policy); 1858 OS << ')'; 1859 } 1860 1861 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1862 OS << getExpressionTraitName(E->getTrait()) << '('; 1863 PrintExpr(E->getQueriedExpression()); 1864 OS << ')'; 1865 } 1866 1867 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1868 OS << "noexcept("; 1869 PrintExpr(E->getOperand()); 1870 OS << ")"; 1871 } 1872 1873 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1874 PrintExpr(E->getPattern()); 1875 OS << "..."; 1876 } 1877 1878 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1879 OS << "sizeof...(" << *E->getPack() << ")"; 1880 } 1881 1882 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1883 SubstNonTypeTemplateParmPackExpr *Node) { 1884 OS << *Node->getParameterPack(); 1885 } 1886 1887 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1888 SubstNonTypeTemplateParmExpr *Node) { 1889 Visit(Node->getReplacement()); 1890 } 1891 1892 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 1893 OS << *E->getParameterPack(); 1894 } 1895 1896 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1897 PrintExpr(Node->GetTemporaryExpr()); 1898 } 1899 1900 // Obj-C 1901 1902 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1903 OS << "@"; 1904 VisitStringLiteral(Node->getString()); 1905 } 1906 1907 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 1908 OS << "@"; 1909 Visit(E->getSubExpr()); 1910 } 1911 1912 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 1913 OS << "@[ "; 1914 StmtRange ch = E->children(); 1915 if (ch.first != ch.second) { 1916 while (1) { 1917 Visit(*ch.first); 1918 ++ch.first; 1919 if (ch.first == ch.second) break; 1920 OS << ", "; 1921 } 1922 } 1923 OS << " ]"; 1924 } 1925 1926 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 1927 OS << "@{ "; 1928 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 1929 if (I > 0) 1930 OS << ", "; 1931 1932 ObjCDictionaryElement Element = E->getKeyValueElement(I); 1933 Visit(Element.Key); 1934 OS << " : "; 1935 Visit(Element.Value); 1936 if (Element.isPackExpansion()) 1937 OS << "..."; 1938 } 1939 OS << " }"; 1940 } 1941 1942 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1943 OS << "@encode("; 1944 Node->getEncodedType().print(OS, Policy); 1945 OS << ')'; 1946 } 1947 1948 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1949 OS << "@selector("; 1950 Node->getSelector().print(OS); 1951 OS << ')'; 1952 } 1953 1954 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1955 OS << "@protocol(" << *Node->getProtocol() << ')'; 1956 } 1957 1958 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1959 OS << "["; 1960 switch (Mess->getReceiverKind()) { 1961 case ObjCMessageExpr::Instance: 1962 PrintExpr(Mess->getInstanceReceiver()); 1963 break; 1964 1965 case ObjCMessageExpr::Class: 1966 Mess->getClassReceiver().print(OS, Policy); 1967 break; 1968 1969 case ObjCMessageExpr::SuperInstance: 1970 case ObjCMessageExpr::SuperClass: 1971 OS << "Super"; 1972 break; 1973 } 1974 1975 OS << ' '; 1976 Selector selector = Mess->getSelector(); 1977 if (selector.isUnarySelector()) { 1978 OS << selector.getNameForSlot(0); 1979 } else { 1980 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1981 if (i < selector.getNumArgs()) { 1982 if (i > 0) OS << ' '; 1983 if (selector.getIdentifierInfoForSlot(i)) 1984 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1985 else 1986 OS << ":"; 1987 } 1988 else OS << ", "; // Handle variadic methods. 1989 1990 PrintExpr(Mess->getArg(i)); 1991 } 1992 } 1993 OS << "]"; 1994 } 1995 1996 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 1997 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1998 } 1999 2000 void 2001 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2002 PrintExpr(E->getSubExpr()); 2003 } 2004 2005 void 2006 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2007 OS << '(' << E->getBridgeKindName(); 2008 E->getType().print(OS, Policy); 2009 OS << ')'; 2010 PrintExpr(E->getSubExpr()); 2011 } 2012 2013 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2014 BlockDecl *BD = Node->getBlockDecl(); 2015 OS << "^"; 2016 2017 const FunctionType *AFT = Node->getFunctionType(); 2018 2019 if (isa<FunctionNoProtoType>(AFT)) { 2020 OS << "()"; 2021 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2022 OS << '('; 2023 for (BlockDecl::param_iterator AI = BD->param_begin(), 2024 E = BD->param_end(); AI != E; ++AI) { 2025 if (AI != BD->param_begin()) OS << ", "; 2026 std::string ParamStr = (*AI)->getNameAsString(); 2027 (*AI)->getType().print(OS, Policy, ParamStr); 2028 } 2029 2030 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2031 if (FT->isVariadic()) { 2032 if (!BD->param_empty()) OS << ", "; 2033 OS << "..."; 2034 } 2035 OS << ')'; 2036 } 2037 OS << "{ }"; 2038 } 2039 2040 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2041 PrintExpr(Node->getSourceExpr()); 2042 } 2043 2044 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2045 OS << "__builtin_astype("; 2046 PrintExpr(Node->getSrcExpr()); 2047 OS << ", "; 2048 Node->getType().print(OS, Policy); 2049 OS << ")"; 2050 } 2051 2052 //===----------------------------------------------------------------------===// 2053 // Stmt method implementations 2054 //===----------------------------------------------------------------------===// 2055 2056 void Stmt::dumpPretty(const ASTContext &Context) const { 2057 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2058 } 2059 2060 void Stmt::printPretty(raw_ostream &OS, 2061 PrinterHelper *Helper, 2062 const PrintingPolicy &Policy, 2063 unsigned Indentation) const { 2064 StmtPrinter P(OS, Helper, Policy, Indentation); 2065 P.Visit(const_cast<Stmt*>(this)); 2066 } 2067 2068 //===----------------------------------------------------------------------===// 2069 // PrinterHelper 2070 //===----------------------------------------------------------------------===// 2071 2072 // Implement virtual destructor. 2073 PrinterHelper::~PrinterHelper() {} 2074