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