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