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