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 OS << ")"; 402 } 403 404 // Inputs 405 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 406 OS << " : "; 407 408 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 409 if (i != 0) 410 OS << ", "; 411 412 if (!Node->getInputName(i).empty()) { 413 OS << '['; 414 OS << Node->getInputName(i); 415 OS << "] "; 416 } 417 418 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 419 OS << " ("; 420 Visit(Node->getInputExpr(i)); 421 OS << ")"; 422 } 423 424 // Clobbers 425 if (Node->getNumClobbers() != 0) 426 OS << " : "; 427 428 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 429 if (i != 0) 430 OS << ", "; 431 432 VisitStringLiteral(Node->getClobberStringLiteral(i)); 433 } 434 435 OS << ");"; 436 if (Policy.IncludeNewlines) OS << "\n"; 437 } 438 439 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) { 440 // FIXME: Implement MS style inline asm statement printer. 441 Indent() << "__asm "; 442 if (Node->hasBraces()) 443 OS << "{\n"; 444 OS << Node->getAsmString() << "\n"; 445 if (Node->hasBraces()) 446 Indent() << "}\n"; 447 } 448 449 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) { 450 PrintStmt(Node->getCapturedDecl()->getBody()); 451 } 452 453 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 454 Indent() << "@try"; 455 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 456 PrintRawCompoundStmt(TS); 457 OS << "\n"; 458 } 459 460 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 461 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 462 Indent() << "@catch("; 463 if (catchStmt->getCatchParamDecl()) { 464 if (Decl *DS = catchStmt->getCatchParamDecl()) 465 PrintRawDecl(DS); 466 } 467 OS << ")"; 468 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 469 PrintRawCompoundStmt(CS); 470 OS << "\n"; 471 } 472 } 473 474 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 475 Node->getFinallyStmt())) { 476 Indent() << "@finally"; 477 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 478 OS << "\n"; 479 } 480 } 481 482 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 483 } 484 485 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 486 Indent() << "@catch (...) { /* todo */ } \n"; 487 } 488 489 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 490 Indent() << "@throw"; 491 if (Node->getThrowExpr()) { 492 OS << " "; 493 PrintExpr(Node->getThrowExpr()); 494 } 495 OS << ";\n"; 496 } 497 498 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 499 Indent() << "@synchronized ("; 500 PrintExpr(Node->getSynchExpr()); 501 OS << ")"; 502 PrintRawCompoundStmt(Node->getSynchBody()); 503 OS << "\n"; 504 } 505 506 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 507 Indent() << "@autoreleasepool"; 508 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt())); 509 OS << "\n"; 510 } 511 512 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 513 OS << "catch ("; 514 if (Decl *ExDecl = Node->getExceptionDecl()) 515 PrintRawDecl(ExDecl); 516 else 517 OS << "..."; 518 OS << ") "; 519 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 520 } 521 522 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 523 Indent(); 524 PrintRawCXXCatchStmt(Node); 525 OS << "\n"; 526 } 527 528 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 529 Indent() << "try "; 530 PrintRawCompoundStmt(Node->getTryBlock()); 531 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 532 OS << " "; 533 PrintRawCXXCatchStmt(Node->getHandler(i)); 534 } 535 OS << "\n"; 536 } 537 538 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 539 Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 540 PrintRawCompoundStmt(Node->getTryBlock()); 541 SEHExceptStmt *E = Node->getExceptHandler(); 542 SEHFinallyStmt *F = Node->getFinallyHandler(); 543 if(E) 544 PrintRawSEHExceptHandler(E); 545 else { 546 assert(F && "Must have a finally block..."); 547 PrintRawSEHFinallyStmt(F); 548 } 549 OS << "\n"; 550 } 551 552 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 553 OS << "__finally "; 554 PrintRawCompoundStmt(Node->getBlock()); 555 OS << "\n"; 556 } 557 558 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 559 OS << "__except ("; 560 VisitExpr(Node->getFilterExpr()); 561 OS << ")\n"; 562 PrintRawCompoundStmt(Node->getBlock()); 563 OS << "\n"; 564 } 565 566 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 567 Indent(); 568 PrintRawSEHExceptHandler(Node); 569 OS << "\n"; 570 } 571 572 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 573 Indent(); 574 PrintRawSEHFinallyStmt(Node); 575 OS << "\n"; 576 } 577 578 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) { 579 Indent() << "__leave;"; 580 if (Policy.IncludeNewlines) OS << "\n"; 581 } 582 583 //===----------------------------------------------------------------------===// 584 // OpenMP clauses printing methods 585 //===----------------------------------------------------------------------===// 586 587 namespace { 588 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> { 589 raw_ostream &OS; 590 const PrintingPolicy &Policy; 591 /// \brief Process clauses with list of variables. 592 template <typename T> 593 void VisitOMPClauseList(T *Node, char StartSym); 594 public: 595 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy) 596 : OS(OS), Policy(Policy) { } 597 #define OPENMP_CLAUSE(Name, Class) \ 598 void Visit##Class(Class *S); 599 #include "clang/Basic/OpenMPKinds.def" 600 }; 601 602 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) { 603 OS << "if("; 604 Node->getCondition()->printPretty(OS, nullptr, Policy, 0); 605 OS << ")"; 606 } 607 608 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) { 609 OS << "final("; 610 Node->getCondition()->printPretty(OS, nullptr, Policy, 0); 611 OS << ")"; 612 } 613 614 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) { 615 OS << "num_threads("; 616 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0); 617 OS << ")"; 618 } 619 620 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) { 621 OS << "safelen("; 622 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0); 623 OS << ")"; 624 } 625 626 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) { 627 OS << "collapse("; 628 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0); 629 OS << ")"; 630 } 631 632 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) { 633 OS << "default(" 634 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind()) 635 << ")"; 636 } 637 638 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) { 639 OS << "proc_bind(" 640 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind()) 641 << ")"; 642 } 643 644 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) { 645 OS << "schedule(" 646 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind()); 647 if (Node->getChunkSize()) { 648 OS << ", "; 649 Node->getChunkSize()->printPretty(OS, nullptr, Policy); 650 } 651 OS << ")"; 652 } 653 654 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) { 655 OS << "ordered"; 656 if (auto *Num = Node->getNumForLoops()) { 657 OS << "("; 658 Num->printPretty(OS, nullptr, Policy, 0); 659 OS << ")"; 660 } 661 } 662 663 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) { 664 OS << "nowait"; 665 } 666 667 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) { 668 OS << "untied"; 669 } 670 671 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) { 672 OS << "mergeable"; 673 } 674 675 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; } 676 677 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; } 678 679 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) { 680 OS << "update"; 681 } 682 683 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) { 684 OS << "capture"; 685 } 686 687 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) { 688 OS << "seq_cst"; 689 } 690 691 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) { 692 OS << "device("; 693 Node->getDevice()->printPretty(OS, nullptr, Policy, 0); 694 OS << ")"; 695 } 696 697 template<typename T> 698 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) { 699 for (typename T::varlist_iterator I = Node->varlist_begin(), 700 E = Node->varlist_end(); 701 I != E; ++I) { 702 assert(*I && "Expected non-null Stmt"); 703 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) { 704 OS << (I == Node->varlist_begin() ? StartSym : ','); 705 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS); 706 } else { 707 OS << (I == Node->varlist_begin() ? StartSym : ','); 708 (*I)->printPretty(OS, nullptr, Policy, 0); 709 } 710 } 711 } 712 713 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) { 714 if (!Node->varlist_empty()) { 715 OS << "private"; 716 VisitOMPClauseList(Node, '('); 717 OS << ")"; 718 } 719 } 720 721 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) { 722 if (!Node->varlist_empty()) { 723 OS << "firstprivate"; 724 VisitOMPClauseList(Node, '('); 725 OS << ")"; 726 } 727 } 728 729 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) { 730 if (!Node->varlist_empty()) { 731 OS << "lastprivate"; 732 VisitOMPClauseList(Node, '('); 733 OS << ")"; 734 } 735 } 736 737 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) { 738 if (!Node->varlist_empty()) { 739 OS << "shared"; 740 VisitOMPClauseList(Node, '('); 741 OS << ")"; 742 } 743 } 744 745 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) { 746 if (!Node->varlist_empty()) { 747 OS << "reduction("; 748 NestedNameSpecifier *QualifierLoc = 749 Node->getQualifierLoc().getNestedNameSpecifier(); 750 OverloadedOperatorKind OOK = 751 Node->getNameInfo().getName().getCXXOverloadedOperator(); 752 if (QualifierLoc == nullptr && OOK != OO_None) { 753 // Print reduction identifier in C format 754 OS << getOperatorSpelling(OOK); 755 } else { 756 // Use C++ format 757 if (QualifierLoc != nullptr) 758 QualifierLoc->print(OS, Policy); 759 OS << Node->getNameInfo(); 760 } 761 OS << ":"; 762 VisitOMPClauseList(Node, ' '); 763 OS << ")"; 764 } 765 } 766 767 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) { 768 if (!Node->varlist_empty()) { 769 OS << "linear"; 770 VisitOMPClauseList(Node, '('); 771 if (Node->getStep() != nullptr) { 772 OS << ": "; 773 Node->getStep()->printPretty(OS, nullptr, Policy, 0); 774 } 775 OS << ")"; 776 } 777 } 778 779 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) { 780 if (!Node->varlist_empty()) { 781 OS << "aligned"; 782 VisitOMPClauseList(Node, '('); 783 if (Node->getAlignment() != nullptr) { 784 OS << ": "; 785 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0); 786 } 787 OS << ")"; 788 } 789 } 790 791 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) { 792 if (!Node->varlist_empty()) { 793 OS << "copyin"; 794 VisitOMPClauseList(Node, '('); 795 OS << ")"; 796 } 797 } 798 799 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) { 800 if (!Node->varlist_empty()) { 801 OS << "copyprivate"; 802 VisitOMPClauseList(Node, '('); 803 OS << ")"; 804 } 805 } 806 807 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) { 808 if (!Node->varlist_empty()) { 809 VisitOMPClauseList(Node, '('); 810 OS << ")"; 811 } 812 } 813 814 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) { 815 if (!Node->varlist_empty()) { 816 OS << "depend("; 817 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), 818 Node->getDependencyKind()) 819 << " :"; 820 VisitOMPClauseList(Node, ' '); 821 OS << ")"; 822 } 823 } 824 } 825 826 //===----------------------------------------------------------------------===// 827 // OpenMP directives printing methods 828 //===----------------------------------------------------------------------===// 829 830 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) { 831 OMPClausePrinter Printer(OS, Policy); 832 ArrayRef<OMPClause *> Clauses = S->clauses(); 833 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 834 I != E; ++I) 835 if (*I && !(*I)->isImplicit()) { 836 Printer.Visit(*I); 837 OS << ' '; 838 } 839 OS << "\n"; 840 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) { 841 assert(isa<CapturedStmt>(S->getAssociatedStmt()) && 842 "Expected captured statement!"); 843 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt(); 844 PrintStmt(CS); 845 } 846 } 847 848 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) { 849 Indent() << "#pragma omp parallel "; 850 PrintOMPExecutableDirective(Node); 851 } 852 853 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) { 854 Indent() << "#pragma omp simd "; 855 PrintOMPExecutableDirective(Node); 856 } 857 858 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) { 859 Indent() << "#pragma omp for "; 860 PrintOMPExecutableDirective(Node); 861 } 862 863 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) { 864 Indent() << "#pragma omp for simd "; 865 PrintOMPExecutableDirective(Node); 866 } 867 868 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) { 869 Indent() << "#pragma omp sections "; 870 PrintOMPExecutableDirective(Node); 871 } 872 873 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) { 874 Indent() << "#pragma omp section"; 875 PrintOMPExecutableDirective(Node); 876 } 877 878 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) { 879 Indent() << "#pragma omp single "; 880 PrintOMPExecutableDirective(Node); 881 } 882 883 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) { 884 Indent() << "#pragma omp master"; 885 PrintOMPExecutableDirective(Node); 886 } 887 888 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) { 889 Indent() << "#pragma omp critical"; 890 if (Node->getDirectiveName().getName()) { 891 OS << " ("; 892 Node->getDirectiveName().printName(OS); 893 OS << ")"; 894 } 895 PrintOMPExecutableDirective(Node); 896 } 897 898 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) { 899 Indent() << "#pragma omp parallel for "; 900 PrintOMPExecutableDirective(Node); 901 } 902 903 void StmtPrinter::VisitOMPParallelForSimdDirective( 904 OMPParallelForSimdDirective *Node) { 905 Indent() << "#pragma omp parallel for simd "; 906 PrintOMPExecutableDirective(Node); 907 } 908 909 void StmtPrinter::VisitOMPParallelSectionsDirective( 910 OMPParallelSectionsDirective *Node) { 911 Indent() << "#pragma omp parallel sections "; 912 PrintOMPExecutableDirective(Node); 913 } 914 915 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) { 916 Indent() << "#pragma omp task "; 917 PrintOMPExecutableDirective(Node); 918 } 919 920 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) { 921 Indent() << "#pragma omp taskyield"; 922 PrintOMPExecutableDirective(Node); 923 } 924 925 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) { 926 Indent() << "#pragma omp barrier"; 927 PrintOMPExecutableDirective(Node); 928 } 929 930 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) { 931 Indent() << "#pragma omp taskwait"; 932 PrintOMPExecutableDirective(Node); 933 } 934 935 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) { 936 Indent() << "#pragma omp taskgroup"; 937 PrintOMPExecutableDirective(Node); 938 } 939 940 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) { 941 Indent() << "#pragma omp flush "; 942 PrintOMPExecutableDirective(Node); 943 } 944 945 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) { 946 Indent() << "#pragma omp ordered"; 947 PrintOMPExecutableDirective(Node); 948 } 949 950 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) { 951 Indent() << "#pragma omp atomic "; 952 PrintOMPExecutableDirective(Node); 953 } 954 955 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) { 956 Indent() << "#pragma omp target "; 957 PrintOMPExecutableDirective(Node); 958 } 959 960 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) { 961 Indent() << "#pragma omp target data "; 962 PrintOMPExecutableDirective(Node); 963 } 964 965 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) { 966 Indent() << "#pragma omp teams "; 967 PrintOMPExecutableDirective(Node); 968 } 969 970 void StmtPrinter::VisitOMPCancellationPointDirective( 971 OMPCancellationPointDirective *Node) { 972 Indent() << "#pragma omp cancellation point " 973 << getOpenMPDirectiveName(Node->getCancelRegion()); 974 PrintOMPExecutableDirective(Node); 975 } 976 977 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) { 978 Indent() << "#pragma omp cancel " 979 << getOpenMPDirectiveName(Node->getCancelRegion()); 980 PrintOMPExecutableDirective(Node); 981 } 982 //===----------------------------------------------------------------------===// 983 // Expr printing methods. 984 //===----------------------------------------------------------------------===// 985 986 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 987 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 988 Qualifier->print(OS, Policy); 989 if (Node->hasTemplateKeyword()) 990 OS << "template "; 991 OS << Node->getNameInfo(); 992 if (Node->hasExplicitTemplateArgs()) 993 TemplateSpecializationType::PrintTemplateArgumentList( 994 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 995 } 996 997 void StmtPrinter::VisitDependentScopeDeclRefExpr( 998 DependentScopeDeclRefExpr *Node) { 999 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1000 Qualifier->print(OS, Policy); 1001 if (Node->hasTemplateKeyword()) 1002 OS << "template "; 1003 OS << Node->getNameInfo(); 1004 if (Node->hasExplicitTemplateArgs()) 1005 TemplateSpecializationType::PrintTemplateArgumentList( 1006 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1007 } 1008 1009 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 1010 if (Node->getQualifier()) 1011 Node->getQualifier()->print(OS, Policy); 1012 if (Node->hasTemplateKeyword()) 1013 OS << "template "; 1014 OS << Node->getNameInfo(); 1015 if (Node->hasExplicitTemplateArgs()) 1016 TemplateSpecializationType::PrintTemplateArgumentList( 1017 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1018 } 1019 1020 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 1021 if (Node->getBase()) { 1022 PrintExpr(Node->getBase()); 1023 OS << (Node->isArrow() ? "->" : "."); 1024 } 1025 OS << *Node->getDecl(); 1026 } 1027 1028 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 1029 if (Node->isSuperReceiver()) 1030 OS << "super."; 1031 else if (Node->isObjectReceiver() && Node->getBase()) { 1032 PrintExpr(Node->getBase()); 1033 OS << "."; 1034 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 1035 OS << Node->getClassReceiver()->getName() << "."; 1036 } 1037 1038 if (Node->isImplicitProperty()) 1039 Node->getImplicitPropertyGetter()->getSelector().print(OS); 1040 else 1041 OS << Node->getExplicitProperty()->getName(); 1042 } 1043 1044 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 1045 1046 PrintExpr(Node->getBaseExpr()); 1047 OS << "["; 1048 PrintExpr(Node->getKeyExpr()); 1049 OS << "]"; 1050 } 1051 1052 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 1053 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType()); 1054 } 1055 1056 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 1057 unsigned value = Node->getValue(); 1058 1059 switch (Node->getKind()) { 1060 case CharacterLiteral::Ascii: break; // no prefix. 1061 case CharacterLiteral::Wide: OS << 'L'; break; 1062 case CharacterLiteral::UTF16: OS << 'u'; break; 1063 case CharacterLiteral::UTF32: OS << 'U'; break; 1064 } 1065 1066 switch (value) { 1067 case '\\': 1068 OS << "'\\\\'"; 1069 break; 1070 case '\'': 1071 OS << "'\\''"; 1072 break; 1073 case '\a': 1074 // TODO: K&R: the meaning of '\\a' is different in traditional C 1075 OS << "'\\a'"; 1076 break; 1077 case '\b': 1078 OS << "'\\b'"; 1079 break; 1080 // Nonstandard escape sequence. 1081 /*case '\e': 1082 OS << "'\\e'"; 1083 break;*/ 1084 case '\f': 1085 OS << "'\\f'"; 1086 break; 1087 case '\n': 1088 OS << "'\\n'"; 1089 break; 1090 case '\r': 1091 OS << "'\\r'"; 1092 break; 1093 case '\t': 1094 OS << "'\\t'"; 1095 break; 1096 case '\v': 1097 OS << "'\\v'"; 1098 break; 1099 default: 1100 if (value < 256 && isPrintable((unsigned char)value)) 1101 OS << "'" << (char)value << "'"; 1102 else if (value < 256) 1103 OS << "'\\x" << llvm::format("%02x", value) << "'"; 1104 else if (value <= 0xFFFF) 1105 OS << "'\\u" << llvm::format("%04x", value) << "'"; 1106 else 1107 OS << "'\\U" << llvm::format("%08x", value) << "'"; 1108 } 1109 } 1110 1111 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 1112 bool isSigned = Node->getType()->isSignedIntegerType(); 1113 OS << Node->getValue().toString(10, isSigned); 1114 1115 // Emit suffixes. Integer literals are always a builtin integer type. 1116 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1117 default: llvm_unreachable("Unexpected type for integer literal!"); 1118 case BuiltinType::Char_S: 1119 case BuiltinType::Char_U: OS << "i8"; break; 1120 case BuiltinType::UChar: OS << "Ui8"; break; 1121 case BuiltinType::Short: OS << "i16"; break; 1122 case BuiltinType::UShort: OS << "Ui16"; break; 1123 case BuiltinType::Int: break; // no suffix. 1124 case BuiltinType::UInt: OS << 'U'; break; 1125 case BuiltinType::Long: OS << 'L'; break; 1126 case BuiltinType::ULong: OS << "UL"; break; 1127 case BuiltinType::LongLong: OS << "LL"; break; 1128 case BuiltinType::ULongLong: OS << "ULL"; break; 1129 } 1130 } 1131 1132 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 1133 bool PrintSuffix) { 1134 SmallString<16> Str; 1135 Node->getValue().toString(Str); 1136 OS << Str; 1137 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 1138 OS << '.'; // Trailing dot in order to separate from ints. 1139 1140 if (!PrintSuffix) 1141 return; 1142 1143 // Emit suffixes. Float literals are always a builtin float type. 1144 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1145 default: llvm_unreachable("Unexpected type for float literal!"); 1146 case BuiltinType::Half: break; // FIXME: suffix? 1147 case BuiltinType::Double: break; // no suffix. 1148 case BuiltinType::Float: OS << 'F'; break; 1149 case BuiltinType::LongDouble: OS << 'L'; break; 1150 } 1151 } 1152 1153 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 1154 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 1155 } 1156 1157 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 1158 PrintExpr(Node->getSubExpr()); 1159 OS << "i"; 1160 } 1161 1162 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 1163 Str->outputString(OS); 1164 } 1165 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 1166 OS << "("; 1167 PrintExpr(Node->getSubExpr()); 1168 OS << ")"; 1169 } 1170 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1171 if (!Node->isPostfix()) { 1172 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1173 1174 // Print a space if this is an "identifier operator" like __real, or if 1175 // it might be concatenated incorrectly like '+'. 1176 switch (Node->getOpcode()) { 1177 default: break; 1178 case UO_Real: 1179 case UO_Imag: 1180 case UO_Extension: 1181 OS << ' '; 1182 break; 1183 case UO_Plus: 1184 case UO_Minus: 1185 if (isa<UnaryOperator>(Node->getSubExpr())) 1186 OS << ' '; 1187 break; 1188 } 1189 } 1190 PrintExpr(Node->getSubExpr()); 1191 1192 if (Node->isPostfix()) 1193 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1194 } 1195 1196 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1197 OS << "__builtin_offsetof("; 1198 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1199 OS << ", "; 1200 bool PrintedSomething = false; 1201 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1202 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 1203 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 1204 // Array node 1205 OS << "["; 1206 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1207 OS << "]"; 1208 PrintedSomething = true; 1209 continue; 1210 } 1211 1212 // Skip implicit base indirections. 1213 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 1214 continue; 1215 1216 // Field or identifier node. 1217 IdentifierInfo *Id = ON.getFieldName(); 1218 if (!Id) 1219 continue; 1220 1221 if (PrintedSomething) 1222 OS << "."; 1223 else 1224 PrintedSomething = true; 1225 OS << Id->getName(); 1226 } 1227 OS << ")"; 1228 } 1229 1230 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 1231 switch(Node->getKind()) { 1232 case UETT_SizeOf: 1233 OS << "sizeof"; 1234 break; 1235 case UETT_AlignOf: 1236 if (Policy.LangOpts.CPlusPlus) 1237 OS << "alignof"; 1238 else if (Policy.LangOpts.C11) 1239 OS << "_Alignof"; 1240 else 1241 OS << "__alignof"; 1242 break; 1243 case UETT_VecStep: 1244 OS << "vec_step"; 1245 break; 1246 case UETT_OpenMPRequiredSimdAlign: 1247 OS << "__builtin_omp_required_simd_align"; 1248 break; 1249 } 1250 if (Node->isArgumentType()) { 1251 OS << '('; 1252 Node->getArgumentType().print(OS, Policy); 1253 OS << ')'; 1254 } else { 1255 OS << " "; 1256 PrintExpr(Node->getArgumentExpr()); 1257 } 1258 } 1259 1260 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1261 OS << "_Generic("; 1262 PrintExpr(Node->getControllingExpr()); 1263 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 1264 OS << ", "; 1265 QualType T = Node->getAssocType(i); 1266 if (T.isNull()) 1267 OS << "default"; 1268 else 1269 T.print(OS, Policy); 1270 OS << ": "; 1271 PrintExpr(Node->getAssocExpr(i)); 1272 } 1273 OS << ")"; 1274 } 1275 1276 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1277 PrintExpr(Node->getLHS()); 1278 OS << "["; 1279 PrintExpr(Node->getRHS()); 1280 OS << "]"; 1281 } 1282 1283 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1284 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1285 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1286 // Don't print any defaulted arguments 1287 break; 1288 } 1289 1290 if (i) OS << ", "; 1291 PrintExpr(Call->getArg(i)); 1292 } 1293 } 1294 1295 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1296 PrintExpr(Call->getCallee()); 1297 OS << "("; 1298 PrintCallArgs(Call); 1299 OS << ")"; 1300 } 1301 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1302 // FIXME: Suppress printing implicit bases (like "this") 1303 PrintExpr(Node->getBase()); 1304 1305 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1306 FieldDecl *ParentDecl = ParentMember 1307 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr; 1308 1309 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1310 OS << (Node->isArrow() ? "->" : "."); 1311 1312 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1313 if (FD->isAnonymousStructOrUnion()) 1314 return; 1315 1316 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1317 Qualifier->print(OS, Policy); 1318 if (Node->hasTemplateKeyword()) 1319 OS << "template "; 1320 OS << Node->getMemberNameInfo(); 1321 if (Node->hasExplicitTemplateArgs()) 1322 TemplateSpecializationType::PrintTemplateArgumentList( 1323 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1324 } 1325 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1326 PrintExpr(Node->getBase()); 1327 OS << (Node->isArrow() ? "->isa" : ".isa"); 1328 } 1329 1330 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1331 PrintExpr(Node->getBase()); 1332 OS << "."; 1333 OS << Node->getAccessor().getName(); 1334 } 1335 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1336 OS << '('; 1337 Node->getTypeAsWritten().print(OS, Policy); 1338 OS << ')'; 1339 PrintExpr(Node->getSubExpr()); 1340 } 1341 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1342 OS << '('; 1343 Node->getType().print(OS, Policy); 1344 OS << ')'; 1345 PrintExpr(Node->getInitializer()); 1346 } 1347 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1348 // No need to print anything, simply forward to the subexpression. 1349 PrintExpr(Node->getSubExpr()); 1350 } 1351 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1352 PrintExpr(Node->getLHS()); 1353 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1354 PrintExpr(Node->getRHS()); 1355 } 1356 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1357 PrintExpr(Node->getLHS()); 1358 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1359 PrintExpr(Node->getRHS()); 1360 } 1361 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1362 PrintExpr(Node->getCond()); 1363 OS << " ? "; 1364 PrintExpr(Node->getLHS()); 1365 OS << " : "; 1366 PrintExpr(Node->getRHS()); 1367 } 1368 1369 // GNU extensions. 1370 1371 void 1372 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1373 PrintExpr(Node->getCommon()); 1374 OS << " ?: "; 1375 PrintExpr(Node->getFalseExpr()); 1376 } 1377 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1378 OS << "&&" << Node->getLabel()->getName(); 1379 } 1380 1381 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1382 OS << "("; 1383 PrintRawCompoundStmt(E->getSubStmt()); 1384 OS << ")"; 1385 } 1386 1387 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1388 OS << "__builtin_choose_expr("; 1389 PrintExpr(Node->getCond()); 1390 OS << ", "; 1391 PrintExpr(Node->getLHS()); 1392 OS << ", "; 1393 PrintExpr(Node->getRHS()); 1394 OS << ")"; 1395 } 1396 1397 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1398 OS << "__null"; 1399 } 1400 1401 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1402 OS << "__builtin_shufflevector("; 1403 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1404 if (i) OS << ", "; 1405 PrintExpr(Node->getExpr(i)); 1406 } 1407 OS << ")"; 1408 } 1409 1410 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1411 OS << "__builtin_convertvector("; 1412 PrintExpr(Node->getSrcExpr()); 1413 OS << ", "; 1414 Node->getType().print(OS, Policy); 1415 OS << ")"; 1416 } 1417 1418 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1419 if (Node->getSyntacticForm()) { 1420 Visit(Node->getSyntacticForm()); 1421 return; 1422 } 1423 1424 OS << "{"; 1425 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1426 if (i) OS << ", "; 1427 if (Node->getInit(i)) 1428 PrintExpr(Node->getInit(i)); 1429 else 1430 OS << "{}"; 1431 } 1432 OS << "}"; 1433 } 1434 1435 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1436 OS << "("; 1437 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1438 if (i) OS << ", "; 1439 PrintExpr(Node->getExpr(i)); 1440 } 1441 OS << ")"; 1442 } 1443 1444 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1445 bool NeedsEquals = true; 1446 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1447 DEnd = Node->designators_end(); 1448 D != DEnd; ++D) { 1449 if (D->isFieldDesignator()) { 1450 if (D->getDotLoc().isInvalid()) { 1451 if (IdentifierInfo *II = D->getFieldName()) { 1452 OS << II->getName() << ":"; 1453 NeedsEquals = false; 1454 } 1455 } else { 1456 OS << "." << D->getFieldName()->getName(); 1457 } 1458 } else { 1459 OS << "["; 1460 if (D->isArrayDesignator()) { 1461 PrintExpr(Node->getArrayIndex(*D)); 1462 } else { 1463 PrintExpr(Node->getArrayRangeStart(*D)); 1464 OS << " ... "; 1465 PrintExpr(Node->getArrayRangeEnd(*D)); 1466 } 1467 OS << "]"; 1468 } 1469 } 1470 1471 if (NeedsEquals) 1472 OS << " = "; 1473 else 1474 OS << " "; 1475 PrintExpr(Node->getInit()); 1476 } 1477 1478 void StmtPrinter::VisitDesignatedInitUpdateExpr( 1479 DesignatedInitUpdateExpr *Node) { 1480 OS << "{"; 1481 OS << "/*base*/"; 1482 PrintExpr(Node->getBase()); 1483 OS << ", "; 1484 1485 OS << "/*updater*/"; 1486 PrintExpr(Node->getUpdater()); 1487 OS << "}"; 1488 } 1489 1490 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) { 1491 OS << "/*no init*/"; 1492 } 1493 1494 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1495 if (Policy.LangOpts.CPlusPlus) { 1496 OS << "/*implicit*/"; 1497 Node->getType().print(OS, Policy); 1498 OS << "()"; 1499 } else { 1500 OS << "/*implicit*/("; 1501 Node->getType().print(OS, Policy); 1502 OS << ')'; 1503 if (Node->getType()->isRecordType()) 1504 OS << "{}"; 1505 else 1506 OS << 0; 1507 } 1508 } 1509 1510 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1511 OS << "__builtin_va_arg("; 1512 PrintExpr(Node->getSubExpr()); 1513 OS << ", "; 1514 Node->getType().print(OS, Policy); 1515 OS << ")"; 1516 } 1517 1518 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1519 PrintExpr(Node->getSyntacticForm()); 1520 } 1521 1522 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1523 const char *Name = nullptr; 1524 switch (Node->getOp()) { 1525 #define BUILTIN(ID, TYPE, ATTRS) 1526 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1527 case AtomicExpr::AO ## ID: \ 1528 Name = #ID "("; \ 1529 break; 1530 #include "clang/Basic/Builtins.def" 1531 } 1532 OS << Name; 1533 1534 // AtomicExpr stores its subexpressions in a permuted order. 1535 PrintExpr(Node->getPtr()); 1536 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1537 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1538 OS << ", "; 1539 PrintExpr(Node->getVal1()); 1540 } 1541 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1542 Node->isCmpXChg()) { 1543 OS << ", "; 1544 PrintExpr(Node->getVal2()); 1545 } 1546 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1547 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1548 OS << ", "; 1549 PrintExpr(Node->getWeak()); 1550 } 1551 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1552 OS << ", "; 1553 PrintExpr(Node->getOrder()); 1554 } 1555 if (Node->isCmpXChg()) { 1556 OS << ", "; 1557 PrintExpr(Node->getOrderFail()); 1558 } 1559 OS << ")"; 1560 } 1561 1562 // C++ 1563 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1564 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1565 "", 1566 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1567 Spelling, 1568 #include "clang/Basic/OperatorKinds.def" 1569 }; 1570 1571 OverloadedOperatorKind Kind = Node->getOperator(); 1572 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1573 if (Node->getNumArgs() == 1) { 1574 OS << OpStrings[Kind] << ' '; 1575 PrintExpr(Node->getArg(0)); 1576 } else { 1577 PrintExpr(Node->getArg(0)); 1578 OS << ' ' << OpStrings[Kind]; 1579 } 1580 } else if (Kind == OO_Arrow) { 1581 PrintExpr(Node->getArg(0)); 1582 } else if (Kind == OO_Call) { 1583 PrintExpr(Node->getArg(0)); 1584 OS << '('; 1585 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1586 if (ArgIdx > 1) 1587 OS << ", "; 1588 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1589 PrintExpr(Node->getArg(ArgIdx)); 1590 } 1591 OS << ')'; 1592 } else if (Kind == OO_Subscript) { 1593 PrintExpr(Node->getArg(0)); 1594 OS << '['; 1595 PrintExpr(Node->getArg(1)); 1596 OS << ']'; 1597 } else if (Node->getNumArgs() == 1) { 1598 OS << OpStrings[Kind] << ' '; 1599 PrintExpr(Node->getArg(0)); 1600 } else if (Node->getNumArgs() == 2) { 1601 PrintExpr(Node->getArg(0)); 1602 OS << ' ' << OpStrings[Kind] << ' '; 1603 PrintExpr(Node->getArg(1)); 1604 } else { 1605 llvm_unreachable("unknown overloaded operator"); 1606 } 1607 } 1608 1609 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1610 // If we have a conversion operator call only print the argument. 1611 CXXMethodDecl *MD = Node->getMethodDecl(); 1612 if (MD && isa<CXXConversionDecl>(MD)) { 1613 PrintExpr(Node->getImplicitObjectArgument()); 1614 return; 1615 } 1616 VisitCallExpr(cast<CallExpr>(Node)); 1617 } 1618 1619 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1620 PrintExpr(Node->getCallee()); 1621 OS << "<<<"; 1622 PrintCallArgs(Node->getConfig()); 1623 OS << ">>>("; 1624 PrintCallArgs(Node); 1625 OS << ")"; 1626 } 1627 1628 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1629 OS << Node->getCastName() << '<'; 1630 Node->getTypeAsWritten().print(OS, Policy); 1631 OS << ">("; 1632 PrintExpr(Node->getSubExpr()); 1633 OS << ")"; 1634 } 1635 1636 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1637 VisitCXXNamedCastExpr(Node); 1638 } 1639 1640 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1641 VisitCXXNamedCastExpr(Node); 1642 } 1643 1644 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1645 VisitCXXNamedCastExpr(Node); 1646 } 1647 1648 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1649 VisitCXXNamedCastExpr(Node); 1650 } 1651 1652 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1653 OS << "typeid("; 1654 if (Node->isTypeOperand()) { 1655 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1656 } else { 1657 PrintExpr(Node->getExprOperand()); 1658 } 1659 OS << ")"; 1660 } 1661 1662 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1663 OS << "__uuidof("; 1664 if (Node->isTypeOperand()) { 1665 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1666 } else { 1667 PrintExpr(Node->getExprOperand()); 1668 } 1669 OS << ")"; 1670 } 1671 1672 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1673 PrintExpr(Node->getBaseExpr()); 1674 if (Node->isArrow()) 1675 OS << "->"; 1676 else 1677 OS << "."; 1678 if (NestedNameSpecifier *Qualifier = 1679 Node->getQualifierLoc().getNestedNameSpecifier()) 1680 Qualifier->print(OS, Policy); 1681 OS << Node->getPropertyDecl()->getDeclName(); 1682 } 1683 1684 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1685 switch (Node->getLiteralOperatorKind()) { 1686 case UserDefinedLiteral::LOK_Raw: 1687 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1688 break; 1689 case UserDefinedLiteral::LOK_Template: { 1690 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1691 const TemplateArgumentList *Args = 1692 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1693 assert(Args); 1694 1695 if (Args->size() != 1) { 1696 OS << "operator \"\" " << Node->getUDSuffix()->getName(); 1697 TemplateSpecializationType::PrintTemplateArgumentList( 1698 OS, Args->data(), Args->size(), Policy); 1699 OS << "()"; 1700 return; 1701 } 1702 1703 const TemplateArgument &Pack = Args->get(0); 1704 for (const auto &P : Pack.pack_elements()) { 1705 char C = (char)P.getAsIntegral().getZExtValue(); 1706 OS << C; 1707 } 1708 break; 1709 } 1710 case UserDefinedLiteral::LOK_Integer: { 1711 // Print integer literal without suffix. 1712 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1713 OS << Int->getValue().toString(10, /*isSigned*/false); 1714 break; 1715 } 1716 case UserDefinedLiteral::LOK_Floating: { 1717 // Print floating literal without suffix. 1718 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1719 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1720 break; 1721 } 1722 case UserDefinedLiteral::LOK_String: 1723 case UserDefinedLiteral::LOK_Character: 1724 PrintExpr(Node->getCookedLiteral()); 1725 break; 1726 } 1727 OS << Node->getUDSuffix()->getName(); 1728 } 1729 1730 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1731 OS << (Node->getValue() ? "true" : "false"); 1732 } 1733 1734 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1735 OS << "nullptr"; 1736 } 1737 1738 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1739 OS << "this"; 1740 } 1741 1742 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1743 if (!Node->getSubExpr()) 1744 OS << "throw"; 1745 else { 1746 OS << "throw "; 1747 PrintExpr(Node->getSubExpr()); 1748 } 1749 } 1750 1751 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1752 // Nothing to print: we picked up the default argument. 1753 } 1754 1755 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1756 // Nothing to print: we picked up the default initializer. 1757 } 1758 1759 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1760 Node->getType().print(OS, Policy); 1761 // If there are no parens, this is list-initialization, and the braces are 1762 // part of the syntax of the inner construct. 1763 if (Node->getLParenLoc().isValid()) 1764 OS << "("; 1765 PrintExpr(Node->getSubExpr()); 1766 if (Node->getLParenLoc().isValid()) 1767 OS << ")"; 1768 } 1769 1770 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1771 PrintExpr(Node->getSubExpr()); 1772 } 1773 1774 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1775 Node->getType().print(OS, Policy); 1776 if (Node->isStdInitListInitialization()) 1777 /* Nothing to do; braces are part of creating the std::initializer_list. */; 1778 else if (Node->isListInitialization()) 1779 OS << "{"; 1780 else 1781 OS << "("; 1782 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1783 ArgEnd = Node->arg_end(); 1784 Arg != ArgEnd; ++Arg) { 1785 if ((*Arg)->isDefaultArgument()) 1786 break; 1787 if (Arg != Node->arg_begin()) 1788 OS << ", "; 1789 PrintExpr(*Arg); 1790 } 1791 if (Node->isStdInitListInitialization()) 1792 /* See above. */; 1793 else if (Node->isListInitialization()) 1794 OS << "}"; 1795 else 1796 OS << ")"; 1797 } 1798 1799 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1800 OS << '['; 1801 bool NeedComma = false; 1802 switch (Node->getCaptureDefault()) { 1803 case LCD_None: 1804 break; 1805 1806 case LCD_ByCopy: 1807 OS << '='; 1808 NeedComma = true; 1809 break; 1810 1811 case LCD_ByRef: 1812 OS << '&'; 1813 NeedComma = true; 1814 break; 1815 } 1816 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1817 CEnd = Node->explicit_capture_end(); 1818 C != CEnd; 1819 ++C) { 1820 if (NeedComma) 1821 OS << ", "; 1822 NeedComma = true; 1823 1824 switch (C->getCaptureKind()) { 1825 case LCK_This: 1826 OS << "this"; 1827 break; 1828 1829 case LCK_ByRef: 1830 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C)) 1831 OS << '&'; 1832 OS << C->getCapturedVar()->getName(); 1833 break; 1834 1835 case LCK_ByCopy: 1836 OS << C->getCapturedVar()->getName(); 1837 break; 1838 case LCK_VLAType: 1839 llvm_unreachable("VLA type in explicit captures."); 1840 } 1841 1842 if (Node->isInitCapture(C)) 1843 PrintExpr(C->getCapturedVar()->getInit()); 1844 } 1845 OS << ']'; 1846 1847 if (Node->hasExplicitParameters()) { 1848 OS << " ("; 1849 CXXMethodDecl *Method = Node->getCallOperator(); 1850 NeedComma = false; 1851 for (auto P : Method->params()) { 1852 if (NeedComma) { 1853 OS << ", "; 1854 } else { 1855 NeedComma = true; 1856 } 1857 std::string ParamStr = P->getNameAsString(); 1858 P->getOriginalType().print(OS, Policy, ParamStr); 1859 } 1860 if (Method->isVariadic()) { 1861 if (NeedComma) 1862 OS << ", "; 1863 OS << "..."; 1864 } 1865 OS << ')'; 1866 1867 if (Node->isMutable()) 1868 OS << " mutable"; 1869 1870 const FunctionProtoType *Proto 1871 = Method->getType()->getAs<FunctionProtoType>(); 1872 Proto->printExceptionSpecification(OS, Policy); 1873 1874 // FIXME: Attributes 1875 1876 // Print the trailing return type if it was specified in the source. 1877 if (Node->hasExplicitResultType()) { 1878 OS << " -> "; 1879 Proto->getReturnType().print(OS, Policy); 1880 } 1881 } 1882 1883 // Print the body. 1884 CompoundStmt *Body = Node->getBody(); 1885 OS << ' '; 1886 PrintStmt(Body); 1887 } 1888 1889 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1890 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1891 TSInfo->getType().print(OS, Policy); 1892 else 1893 Node->getType().print(OS, Policy); 1894 OS << "()"; 1895 } 1896 1897 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1898 if (E->isGlobalNew()) 1899 OS << "::"; 1900 OS << "new "; 1901 unsigned NumPlace = E->getNumPlacementArgs(); 1902 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1903 OS << "("; 1904 PrintExpr(E->getPlacementArg(0)); 1905 for (unsigned i = 1; i < NumPlace; ++i) { 1906 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1907 break; 1908 OS << ", "; 1909 PrintExpr(E->getPlacementArg(i)); 1910 } 1911 OS << ") "; 1912 } 1913 if (E->isParenTypeId()) 1914 OS << "("; 1915 std::string TypeS; 1916 if (Expr *Size = E->getArraySize()) { 1917 llvm::raw_string_ostream s(TypeS); 1918 s << '['; 1919 Size->printPretty(s, Helper, Policy); 1920 s << ']'; 1921 } 1922 E->getAllocatedType().print(OS, Policy, TypeS); 1923 if (E->isParenTypeId()) 1924 OS << ")"; 1925 1926 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1927 if (InitStyle) { 1928 if (InitStyle == CXXNewExpr::CallInit) 1929 OS << "("; 1930 PrintExpr(E->getInitializer()); 1931 if (InitStyle == CXXNewExpr::CallInit) 1932 OS << ")"; 1933 } 1934 } 1935 1936 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1937 if (E->isGlobalDelete()) 1938 OS << "::"; 1939 OS << "delete "; 1940 if (E->isArrayForm()) 1941 OS << "[] "; 1942 PrintExpr(E->getArgument()); 1943 } 1944 1945 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1946 PrintExpr(E->getBase()); 1947 if (E->isArrow()) 1948 OS << "->"; 1949 else 1950 OS << '.'; 1951 if (E->getQualifier()) 1952 E->getQualifier()->print(OS, Policy); 1953 OS << "~"; 1954 1955 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1956 OS << II->getName(); 1957 else 1958 E->getDestroyedType().print(OS, Policy); 1959 } 1960 1961 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1962 if (E->isListInitialization() && !E->isStdInitListInitialization()) 1963 OS << "{"; 1964 1965 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1966 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1967 // Don't print any defaulted arguments 1968 break; 1969 } 1970 1971 if (i) OS << ", "; 1972 PrintExpr(E->getArg(i)); 1973 } 1974 1975 if (E->isListInitialization() && !E->isStdInitListInitialization()) 1976 OS << "}"; 1977 } 1978 1979 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 1980 PrintExpr(E->getSubExpr()); 1981 } 1982 1983 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1984 // Just forward to the subexpression. 1985 PrintExpr(E->getSubExpr()); 1986 } 1987 1988 void 1989 StmtPrinter::VisitCXXUnresolvedConstructExpr( 1990 CXXUnresolvedConstructExpr *Node) { 1991 Node->getTypeAsWritten().print(OS, Policy); 1992 OS << "("; 1993 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1994 ArgEnd = Node->arg_end(); 1995 Arg != ArgEnd; ++Arg) { 1996 if (Arg != Node->arg_begin()) 1997 OS << ", "; 1998 PrintExpr(*Arg); 1999 } 2000 OS << ")"; 2001 } 2002 2003 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 2004 CXXDependentScopeMemberExpr *Node) { 2005 if (!Node->isImplicitAccess()) { 2006 PrintExpr(Node->getBase()); 2007 OS << (Node->isArrow() ? "->" : "."); 2008 } 2009 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2010 Qualifier->print(OS, Policy); 2011 if (Node->hasTemplateKeyword()) 2012 OS << "template "; 2013 OS << Node->getMemberNameInfo(); 2014 if (Node->hasExplicitTemplateArgs()) 2015 TemplateSpecializationType::PrintTemplateArgumentList( 2016 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 2017 } 2018 2019 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 2020 if (!Node->isImplicitAccess()) { 2021 PrintExpr(Node->getBase()); 2022 OS << (Node->isArrow() ? "->" : "."); 2023 } 2024 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2025 Qualifier->print(OS, Policy); 2026 if (Node->hasTemplateKeyword()) 2027 OS << "template "; 2028 OS << Node->getMemberNameInfo(); 2029 if (Node->hasExplicitTemplateArgs()) 2030 TemplateSpecializationType::PrintTemplateArgumentList( 2031 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 2032 } 2033 2034 static const char *getTypeTraitName(TypeTrait TT) { 2035 switch (TT) { 2036 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 2037 case clang::UTT_##Name: return #Spelling; 2038 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 2039 case clang::BTT_##Name: return #Spelling; 2040 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 2041 case clang::TT_##Name: return #Spelling; 2042 #include "clang/Basic/TokenKinds.def" 2043 } 2044 llvm_unreachable("Type trait not covered by switch"); 2045 } 2046 2047 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 2048 switch (ATT) { 2049 case ATT_ArrayRank: return "__array_rank"; 2050 case ATT_ArrayExtent: return "__array_extent"; 2051 } 2052 llvm_unreachable("Array type trait not covered by switch"); 2053 } 2054 2055 static const char *getExpressionTraitName(ExpressionTrait ET) { 2056 switch (ET) { 2057 case ET_IsLValueExpr: return "__is_lvalue_expr"; 2058 case ET_IsRValueExpr: return "__is_rvalue_expr"; 2059 } 2060 llvm_unreachable("Expression type trait not covered by switch"); 2061 } 2062 2063 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 2064 OS << getTypeTraitName(E->getTrait()) << "("; 2065 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 2066 if (I > 0) 2067 OS << ", "; 2068 E->getArg(I)->getType().print(OS, Policy); 2069 } 2070 OS << ")"; 2071 } 2072 2073 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2074 OS << getTypeTraitName(E->getTrait()) << '('; 2075 E->getQueriedType().print(OS, Policy); 2076 OS << ')'; 2077 } 2078 2079 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2080 OS << getExpressionTraitName(E->getTrait()) << '('; 2081 PrintExpr(E->getQueriedExpression()); 2082 OS << ')'; 2083 } 2084 2085 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 2086 OS << "noexcept("; 2087 PrintExpr(E->getOperand()); 2088 OS << ")"; 2089 } 2090 2091 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 2092 PrintExpr(E->getPattern()); 2093 OS << "..."; 2094 } 2095 2096 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2097 OS << "sizeof...(" << *E->getPack() << ")"; 2098 } 2099 2100 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 2101 SubstNonTypeTemplateParmPackExpr *Node) { 2102 OS << *Node->getParameterPack(); 2103 } 2104 2105 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 2106 SubstNonTypeTemplateParmExpr *Node) { 2107 Visit(Node->getReplacement()); 2108 } 2109 2110 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 2111 OS << *E->getParameterPack(); 2112 } 2113 2114 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 2115 PrintExpr(Node->GetTemporaryExpr()); 2116 } 2117 2118 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 2119 OS << "("; 2120 if (E->getLHS()) { 2121 PrintExpr(E->getLHS()); 2122 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2123 } 2124 OS << "..."; 2125 if (E->getRHS()) { 2126 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2127 PrintExpr(E->getRHS()); 2128 } 2129 OS << ")"; 2130 } 2131 2132 // Obj-C 2133 2134 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 2135 OS << "@"; 2136 VisitStringLiteral(Node->getString()); 2137 } 2138 2139 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 2140 OS << "@"; 2141 Visit(E->getSubExpr()); 2142 } 2143 2144 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 2145 OS << "@[ "; 2146 ObjCArrayLiteral::child_range Ch = E->children(); 2147 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) { 2148 if (I != Ch.begin()) 2149 OS << ", "; 2150 Visit(*I); 2151 } 2152 OS << " ]"; 2153 } 2154 2155 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 2156 OS << "@{ "; 2157 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 2158 if (I > 0) 2159 OS << ", "; 2160 2161 ObjCDictionaryElement Element = E->getKeyValueElement(I); 2162 Visit(Element.Key); 2163 OS << " : "; 2164 Visit(Element.Value); 2165 if (Element.isPackExpansion()) 2166 OS << "..."; 2167 } 2168 OS << " }"; 2169 } 2170 2171 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 2172 OS << "@encode("; 2173 Node->getEncodedType().print(OS, Policy); 2174 OS << ')'; 2175 } 2176 2177 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 2178 OS << "@selector("; 2179 Node->getSelector().print(OS); 2180 OS << ')'; 2181 } 2182 2183 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 2184 OS << "@protocol(" << *Node->getProtocol() << ')'; 2185 } 2186 2187 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 2188 OS << "["; 2189 switch (Mess->getReceiverKind()) { 2190 case ObjCMessageExpr::Instance: 2191 PrintExpr(Mess->getInstanceReceiver()); 2192 break; 2193 2194 case ObjCMessageExpr::Class: 2195 Mess->getClassReceiver().print(OS, Policy); 2196 break; 2197 2198 case ObjCMessageExpr::SuperInstance: 2199 case ObjCMessageExpr::SuperClass: 2200 OS << "Super"; 2201 break; 2202 } 2203 2204 OS << ' '; 2205 Selector selector = Mess->getSelector(); 2206 if (selector.isUnarySelector()) { 2207 OS << selector.getNameForSlot(0); 2208 } else { 2209 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 2210 if (i < selector.getNumArgs()) { 2211 if (i > 0) OS << ' '; 2212 if (selector.getIdentifierInfoForSlot(i)) 2213 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 2214 else 2215 OS << ":"; 2216 } 2217 else OS << ", "; // Handle variadic methods. 2218 2219 PrintExpr(Mess->getArg(i)); 2220 } 2221 } 2222 OS << "]"; 2223 } 2224 2225 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 2226 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 2227 } 2228 2229 void 2230 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2231 PrintExpr(E->getSubExpr()); 2232 } 2233 2234 void 2235 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2236 OS << '(' << E->getBridgeKindName(); 2237 E->getType().print(OS, Policy); 2238 OS << ')'; 2239 PrintExpr(E->getSubExpr()); 2240 } 2241 2242 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2243 BlockDecl *BD = Node->getBlockDecl(); 2244 OS << "^"; 2245 2246 const FunctionType *AFT = Node->getFunctionType(); 2247 2248 if (isa<FunctionNoProtoType>(AFT)) { 2249 OS << "()"; 2250 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2251 OS << '('; 2252 for (BlockDecl::param_iterator AI = BD->param_begin(), 2253 E = BD->param_end(); AI != E; ++AI) { 2254 if (AI != BD->param_begin()) OS << ", "; 2255 std::string ParamStr = (*AI)->getNameAsString(); 2256 (*AI)->getType().print(OS, Policy, ParamStr); 2257 } 2258 2259 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2260 if (FT->isVariadic()) { 2261 if (!BD->param_empty()) OS << ", "; 2262 OS << "..."; 2263 } 2264 OS << ')'; 2265 } 2266 OS << "{ }"; 2267 } 2268 2269 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2270 PrintExpr(Node->getSourceExpr()); 2271 } 2272 2273 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 2274 // TODO: Print something reasonable for a TypoExpr, if necessary. 2275 assert(false && "Cannot print TypoExpr nodes"); 2276 } 2277 2278 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2279 OS << "__builtin_astype("; 2280 PrintExpr(Node->getSrcExpr()); 2281 OS << ", "; 2282 Node->getType().print(OS, Policy); 2283 OS << ")"; 2284 } 2285 2286 //===----------------------------------------------------------------------===// 2287 // Stmt method implementations 2288 //===----------------------------------------------------------------------===// 2289 2290 void Stmt::dumpPretty(const ASTContext &Context) const { 2291 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2292 } 2293 2294 void Stmt::printPretty(raw_ostream &OS, 2295 PrinterHelper *Helper, 2296 const PrintingPolicy &Policy, 2297 unsigned Indentation) const { 2298 StmtPrinter P(OS, Helper, Policy, Indentation); 2299 P.Visit(const_cast<Stmt*>(this)); 2300 } 2301 2302 //===----------------------------------------------------------------------===// 2303 // PrinterHelper 2304 //===----------------------------------------------------------------------===// 2305 2306 // Implement virtual destructor. 2307 PrinterHelper::~PrinterHelper() {} 2308