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