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