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