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 OS << (I == Node->varlist_begin() ? StartSym : ',') 634 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl()); 635 } 636 637 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) { 638 if (!Node->varlist_empty()) { 639 OS << "private"; 640 VisitOMPClauseList(Node, '('); 641 OS << ")"; 642 } 643 } 644 645 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) { 646 if (!Node->varlist_empty()) { 647 OS << "firstprivate"; 648 VisitOMPClauseList(Node, '('); 649 OS << ")"; 650 } 651 } 652 653 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) { 654 if (!Node->varlist_empty()) { 655 OS << "shared"; 656 VisitOMPClauseList(Node, '('); 657 OS << ")"; 658 } 659 } 660 661 } 662 663 //===----------------------------------------------------------------------===// 664 // OpenMP directives printing methods 665 //===----------------------------------------------------------------------===// 666 667 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) { 668 OMPClausePrinter Printer(OS, Policy); 669 ArrayRef<OMPClause *> Clauses = S->clauses(); 670 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 671 I != E; ++I) 672 if (*I && !(*I)->isImplicit()) { 673 Printer.Visit(*I); 674 OS << ' '; 675 } 676 OS << "\n"; 677 if (S->getAssociatedStmt()) { 678 assert(isa<CapturedStmt>(S->getAssociatedStmt()) && 679 "Expected captured statement!"); 680 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt(); 681 PrintStmt(CS); 682 } 683 } 684 685 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) { 686 Indent() << "#pragma omp parallel "; 687 PrintOMPExecutableDirective(Node); 688 } 689 690 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) { 691 Indent() << "#pragma omp simd "; 692 PrintOMPExecutableDirective(Node); 693 } 694 695 //===----------------------------------------------------------------------===// 696 // Expr printing methods. 697 //===----------------------------------------------------------------------===// 698 699 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 700 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 701 Qualifier->print(OS, Policy); 702 if (Node->hasTemplateKeyword()) 703 OS << "template "; 704 OS << Node->getNameInfo(); 705 if (Node->hasExplicitTemplateArgs()) 706 TemplateSpecializationType::PrintTemplateArgumentList( 707 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 708 } 709 710 void StmtPrinter::VisitDependentScopeDeclRefExpr( 711 DependentScopeDeclRefExpr *Node) { 712 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 713 Qualifier->print(OS, Policy); 714 if (Node->hasTemplateKeyword()) 715 OS << "template "; 716 OS << Node->getNameInfo(); 717 if (Node->hasExplicitTemplateArgs()) 718 TemplateSpecializationType::PrintTemplateArgumentList( 719 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 720 } 721 722 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 723 if (Node->getQualifier()) 724 Node->getQualifier()->print(OS, Policy); 725 if (Node->hasTemplateKeyword()) 726 OS << "template "; 727 OS << Node->getNameInfo(); 728 if (Node->hasExplicitTemplateArgs()) 729 TemplateSpecializationType::PrintTemplateArgumentList( 730 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 731 } 732 733 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 734 if (Node->getBase()) { 735 PrintExpr(Node->getBase()); 736 OS << (Node->isArrow() ? "->" : "."); 737 } 738 OS << *Node->getDecl(); 739 } 740 741 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 742 if (Node->isSuperReceiver()) 743 OS << "super."; 744 else if (Node->isObjectReceiver() && Node->getBase()) { 745 PrintExpr(Node->getBase()); 746 OS << "."; 747 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 748 OS << Node->getClassReceiver()->getName() << "."; 749 } 750 751 if (Node->isImplicitProperty()) 752 Node->getImplicitPropertyGetter()->getSelector().print(OS); 753 else 754 OS << Node->getExplicitProperty()->getName(); 755 } 756 757 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 758 759 PrintExpr(Node->getBaseExpr()); 760 OS << "["; 761 PrintExpr(Node->getKeyExpr()); 762 OS << "]"; 763 } 764 765 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 766 switch (Node->getIdentType()) { 767 default: 768 llvm_unreachable("unknown case"); 769 case PredefinedExpr::Func: 770 OS << "__func__"; 771 break; 772 case PredefinedExpr::Function: 773 OS << "__FUNCTION__"; 774 break; 775 case PredefinedExpr::FuncDName: 776 OS << "__FUNCDNAME__"; 777 break; 778 case PredefinedExpr::LFunction: 779 OS << "L__FUNCTION__"; 780 break; 781 case PredefinedExpr::PrettyFunction: 782 OS << "__PRETTY_FUNCTION__"; 783 break; 784 } 785 } 786 787 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 788 unsigned value = Node->getValue(); 789 790 switch (Node->getKind()) { 791 case CharacterLiteral::Ascii: break; // no prefix. 792 case CharacterLiteral::Wide: OS << 'L'; break; 793 case CharacterLiteral::UTF16: OS << 'u'; break; 794 case CharacterLiteral::UTF32: OS << 'U'; break; 795 } 796 797 switch (value) { 798 case '\\': 799 OS << "'\\\\'"; 800 break; 801 case '\'': 802 OS << "'\\''"; 803 break; 804 case '\a': 805 // TODO: K&R: the meaning of '\\a' is different in traditional C 806 OS << "'\\a'"; 807 break; 808 case '\b': 809 OS << "'\\b'"; 810 break; 811 // Nonstandard escape sequence. 812 /*case '\e': 813 OS << "'\\e'"; 814 break;*/ 815 case '\f': 816 OS << "'\\f'"; 817 break; 818 case '\n': 819 OS << "'\\n'"; 820 break; 821 case '\r': 822 OS << "'\\r'"; 823 break; 824 case '\t': 825 OS << "'\\t'"; 826 break; 827 case '\v': 828 OS << "'\\v'"; 829 break; 830 default: 831 if (value < 256 && isPrintable((unsigned char)value)) 832 OS << "'" << (char)value << "'"; 833 else if (value < 256) 834 OS << "'\\x" << llvm::format("%02x", value) << "'"; 835 else if (value <= 0xFFFF) 836 OS << "'\\u" << llvm::format("%04x", value) << "'"; 837 else 838 OS << "'\\U" << llvm::format("%08x", value) << "'"; 839 } 840 } 841 842 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 843 bool isSigned = Node->getType()->isSignedIntegerType(); 844 OS << Node->getValue().toString(10, isSigned); 845 846 // Emit suffixes. Integer literals are always a builtin integer type. 847 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 848 default: llvm_unreachable("Unexpected type for integer literal!"); 849 // FIXME: The Short and UShort cases are to handle cases where a short 850 // integeral literal is formed during template instantiation. They should 851 // be removed when template instantiation no longer needs integer literals. 852 case BuiltinType::Short: 853 case BuiltinType::UShort: 854 case BuiltinType::Int: break; // no suffix. 855 case BuiltinType::UInt: OS << 'U'; break; 856 case BuiltinType::Long: OS << 'L'; break; 857 case BuiltinType::ULong: OS << "UL"; break; 858 case BuiltinType::LongLong: OS << "LL"; break; 859 case BuiltinType::ULongLong: OS << "ULL"; break; 860 case BuiltinType::Int128: OS << "i128"; break; 861 case BuiltinType::UInt128: OS << "Ui128"; break; 862 } 863 } 864 865 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 866 bool PrintSuffix) { 867 SmallString<16> Str; 868 Node->getValue().toString(Str); 869 OS << Str; 870 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 871 OS << '.'; // Trailing dot in order to separate from ints. 872 873 if (!PrintSuffix) 874 return; 875 876 // Emit suffixes. Float literals are always a builtin float type. 877 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 878 default: llvm_unreachable("Unexpected type for float literal!"); 879 case BuiltinType::Half: break; // FIXME: suffix? 880 case BuiltinType::Double: break; // no suffix. 881 case BuiltinType::Float: OS << 'F'; break; 882 case BuiltinType::LongDouble: OS << 'L'; break; 883 } 884 } 885 886 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 887 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 888 } 889 890 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 891 PrintExpr(Node->getSubExpr()); 892 OS << "i"; 893 } 894 895 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 896 Str->outputString(OS); 897 } 898 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 899 OS << "("; 900 PrintExpr(Node->getSubExpr()); 901 OS << ")"; 902 } 903 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 904 if (!Node->isPostfix()) { 905 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 906 907 // Print a space if this is an "identifier operator" like __real, or if 908 // it might be concatenated incorrectly like '+'. 909 switch (Node->getOpcode()) { 910 default: break; 911 case UO_Real: 912 case UO_Imag: 913 case UO_Extension: 914 OS << ' '; 915 break; 916 case UO_Plus: 917 case UO_Minus: 918 if (isa<UnaryOperator>(Node->getSubExpr())) 919 OS << ' '; 920 break; 921 } 922 } 923 PrintExpr(Node->getSubExpr()); 924 925 if (Node->isPostfix()) 926 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 927 } 928 929 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 930 OS << "__builtin_offsetof("; 931 Node->getTypeSourceInfo()->getType().print(OS, Policy); 932 OS << ", "; 933 bool PrintedSomething = false; 934 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 935 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 936 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 937 // Array node 938 OS << "["; 939 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 940 OS << "]"; 941 PrintedSomething = true; 942 continue; 943 } 944 945 // Skip implicit base indirections. 946 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 947 continue; 948 949 // Field or identifier node. 950 IdentifierInfo *Id = ON.getFieldName(); 951 if (!Id) 952 continue; 953 954 if (PrintedSomething) 955 OS << "."; 956 else 957 PrintedSomething = true; 958 OS << Id->getName(); 959 } 960 OS << ")"; 961 } 962 963 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 964 switch(Node->getKind()) { 965 case UETT_SizeOf: 966 OS << "sizeof"; 967 break; 968 case UETT_AlignOf: 969 if (Policy.LangOpts.CPlusPlus) 970 OS << "alignof"; 971 else if (Policy.LangOpts.C11) 972 OS << "_Alignof"; 973 else 974 OS << "__alignof"; 975 break; 976 case UETT_VecStep: 977 OS << "vec_step"; 978 break; 979 } 980 if (Node->isArgumentType()) { 981 OS << '('; 982 Node->getArgumentType().print(OS, Policy); 983 OS << ')'; 984 } else { 985 OS << " "; 986 PrintExpr(Node->getArgumentExpr()); 987 } 988 } 989 990 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 991 OS << "_Generic("; 992 PrintExpr(Node->getControllingExpr()); 993 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 994 OS << ", "; 995 QualType T = Node->getAssocType(i); 996 if (T.isNull()) 997 OS << "default"; 998 else 999 T.print(OS, Policy); 1000 OS << ": "; 1001 PrintExpr(Node->getAssocExpr(i)); 1002 } 1003 OS << ")"; 1004 } 1005 1006 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1007 PrintExpr(Node->getLHS()); 1008 OS << "["; 1009 PrintExpr(Node->getRHS()); 1010 OS << "]"; 1011 } 1012 1013 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1014 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1015 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1016 // Don't print any defaulted arguments 1017 break; 1018 } 1019 1020 if (i) OS << ", "; 1021 PrintExpr(Call->getArg(i)); 1022 } 1023 } 1024 1025 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1026 PrintExpr(Call->getCallee()); 1027 OS << "("; 1028 PrintCallArgs(Call); 1029 OS << ")"; 1030 } 1031 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1032 // FIXME: Suppress printing implicit bases (like "this") 1033 PrintExpr(Node->getBase()); 1034 1035 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1036 FieldDecl *ParentDecl = ParentMember 1037 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL; 1038 1039 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1040 OS << (Node->isArrow() ? "->" : "."); 1041 1042 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1043 if (FD->isAnonymousStructOrUnion()) 1044 return; 1045 1046 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1047 Qualifier->print(OS, Policy); 1048 if (Node->hasTemplateKeyword()) 1049 OS << "template "; 1050 OS << Node->getMemberNameInfo(); 1051 if (Node->hasExplicitTemplateArgs()) 1052 TemplateSpecializationType::PrintTemplateArgumentList( 1053 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1054 } 1055 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1056 PrintExpr(Node->getBase()); 1057 OS << (Node->isArrow() ? "->isa" : ".isa"); 1058 } 1059 1060 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1061 PrintExpr(Node->getBase()); 1062 OS << "."; 1063 OS << Node->getAccessor().getName(); 1064 } 1065 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1066 OS << '('; 1067 Node->getTypeAsWritten().print(OS, Policy); 1068 OS << ')'; 1069 PrintExpr(Node->getSubExpr()); 1070 } 1071 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1072 OS << '('; 1073 Node->getType().print(OS, Policy); 1074 OS << ')'; 1075 PrintExpr(Node->getInitializer()); 1076 } 1077 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1078 // No need to print anything, simply forward to the subexpression. 1079 PrintExpr(Node->getSubExpr()); 1080 } 1081 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1082 PrintExpr(Node->getLHS()); 1083 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1084 PrintExpr(Node->getRHS()); 1085 } 1086 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1087 PrintExpr(Node->getLHS()); 1088 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1089 PrintExpr(Node->getRHS()); 1090 } 1091 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1092 PrintExpr(Node->getCond()); 1093 OS << " ? "; 1094 PrintExpr(Node->getLHS()); 1095 OS << " : "; 1096 PrintExpr(Node->getRHS()); 1097 } 1098 1099 // GNU extensions. 1100 1101 void 1102 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1103 PrintExpr(Node->getCommon()); 1104 OS << " ?: "; 1105 PrintExpr(Node->getFalseExpr()); 1106 } 1107 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1108 OS << "&&" << Node->getLabel()->getName(); 1109 } 1110 1111 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1112 OS << "("; 1113 PrintRawCompoundStmt(E->getSubStmt()); 1114 OS << ")"; 1115 } 1116 1117 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1118 OS << "__builtin_choose_expr("; 1119 PrintExpr(Node->getCond()); 1120 OS << ", "; 1121 PrintExpr(Node->getLHS()); 1122 OS << ", "; 1123 PrintExpr(Node->getRHS()); 1124 OS << ")"; 1125 } 1126 1127 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1128 OS << "__null"; 1129 } 1130 1131 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1132 OS << "__builtin_shufflevector("; 1133 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1134 if (i) OS << ", "; 1135 PrintExpr(Node->getExpr(i)); 1136 } 1137 OS << ")"; 1138 } 1139 1140 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1141 OS << "__builtin_convertvector("; 1142 PrintExpr(Node->getSrcExpr()); 1143 OS << ", "; 1144 Node->getType().print(OS, Policy); 1145 OS << ")"; 1146 } 1147 1148 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1149 if (Node->getSyntacticForm()) { 1150 Visit(Node->getSyntacticForm()); 1151 return; 1152 } 1153 1154 OS << "{ "; 1155 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1156 if (i) OS << ", "; 1157 if (Node->getInit(i)) 1158 PrintExpr(Node->getInit(i)); 1159 else 1160 OS << "0"; 1161 } 1162 OS << " }"; 1163 } 1164 1165 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1166 OS << "( "; 1167 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1168 if (i) OS << ", "; 1169 PrintExpr(Node->getExpr(i)); 1170 } 1171 OS << " )"; 1172 } 1173 1174 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1175 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1176 DEnd = Node->designators_end(); 1177 D != DEnd; ++D) { 1178 if (D->isFieldDesignator()) { 1179 if (D->getDotLoc().isInvalid()) 1180 OS << D->getFieldName()->getName() << ":"; 1181 else 1182 OS << "." << D->getFieldName()->getName(); 1183 } else { 1184 OS << "["; 1185 if (D->isArrayDesignator()) { 1186 PrintExpr(Node->getArrayIndex(*D)); 1187 } else { 1188 PrintExpr(Node->getArrayRangeStart(*D)); 1189 OS << " ... "; 1190 PrintExpr(Node->getArrayRangeEnd(*D)); 1191 } 1192 OS << "]"; 1193 } 1194 } 1195 1196 OS << " = "; 1197 PrintExpr(Node->getInit()); 1198 } 1199 1200 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1201 if (Policy.LangOpts.CPlusPlus) { 1202 OS << "/*implicit*/"; 1203 Node->getType().print(OS, Policy); 1204 OS << "()"; 1205 } else { 1206 OS << "/*implicit*/("; 1207 Node->getType().print(OS, Policy); 1208 OS << ')'; 1209 if (Node->getType()->isRecordType()) 1210 OS << "{}"; 1211 else 1212 OS << 0; 1213 } 1214 } 1215 1216 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1217 OS << "__builtin_va_arg("; 1218 PrintExpr(Node->getSubExpr()); 1219 OS << ", "; 1220 Node->getType().print(OS, Policy); 1221 OS << ")"; 1222 } 1223 1224 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1225 PrintExpr(Node->getSyntacticForm()); 1226 } 1227 1228 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1229 const char *Name = 0; 1230 switch (Node->getOp()) { 1231 #define BUILTIN(ID, TYPE, ATTRS) 1232 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1233 case AtomicExpr::AO ## ID: \ 1234 Name = #ID "("; \ 1235 break; 1236 #include "clang/Basic/Builtins.def" 1237 } 1238 OS << Name; 1239 1240 // AtomicExpr stores its subexpressions in a permuted order. 1241 PrintExpr(Node->getPtr()); 1242 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1243 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1244 OS << ", "; 1245 PrintExpr(Node->getVal1()); 1246 } 1247 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1248 Node->isCmpXChg()) { 1249 OS << ", "; 1250 PrintExpr(Node->getVal2()); 1251 } 1252 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1253 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1254 OS << ", "; 1255 PrintExpr(Node->getWeak()); 1256 } 1257 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1258 OS << ", "; 1259 PrintExpr(Node->getOrder()); 1260 } 1261 if (Node->isCmpXChg()) { 1262 OS << ", "; 1263 PrintExpr(Node->getOrderFail()); 1264 } 1265 OS << ")"; 1266 } 1267 1268 // C++ 1269 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1270 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1271 "", 1272 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1273 Spelling, 1274 #include "clang/Basic/OperatorKinds.def" 1275 }; 1276 1277 OverloadedOperatorKind Kind = Node->getOperator(); 1278 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1279 if (Node->getNumArgs() == 1) { 1280 OS << OpStrings[Kind] << ' '; 1281 PrintExpr(Node->getArg(0)); 1282 } else { 1283 PrintExpr(Node->getArg(0)); 1284 OS << ' ' << OpStrings[Kind]; 1285 } 1286 } else if (Kind == OO_Arrow) { 1287 PrintExpr(Node->getArg(0)); 1288 } else if (Kind == OO_Call) { 1289 PrintExpr(Node->getArg(0)); 1290 OS << '('; 1291 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1292 if (ArgIdx > 1) 1293 OS << ", "; 1294 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1295 PrintExpr(Node->getArg(ArgIdx)); 1296 } 1297 OS << ')'; 1298 } else if (Kind == OO_Subscript) { 1299 PrintExpr(Node->getArg(0)); 1300 OS << '['; 1301 PrintExpr(Node->getArg(1)); 1302 OS << ']'; 1303 } else if (Node->getNumArgs() == 1) { 1304 OS << OpStrings[Kind] << ' '; 1305 PrintExpr(Node->getArg(0)); 1306 } else if (Node->getNumArgs() == 2) { 1307 PrintExpr(Node->getArg(0)); 1308 OS << ' ' << OpStrings[Kind] << ' '; 1309 PrintExpr(Node->getArg(1)); 1310 } else { 1311 llvm_unreachable("unknown overloaded operator"); 1312 } 1313 } 1314 1315 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1316 // If we have a conversion operator call only print the argument. 1317 CXXMethodDecl *MD = Node->getMethodDecl(); 1318 if (MD && isa<CXXConversionDecl>(MD)) { 1319 PrintExpr(Node->getImplicitObjectArgument()); 1320 return; 1321 } 1322 VisitCallExpr(cast<CallExpr>(Node)); 1323 } 1324 1325 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1326 PrintExpr(Node->getCallee()); 1327 OS << "<<<"; 1328 PrintCallArgs(Node->getConfig()); 1329 OS << ">>>("; 1330 PrintCallArgs(Node); 1331 OS << ")"; 1332 } 1333 1334 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1335 OS << Node->getCastName() << '<'; 1336 Node->getTypeAsWritten().print(OS, Policy); 1337 OS << ">("; 1338 PrintExpr(Node->getSubExpr()); 1339 OS << ")"; 1340 } 1341 1342 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1343 VisitCXXNamedCastExpr(Node); 1344 } 1345 1346 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1347 VisitCXXNamedCastExpr(Node); 1348 } 1349 1350 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1351 VisitCXXNamedCastExpr(Node); 1352 } 1353 1354 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1355 VisitCXXNamedCastExpr(Node); 1356 } 1357 1358 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1359 OS << "typeid("; 1360 if (Node->isTypeOperand()) { 1361 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1362 } else { 1363 PrintExpr(Node->getExprOperand()); 1364 } 1365 OS << ")"; 1366 } 1367 1368 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1369 OS << "__uuidof("; 1370 if (Node->isTypeOperand()) { 1371 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1372 } else { 1373 PrintExpr(Node->getExprOperand()); 1374 } 1375 OS << ")"; 1376 } 1377 1378 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1379 PrintExpr(Node->getBaseExpr()); 1380 if (Node->isArrow()) 1381 OS << "->"; 1382 else 1383 OS << "."; 1384 if (NestedNameSpecifier *Qualifier = 1385 Node->getQualifierLoc().getNestedNameSpecifier()) 1386 Qualifier->print(OS, Policy); 1387 OS << Node->getPropertyDecl()->getDeclName(); 1388 } 1389 1390 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1391 switch (Node->getLiteralOperatorKind()) { 1392 case UserDefinedLiteral::LOK_Raw: 1393 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1394 break; 1395 case UserDefinedLiteral::LOK_Template: { 1396 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1397 const TemplateArgumentList *Args = 1398 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1399 assert(Args); 1400 const TemplateArgument &Pack = Args->get(0); 1401 for (TemplateArgument::pack_iterator I = Pack.pack_begin(), 1402 E = Pack.pack_end(); I != E; ++I) { 1403 char C = (char)I->getAsIntegral().getZExtValue(); 1404 OS << C; 1405 } 1406 break; 1407 } 1408 case UserDefinedLiteral::LOK_Integer: { 1409 // Print integer literal without suffix. 1410 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1411 OS << Int->getValue().toString(10, /*isSigned*/false); 1412 break; 1413 } 1414 case UserDefinedLiteral::LOK_Floating: { 1415 // Print floating literal without suffix. 1416 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1417 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1418 break; 1419 } 1420 case UserDefinedLiteral::LOK_String: 1421 case UserDefinedLiteral::LOK_Character: 1422 PrintExpr(Node->getCookedLiteral()); 1423 break; 1424 } 1425 OS << Node->getUDSuffix()->getName(); 1426 } 1427 1428 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1429 OS << (Node->getValue() ? "true" : "false"); 1430 } 1431 1432 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1433 OS << "nullptr"; 1434 } 1435 1436 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1437 OS << "this"; 1438 } 1439 1440 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1441 if (Node->getSubExpr() == 0) 1442 OS << "throw"; 1443 else { 1444 OS << "throw "; 1445 PrintExpr(Node->getSubExpr()); 1446 } 1447 } 1448 1449 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1450 // Nothing to print: we picked up the default argument. 1451 } 1452 1453 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1454 // Nothing to print: we picked up the default initializer. 1455 } 1456 1457 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1458 Node->getType().print(OS, Policy); 1459 OS << "("; 1460 PrintExpr(Node->getSubExpr()); 1461 OS << ")"; 1462 } 1463 1464 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1465 PrintExpr(Node->getSubExpr()); 1466 } 1467 1468 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1469 Node->getType().print(OS, Policy); 1470 OS << "("; 1471 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1472 ArgEnd = Node->arg_end(); 1473 Arg != ArgEnd; ++Arg) { 1474 if (Arg->isDefaultArgument()) 1475 break; 1476 if (Arg != Node->arg_begin()) 1477 OS << ", "; 1478 PrintExpr(*Arg); 1479 } 1480 OS << ")"; 1481 } 1482 1483 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1484 OS << '['; 1485 bool NeedComma = false; 1486 switch (Node->getCaptureDefault()) { 1487 case LCD_None: 1488 break; 1489 1490 case LCD_ByCopy: 1491 OS << '='; 1492 NeedComma = true; 1493 break; 1494 1495 case LCD_ByRef: 1496 OS << '&'; 1497 NeedComma = true; 1498 break; 1499 } 1500 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1501 CEnd = Node->explicit_capture_end(); 1502 C != CEnd; 1503 ++C) { 1504 if (NeedComma) 1505 OS << ", "; 1506 NeedComma = true; 1507 1508 switch (C->getCaptureKind()) { 1509 case LCK_This: 1510 OS << "this"; 1511 break; 1512 1513 case LCK_ByRef: 1514 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture()) 1515 OS << '&'; 1516 OS << C->getCapturedVar()->getName(); 1517 break; 1518 1519 case LCK_ByCopy: 1520 OS << C->getCapturedVar()->getName(); 1521 break; 1522 } 1523 1524 if (C->isInitCapture()) 1525 PrintExpr(C->getCapturedVar()->getInit()); 1526 } 1527 OS << ']'; 1528 1529 if (Node->hasExplicitParameters()) { 1530 OS << " ("; 1531 CXXMethodDecl *Method = Node->getCallOperator(); 1532 NeedComma = false; 1533 for (auto P : Method->params()) { 1534 if (NeedComma) { 1535 OS << ", "; 1536 } else { 1537 NeedComma = true; 1538 } 1539 std::string ParamStr = P->getNameAsString(); 1540 P->getOriginalType().print(OS, Policy, ParamStr); 1541 } 1542 if (Method->isVariadic()) { 1543 if (NeedComma) 1544 OS << ", "; 1545 OS << "..."; 1546 } 1547 OS << ')'; 1548 1549 if (Node->isMutable()) 1550 OS << " mutable"; 1551 1552 const FunctionProtoType *Proto 1553 = Method->getType()->getAs<FunctionProtoType>(); 1554 Proto->printExceptionSpecification(OS, Policy); 1555 1556 // FIXME: Attributes 1557 1558 // Print the trailing return type if it was specified in the source. 1559 if (Node->hasExplicitResultType()) { 1560 OS << " -> "; 1561 Proto->getReturnType().print(OS, Policy); 1562 } 1563 } 1564 1565 // Print the body. 1566 CompoundStmt *Body = Node->getBody(); 1567 OS << ' '; 1568 PrintStmt(Body); 1569 } 1570 1571 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1572 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1573 TSInfo->getType().print(OS, Policy); 1574 else 1575 Node->getType().print(OS, Policy); 1576 OS << "()"; 1577 } 1578 1579 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1580 if (E->isGlobalNew()) 1581 OS << "::"; 1582 OS << "new "; 1583 unsigned NumPlace = E->getNumPlacementArgs(); 1584 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1585 OS << "("; 1586 PrintExpr(E->getPlacementArg(0)); 1587 for (unsigned i = 1; i < NumPlace; ++i) { 1588 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1589 break; 1590 OS << ", "; 1591 PrintExpr(E->getPlacementArg(i)); 1592 } 1593 OS << ") "; 1594 } 1595 if (E->isParenTypeId()) 1596 OS << "("; 1597 std::string TypeS; 1598 if (Expr *Size = E->getArraySize()) { 1599 llvm::raw_string_ostream s(TypeS); 1600 s << '['; 1601 Size->printPretty(s, Helper, Policy); 1602 s << ']'; 1603 } 1604 E->getAllocatedType().print(OS, Policy, TypeS); 1605 if (E->isParenTypeId()) 1606 OS << ")"; 1607 1608 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1609 if (InitStyle) { 1610 if (InitStyle == CXXNewExpr::CallInit) 1611 OS << "("; 1612 PrintExpr(E->getInitializer()); 1613 if (InitStyle == CXXNewExpr::CallInit) 1614 OS << ")"; 1615 } 1616 } 1617 1618 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1619 if (E->isGlobalDelete()) 1620 OS << "::"; 1621 OS << "delete "; 1622 if (E->isArrayForm()) 1623 OS << "[] "; 1624 PrintExpr(E->getArgument()); 1625 } 1626 1627 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1628 PrintExpr(E->getBase()); 1629 if (E->isArrow()) 1630 OS << "->"; 1631 else 1632 OS << '.'; 1633 if (E->getQualifier()) 1634 E->getQualifier()->print(OS, Policy); 1635 OS << "~"; 1636 1637 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1638 OS << II->getName(); 1639 else 1640 E->getDestroyedType().print(OS, Policy); 1641 } 1642 1643 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1644 if (E->isListInitialization()) 1645 OS << "{ "; 1646 1647 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1648 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1649 // Don't print any defaulted arguments 1650 break; 1651 } 1652 1653 if (i) OS << ", "; 1654 PrintExpr(E->getArg(i)); 1655 } 1656 1657 if (E->isListInitialization()) 1658 OS << " }"; 1659 } 1660 1661 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 1662 PrintExpr(E->getSubExpr()); 1663 } 1664 1665 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1666 // Just forward to the subexpression. 1667 PrintExpr(E->getSubExpr()); 1668 } 1669 1670 void 1671 StmtPrinter::VisitCXXUnresolvedConstructExpr( 1672 CXXUnresolvedConstructExpr *Node) { 1673 Node->getTypeAsWritten().print(OS, Policy); 1674 OS << "("; 1675 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1676 ArgEnd = Node->arg_end(); 1677 Arg != ArgEnd; ++Arg) { 1678 if (Arg != Node->arg_begin()) 1679 OS << ", "; 1680 PrintExpr(*Arg); 1681 } 1682 OS << ")"; 1683 } 1684 1685 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1686 CXXDependentScopeMemberExpr *Node) { 1687 if (!Node->isImplicitAccess()) { 1688 PrintExpr(Node->getBase()); 1689 OS << (Node->isArrow() ? "->" : "."); 1690 } 1691 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1692 Qualifier->print(OS, Policy); 1693 if (Node->hasTemplateKeyword()) 1694 OS << "template "; 1695 OS << Node->getMemberNameInfo(); 1696 if (Node->hasExplicitTemplateArgs()) 1697 TemplateSpecializationType::PrintTemplateArgumentList( 1698 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1699 } 1700 1701 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1702 if (!Node->isImplicitAccess()) { 1703 PrintExpr(Node->getBase()); 1704 OS << (Node->isArrow() ? "->" : "."); 1705 } 1706 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1707 Qualifier->print(OS, Policy); 1708 if (Node->hasTemplateKeyword()) 1709 OS << "template "; 1710 OS << Node->getMemberNameInfo(); 1711 if (Node->hasExplicitTemplateArgs()) 1712 TemplateSpecializationType::PrintTemplateArgumentList( 1713 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1714 } 1715 1716 static const char *getTypeTraitName(TypeTrait TT) { 1717 switch (TT) { 1718 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 1719 case clang::UTT_##Name: return #Spelling; 1720 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 1721 case clang::BTT_##Name: return #Spelling; 1722 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 1723 case clang::TT_##Name: return #Spelling; 1724 #include "clang/Basic/TokenKinds.def" 1725 } 1726 llvm_unreachable("Type trait not covered by switch"); 1727 } 1728 1729 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1730 switch (ATT) { 1731 case ATT_ArrayRank: return "__array_rank"; 1732 case ATT_ArrayExtent: return "__array_extent"; 1733 } 1734 llvm_unreachable("Array type trait not covered by switch"); 1735 } 1736 1737 static const char *getExpressionTraitName(ExpressionTrait ET) { 1738 switch (ET) { 1739 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1740 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1741 } 1742 llvm_unreachable("Expression type trait not covered by switch"); 1743 } 1744 1745 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 1746 OS << getTypeTraitName(E->getTrait()) << "("; 1747 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 1748 if (I > 0) 1749 OS << ", "; 1750 E->getArg(I)->getType().print(OS, Policy); 1751 } 1752 OS << ")"; 1753 } 1754 1755 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1756 OS << getTypeTraitName(E->getTrait()) << '('; 1757 E->getQueriedType().print(OS, Policy); 1758 OS << ')'; 1759 } 1760 1761 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1762 OS << getExpressionTraitName(E->getTrait()) << '('; 1763 PrintExpr(E->getQueriedExpression()); 1764 OS << ')'; 1765 } 1766 1767 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1768 OS << "noexcept("; 1769 PrintExpr(E->getOperand()); 1770 OS << ")"; 1771 } 1772 1773 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1774 PrintExpr(E->getPattern()); 1775 OS << "..."; 1776 } 1777 1778 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1779 OS << "sizeof...(" << *E->getPack() << ")"; 1780 } 1781 1782 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1783 SubstNonTypeTemplateParmPackExpr *Node) { 1784 OS << *Node->getParameterPack(); 1785 } 1786 1787 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1788 SubstNonTypeTemplateParmExpr *Node) { 1789 Visit(Node->getReplacement()); 1790 } 1791 1792 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 1793 OS << *E->getParameterPack(); 1794 } 1795 1796 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1797 PrintExpr(Node->GetTemporaryExpr()); 1798 } 1799 1800 // Obj-C 1801 1802 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1803 OS << "@"; 1804 VisitStringLiteral(Node->getString()); 1805 } 1806 1807 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 1808 OS << "@"; 1809 Visit(E->getSubExpr()); 1810 } 1811 1812 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 1813 OS << "@[ "; 1814 StmtRange ch = E->children(); 1815 if (ch.first != ch.second) { 1816 while (1) { 1817 Visit(*ch.first); 1818 ++ch.first; 1819 if (ch.first == ch.second) break; 1820 OS << ", "; 1821 } 1822 } 1823 OS << " ]"; 1824 } 1825 1826 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 1827 OS << "@{ "; 1828 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 1829 if (I > 0) 1830 OS << ", "; 1831 1832 ObjCDictionaryElement Element = E->getKeyValueElement(I); 1833 Visit(Element.Key); 1834 OS << " : "; 1835 Visit(Element.Value); 1836 if (Element.isPackExpansion()) 1837 OS << "..."; 1838 } 1839 OS << " }"; 1840 } 1841 1842 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1843 OS << "@encode("; 1844 Node->getEncodedType().print(OS, Policy); 1845 OS << ')'; 1846 } 1847 1848 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1849 OS << "@selector("; 1850 Node->getSelector().print(OS); 1851 OS << ')'; 1852 } 1853 1854 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1855 OS << "@protocol(" << *Node->getProtocol() << ')'; 1856 } 1857 1858 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1859 OS << "["; 1860 switch (Mess->getReceiverKind()) { 1861 case ObjCMessageExpr::Instance: 1862 PrintExpr(Mess->getInstanceReceiver()); 1863 break; 1864 1865 case ObjCMessageExpr::Class: 1866 Mess->getClassReceiver().print(OS, Policy); 1867 break; 1868 1869 case ObjCMessageExpr::SuperInstance: 1870 case ObjCMessageExpr::SuperClass: 1871 OS << "Super"; 1872 break; 1873 } 1874 1875 OS << ' '; 1876 Selector selector = Mess->getSelector(); 1877 if (selector.isUnarySelector()) { 1878 OS << selector.getNameForSlot(0); 1879 } else { 1880 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1881 if (i < selector.getNumArgs()) { 1882 if (i > 0) OS << ' '; 1883 if (selector.getIdentifierInfoForSlot(i)) 1884 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1885 else 1886 OS << ":"; 1887 } 1888 else OS << ", "; // Handle variadic methods. 1889 1890 PrintExpr(Mess->getArg(i)); 1891 } 1892 } 1893 OS << "]"; 1894 } 1895 1896 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 1897 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1898 } 1899 1900 void 1901 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 1902 PrintExpr(E->getSubExpr()); 1903 } 1904 1905 void 1906 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 1907 OS << '(' << E->getBridgeKindName(); 1908 E->getType().print(OS, Policy); 1909 OS << ')'; 1910 PrintExpr(E->getSubExpr()); 1911 } 1912 1913 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1914 BlockDecl *BD = Node->getBlockDecl(); 1915 OS << "^"; 1916 1917 const FunctionType *AFT = Node->getFunctionType(); 1918 1919 if (isa<FunctionNoProtoType>(AFT)) { 1920 OS << "()"; 1921 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1922 OS << '('; 1923 for (BlockDecl::param_iterator AI = BD->param_begin(), 1924 E = BD->param_end(); AI != E; ++AI) { 1925 if (AI != BD->param_begin()) OS << ", "; 1926 std::string ParamStr = (*AI)->getNameAsString(); 1927 (*AI)->getType().print(OS, Policy, ParamStr); 1928 } 1929 1930 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1931 if (FT->isVariadic()) { 1932 if (!BD->param_empty()) OS << ", "; 1933 OS << "..."; 1934 } 1935 OS << ')'; 1936 } 1937 OS << "{ }"; 1938 } 1939 1940 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 1941 PrintExpr(Node->getSourceExpr()); 1942 } 1943 1944 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 1945 OS << "__builtin_astype("; 1946 PrintExpr(Node->getSrcExpr()); 1947 OS << ", "; 1948 Node->getType().print(OS, Policy); 1949 OS << ")"; 1950 } 1951 1952 //===----------------------------------------------------------------------===// 1953 // Stmt method implementations 1954 //===----------------------------------------------------------------------===// 1955 1956 void Stmt::dumpPretty(const ASTContext &Context) const { 1957 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts())); 1958 } 1959 1960 void Stmt::printPretty(raw_ostream &OS, 1961 PrinterHelper *Helper, 1962 const PrintingPolicy &Policy, 1963 unsigned Indentation) const { 1964 if (this == 0) { 1965 OS << "<NULL>"; 1966 return; 1967 } 1968 1969 StmtPrinter P(OS, Helper, Policy, Indentation); 1970 P.Visit(const_cast<Stmt*>(this)); 1971 } 1972 1973 //===----------------------------------------------------------------------===// 1974 // PrinterHelper 1975 //===----------------------------------------------------------------------===// 1976 1977 // Implement virtual destructor. 1978 PrinterHelper::~PrinterHelper() {} 1979