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