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