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