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