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