StmtPrinter.cpp revision d0fb3adeed631385a53d15ced60d67c5f64eb133
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/StmtVisitor.h" 16#include "clang/AST/DeclCXX.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/DeclTemplate.h" 19#include "clang/AST/PrettyPrinter.h" 20#include "llvm/Support/Format.h" 21#include "clang/AST/Expr.h" 22#include "clang/AST/ExprCXX.h" 23using namespace clang; 24 25//===----------------------------------------------------------------------===// 26// StmtPrinter Visitor 27//===----------------------------------------------------------------------===// 28 29namespace { 30 class StmtPrinter : public StmtVisitor<StmtPrinter> { 31 llvm::raw_ostream &OS; 32 ASTContext &Context; 33 unsigned IndentLevel; 34 clang::PrinterHelper* Helper; 35 PrintingPolicy Policy; 36 37 public: 38 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper, 39 const PrintingPolicy &Policy, 40 unsigned Indentation = 0) 41 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper), 42 Policy(Policy) {} 43 44 void PrintStmt(Stmt *S) { 45 PrintStmt(S, Policy.Indentation); 46 } 47 48 void PrintStmt(Stmt *S, int SubIndent) { 49 IndentLevel += SubIndent; 50 if (S && isa<Expr>(S)) { 51 // If this is an expr used in a stmt context, indent and newline it. 52 Indent(); 53 Visit(S); 54 OS << ";\n"; 55 } else if (S) { 56 Visit(S); 57 } else { 58 Indent() << "<<<NULL STATEMENT>>>\n"; 59 } 60 IndentLevel -= SubIndent; 61 } 62 63 void PrintRawCompoundStmt(CompoundStmt *S); 64 void PrintRawDecl(Decl *D); 65 void PrintRawDeclStmt(DeclStmt *S); 66 void PrintRawIfStmt(IfStmt *If); 67 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); 68 69 void PrintExpr(Expr *E) { 70 if (E) 71 Visit(E); 72 else 73 OS << "<null expr>"; 74 } 75 76 llvm::raw_ostream &Indent(int Delta = 0) { 77 for (int i = 0, e = IndentLevel+Delta; i < e; ++i) 78 OS << " "; 79 return OS; 80 } 81 82 void Visit(Stmt* S) { 83 if (Helper && Helper->handledStmt(S,OS)) 84 return; 85 else StmtVisitor<StmtPrinter>::Visit(S); 86 } 87 88 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED { 89 Indent() << "<<unknown stmt type>>\n"; 90 } 91 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED { 92 OS << "<<unknown expr type>>"; 93 } 94 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); 95 96#define ABSTRACT_STMT(CLASS) 97#define STMT(CLASS, PARENT) \ 98 void Visit##CLASS(CLASS *Node); 99#include "clang/AST/StmtNodes.inc" 100 }; 101} 102 103//===----------------------------------------------------------------------===// 104// Stmt printing methods. 105//===----------------------------------------------------------------------===// 106 107/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and 108/// with no newline after the }. 109void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { 110 OS << "{\n"; 111 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); 112 I != E; ++I) 113 PrintStmt(*I); 114 115 Indent() << "}"; 116} 117 118void StmtPrinter::PrintRawDecl(Decl *D) { 119 D->print(OS, Policy, IndentLevel); 120} 121 122void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) { 123 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end(); 124 llvm::SmallVector<Decl*, 2> Decls; 125 for ( ; Begin != End; ++Begin) 126 Decls.push_back(*Begin); 127 128 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); 129} 130 131void StmtPrinter::VisitNullStmt(NullStmt *Node) { 132 Indent() << ";\n"; 133} 134 135void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { 136 Indent(); 137 PrintRawDeclStmt(Node); 138 OS << ";\n"; 139} 140 141void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { 142 Indent(); 143 PrintRawCompoundStmt(Node); 144 OS << "\n"; 145} 146 147void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { 148 Indent(-1) << "case "; 149 PrintExpr(Node->getLHS()); 150 if (Node->getRHS()) { 151 OS << " ... "; 152 PrintExpr(Node->getRHS()); 153 } 154 OS << ":\n"; 155 156 PrintStmt(Node->getSubStmt(), 0); 157} 158 159void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { 160 Indent(-1) << "default:\n"; 161 PrintStmt(Node->getSubStmt(), 0); 162} 163 164void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { 165 Indent(-1) << Node->getName() << ":\n"; 166 PrintStmt(Node->getSubStmt(), 0); 167} 168 169void StmtPrinter::PrintRawIfStmt(IfStmt *If) { 170 OS << "if ("; 171 PrintExpr(If->getCond()); 172 OS << ')'; 173 174 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { 175 OS << ' '; 176 PrintRawCompoundStmt(CS); 177 OS << (If->getElse() ? ' ' : '\n'); 178 } else { 179 OS << '\n'; 180 PrintStmt(If->getThen()); 181 if (If->getElse()) Indent(); 182 } 183 184 if (Stmt *Else = If->getElse()) { 185 OS << "else"; 186 187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { 188 OS << ' '; 189 PrintRawCompoundStmt(CS); 190 OS << '\n'; 191 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { 192 OS << ' '; 193 PrintRawIfStmt(ElseIf); 194 } else { 195 OS << '\n'; 196 PrintStmt(If->getElse()); 197 } 198 } 199} 200 201void StmtPrinter::VisitIfStmt(IfStmt *If) { 202 Indent(); 203 PrintRawIfStmt(If); 204} 205 206void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { 207 Indent() << "switch ("; 208 PrintExpr(Node->getCond()); 209 OS << ")"; 210 211 // Pretty print compoundstmt bodies (very common). 212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 213 OS << " "; 214 PrintRawCompoundStmt(CS); 215 OS << "\n"; 216 } else { 217 OS << "\n"; 218 PrintStmt(Node->getBody()); 219 } 220} 221 222void StmtPrinter::VisitSwitchCase(SwitchCase*) { 223 assert(0 && "SwitchCase is an abstract class"); 224} 225 226void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { 227 Indent() << "while ("; 228 PrintExpr(Node->getCond()); 229 OS << ")\n"; 230 PrintStmt(Node->getBody()); 231} 232 233void StmtPrinter::VisitDoStmt(DoStmt *Node) { 234 Indent() << "do "; 235 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 236 PrintRawCompoundStmt(CS); 237 OS << " "; 238 } else { 239 OS << "\n"; 240 PrintStmt(Node->getBody()); 241 Indent(); 242 } 243 244 OS << "while ("; 245 PrintExpr(Node->getCond()); 246 OS << ");\n"; 247} 248 249void StmtPrinter::VisitForStmt(ForStmt *Node) { 250 Indent() << "for ("; 251 if (Node->getInit()) { 252 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) 253 PrintRawDeclStmt(DS); 254 else 255 PrintExpr(cast<Expr>(Node->getInit())); 256 } 257 OS << ";"; 258 if (Node->getCond()) { 259 OS << " "; 260 PrintExpr(Node->getCond()); 261 } 262 OS << ";"; 263 if (Node->getInc()) { 264 OS << " "; 265 PrintExpr(Node->getInc()); 266 } 267 OS << ") "; 268 269 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 270 PrintRawCompoundStmt(CS); 271 OS << "\n"; 272 } else { 273 OS << "\n"; 274 PrintStmt(Node->getBody()); 275 } 276} 277 278void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { 279 Indent() << "for ("; 280 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) 281 PrintRawDeclStmt(DS); 282 else 283 PrintExpr(cast<Expr>(Node->getElement())); 284 OS << " in "; 285 PrintExpr(Node->getCollection()); 286 OS << ") "; 287 288 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 289 PrintRawCompoundStmt(CS); 290 OS << "\n"; 291 } else { 292 OS << "\n"; 293 PrintStmt(Node->getBody()); 294 } 295} 296 297void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { 298 Indent() << "goto " << Node->getLabel()->getName() << ";\n"; 299} 300 301void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 302 Indent() << "goto *"; 303 PrintExpr(Node->getTarget()); 304 OS << ";\n"; 305} 306 307void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 308 Indent() << "continue;\n"; 309} 310 311void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 312 Indent() << "break;\n"; 313} 314 315 316void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 317 Indent() << "return"; 318 if (Node->getRetValue()) { 319 OS << " "; 320 PrintExpr(Node->getRetValue()); 321 } 322 OS << ";\n"; 323} 324 325 326void StmtPrinter::VisitAsmStmt(AsmStmt *Node) { 327 Indent() << "asm "; 328 329 if (Node->isVolatile()) 330 OS << "volatile "; 331 332 OS << "("; 333 VisitStringLiteral(Node->getAsmString()); 334 335 // Outputs 336 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 337 Node->getNumClobbers() != 0) 338 OS << " : "; 339 340 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 341 if (i != 0) 342 OS << ", "; 343 344 if (!Node->getOutputName(i).empty()) { 345 OS << '['; 346 OS << Node->getOutputName(i); 347 OS << "] "; 348 } 349 350 VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 351 OS << " "; 352 Visit(Node->getOutputExpr(i)); 353 } 354 355 // Inputs 356 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 357 OS << " : "; 358 359 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 360 if (i != 0) 361 OS << ", "; 362 363 if (!Node->getInputName(i).empty()) { 364 OS << '['; 365 OS << Node->getInputName(i); 366 OS << "] "; 367 } 368 369 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 370 OS << " "; 371 Visit(Node->getInputExpr(i)); 372 } 373 374 // Clobbers 375 if (Node->getNumClobbers() != 0) 376 OS << " : "; 377 378 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 379 if (i != 0) 380 OS << ", "; 381 382 VisitStringLiteral(Node->getClobber(i)); 383 } 384 385 OS << ");\n"; 386} 387 388void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 389 Indent() << "@try"; 390 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 391 PrintRawCompoundStmt(TS); 392 OS << "\n"; 393 } 394 395 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 396 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 397 Indent() << "@catch("; 398 if (catchStmt->getCatchParamDecl()) { 399 if (Decl *DS = catchStmt->getCatchParamDecl()) 400 PrintRawDecl(DS); 401 } 402 OS << ")"; 403 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 404 PrintRawCompoundStmt(CS); 405 OS << "\n"; 406 } 407 } 408 409 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 410 Node->getFinallyStmt())) { 411 Indent() << "@finally"; 412 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 413 OS << "\n"; 414 } 415} 416 417void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 418} 419 420void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 421 Indent() << "@catch (...) { /* todo */ } \n"; 422} 423 424void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 425 Indent() << "@throw"; 426 if (Node->getThrowExpr()) { 427 OS << " "; 428 PrintExpr(Node->getThrowExpr()); 429 } 430 OS << ";\n"; 431} 432 433void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 434 Indent() << "@synchronized ("; 435 PrintExpr(Node->getSynchExpr()); 436 OS << ")"; 437 PrintRawCompoundStmt(Node->getSynchBody()); 438 OS << "\n"; 439} 440 441void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 442 OS << "catch ("; 443 if (Decl *ExDecl = Node->getExceptionDecl()) 444 PrintRawDecl(ExDecl); 445 else 446 OS << "..."; 447 OS << ") "; 448 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 449} 450 451void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 452 Indent(); 453 PrintRawCXXCatchStmt(Node); 454 OS << "\n"; 455} 456 457void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 458 Indent() << "try "; 459 PrintRawCompoundStmt(Node->getTryBlock()); 460 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 461 OS << " "; 462 PrintRawCXXCatchStmt(Node->getHandler(i)); 463 } 464 OS << "\n"; 465} 466 467//===----------------------------------------------------------------------===// 468// Expr printing methods. 469//===----------------------------------------------------------------------===// 470 471void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 472 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 473 Qualifier->print(OS, Policy); 474 OS << Node->getNameInfo(); 475 if (Node->hasExplicitTemplateArgs()) 476 OS << TemplateSpecializationType::PrintTemplateArgumentList( 477 Node->getTemplateArgs(), 478 Node->getNumTemplateArgs(), 479 Policy); 480} 481 482void StmtPrinter::VisitDependentScopeDeclRefExpr( 483 DependentScopeDeclRefExpr *Node) { 484 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 485 Qualifier->print(OS, Policy); 486 OS << Node->getNameInfo(); 487 if (Node->hasExplicitTemplateArgs()) 488 OS << TemplateSpecializationType::PrintTemplateArgumentList( 489 Node->getTemplateArgs(), 490 Node->getNumTemplateArgs(), 491 Policy); 492} 493 494void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 495 if (Node->getQualifier()) 496 Node->getQualifier()->print(OS, Policy); 497 OS << Node->getNameInfo(); 498 if (Node->hasExplicitTemplateArgs()) 499 OS << TemplateSpecializationType::PrintTemplateArgumentList( 500 Node->getTemplateArgs(), 501 Node->getNumTemplateArgs(), 502 Policy); 503} 504 505void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 506 if (Node->getBase()) { 507 PrintExpr(Node->getBase()); 508 OS << (Node->isArrow() ? "->" : "."); 509 } 510 OS << Node->getDecl(); 511} 512 513void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 514 if (Node->isSuperReceiver()) 515 OS << "super."; 516 else if (Node->getBase()) { 517 PrintExpr(Node->getBase()); 518 OS << "."; 519 } 520 521 if (Node->isImplicitProperty()) 522 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString(); 523 else 524 OS << Node->getExplicitProperty()->getName(); 525} 526 527void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 528 switch (Node->getIdentType()) { 529 default: 530 assert(0 && "unknown case"); 531 case PredefinedExpr::Func: 532 OS << "__func__"; 533 break; 534 case PredefinedExpr::Function: 535 OS << "__FUNCTION__"; 536 break; 537 case PredefinedExpr::PrettyFunction: 538 OS << "__PRETTY_FUNCTION__"; 539 break; 540 } 541} 542 543void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 544 unsigned value = Node->getValue(); 545 if (Node->isWide()) 546 OS << "L"; 547 switch (value) { 548 case '\\': 549 OS << "'\\\\'"; 550 break; 551 case '\'': 552 OS << "'\\''"; 553 break; 554 case '\a': 555 // TODO: K&R: the meaning of '\\a' is different in traditional C 556 OS << "'\\a'"; 557 break; 558 case '\b': 559 OS << "'\\b'"; 560 break; 561 // Nonstandard escape sequence. 562 /*case '\e': 563 OS << "'\\e'"; 564 break;*/ 565 case '\f': 566 OS << "'\\f'"; 567 break; 568 case '\n': 569 OS << "'\\n'"; 570 break; 571 case '\r': 572 OS << "'\\r'"; 573 break; 574 case '\t': 575 OS << "'\\t'"; 576 break; 577 case '\v': 578 OS << "'\\v'"; 579 break; 580 default: 581 if (value < 256 && isprint(value)) { 582 OS << "'" << (char)value << "'"; 583 } else if (value < 256) { 584 OS << "'\\x" << llvm::format("%x", value) << "'"; 585 } else { 586 // FIXME what to really do here? 587 OS << value; 588 } 589 } 590} 591 592void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 593 bool isSigned = Node->getType()->isSignedIntegerType(); 594 OS << Node->getValue().toString(10, isSigned); 595 596 // Emit suffixes. Integer literals are always a builtin integer type. 597 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 598 default: assert(0 && "Unexpected type for integer literal!"); 599 case BuiltinType::Int: break; // no suffix. 600 case BuiltinType::UInt: OS << 'U'; break; 601 case BuiltinType::Long: OS << 'L'; break; 602 case BuiltinType::ULong: OS << "UL"; break; 603 case BuiltinType::LongLong: OS << "LL"; break; 604 case BuiltinType::ULongLong: OS << "ULL"; break; 605 } 606} 607void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 608 // FIXME: print value more precisely. 609 OS << Node->getValueAsApproximateDouble(); 610} 611 612void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 613 PrintExpr(Node->getSubExpr()); 614 OS << "i"; 615} 616 617void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 618 if (Str->isWide()) OS << 'L'; 619 OS << '"'; 620 621 // FIXME: this doesn't print wstrings right. 622 llvm::StringRef StrData = Str->getString(); 623 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end(); 624 I != E; ++I) { 625 unsigned char Char = *I; 626 627 switch (Char) { 628 default: 629 if (isprint(Char)) 630 OS << (char)Char; 631 else // Output anything hard as an octal escape. 632 OS << '\\' 633 << (char)('0'+ ((Char >> 6) & 7)) 634 << (char)('0'+ ((Char >> 3) & 7)) 635 << (char)('0'+ ((Char >> 0) & 7)); 636 break; 637 // Handle some common non-printable cases to make dumps prettier. 638 case '\\': OS << "\\\\"; break; 639 case '"': OS << "\\\""; break; 640 case '\n': OS << "\\n"; break; 641 case '\t': OS << "\\t"; break; 642 case '\a': OS << "\\a"; break; 643 case '\b': OS << "\\b"; break; 644 } 645 } 646 OS << '"'; 647} 648void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 649 OS << "("; 650 PrintExpr(Node->getSubExpr()); 651 OS << ")"; 652} 653void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 654 if (!Node->isPostfix()) { 655 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 656 657 // Print a space if this is an "identifier operator" like __real, or if 658 // it might be concatenated incorrectly like '+'. 659 switch (Node->getOpcode()) { 660 default: break; 661 case UO_Real: 662 case UO_Imag: 663 case UO_Extension: 664 OS << ' '; 665 break; 666 case UO_Plus: 667 case UO_Minus: 668 if (isa<UnaryOperator>(Node->getSubExpr())) 669 OS << ' '; 670 break; 671 } 672 } 673 PrintExpr(Node->getSubExpr()); 674 675 if (Node->isPostfix()) 676 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 677} 678 679void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 680 OS << "__builtin_offsetof("; 681 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", "; 682 bool PrintedSomething = false; 683 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 684 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 685 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 686 // Array node 687 OS << "["; 688 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 689 OS << "]"; 690 PrintedSomething = true; 691 continue; 692 } 693 694 // Skip implicit base indirections. 695 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 696 continue; 697 698 // Field or identifier node. 699 IdentifierInfo *Id = ON.getFieldName(); 700 if (!Id) 701 continue; 702 703 if (PrintedSomething) 704 OS << "."; 705 else 706 PrintedSomething = true; 707 OS << Id->getName(); 708 } 709 OS << ")"; 710} 711 712void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) { 713 OS << (Node->isSizeOf() ? "sizeof" : "__alignof"); 714 if (Node->isArgumentType()) 715 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")"; 716 else { 717 OS << " "; 718 PrintExpr(Node->getArgumentExpr()); 719 } 720} 721void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 722 PrintExpr(Node->getLHS()); 723 OS << "["; 724 PrintExpr(Node->getRHS()); 725 OS << "]"; 726} 727 728void StmtPrinter::VisitCallExpr(CallExpr *Call) { 729 PrintExpr(Call->getCallee()); 730 OS << "("; 731 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 732 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 733 // Don't print any defaulted arguments 734 break; 735 } 736 737 if (i) OS << ", "; 738 PrintExpr(Call->getArg(i)); 739 } 740 OS << ")"; 741} 742void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 743 // FIXME: Suppress printing implicit bases (like "this") 744 PrintExpr(Node->getBase()); 745 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 746 if (FD->isAnonymousStructOrUnion()) 747 return; 748 OS << (Node->isArrow() ? "->" : "."); 749 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 750 Qualifier->print(OS, Policy); 751 752 OS << Node->getMemberNameInfo(); 753 754 if (Node->hasExplicitTemplateArgs()) 755 OS << TemplateSpecializationType::PrintTemplateArgumentList( 756 Node->getTemplateArgs(), 757 Node->getNumTemplateArgs(), 758 Policy); 759} 760void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 761 PrintExpr(Node->getBase()); 762 OS << (Node->isArrow() ? "->isa" : ".isa"); 763} 764 765void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 766 PrintExpr(Node->getBase()); 767 OS << "."; 768 OS << Node->getAccessor().getName(); 769} 770void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 771 OS << "(" << Node->getType().getAsString(Policy) << ")"; 772 PrintExpr(Node->getSubExpr()); 773} 774void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 775 OS << "(" << Node->getType().getAsString(Policy) << ")"; 776 PrintExpr(Node->getInitializer()); 777} 778void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 779 // No need to print anything, simply forward to the sub expression. 780 PrintExpr(Node->getSubExpr()); 781} 782void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 783 PrintExpr(Node->getLHS()); 784 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 785 PrintExpr(Node->getRHS()); 786} 787void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 788 PrintExpr(Node->getLHS()); 789 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 790 PrintExpr(Node->getRHS()); 791} 792void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 793 PrintExpr(Node->getCond()); 794 795 if (Node->getLHS()) { 796 OS << " ? "; 797 PrintExpr(Node->getLHS()); 798 OS << " : "; 799 } 800 else { // Handle GCC extension where LHS can be NULL. 801 OS << " ?: "; 802 } 803 804 PrintExpr(Node->getRHS()); 805} 806 807// GNU extensions. 808 809void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 810 OS << "&&" << Node->getLabel()->getName(); 811} 812 813void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 814 OS << "("; 815 PrintRawCompoundStmt(E->getSubStmt()); 816 OS << ")"; 817} 818 819void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 820 OS << "__builtin_choose_expr("; 821 PrintExpr(Node->getCond()); 822 OS << ", "; 823 PrintExpr(Node->getLHS()); 824 OS << ", "; 825 PrintExpr(Node->getRHS()); 826 OS << ")"; 827} 828 829void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 830 OS << "__null"; 831} 832 833void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 834 OS << "__builtin_shufflevector("; 835 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 836 if (i) OS << ", "; 837 PrintExpr(Node->getExpr(i)); 838 } 839 OS << ")"; 840} 841 842void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 843 if (Node->getSyntacticForm()) { 844 Visit(Node->getSyntacticForm()); 845 return; 846 } 847 848 OS << "{ "; 849 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 850 if (i) OS << ", "; 851 if (Node->getInit(i)) 852 PrintExpr(Node->getInit(i)); 853 else 854 OS << "0"; 855 } 856 OS << " }"; 857} 858 859void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 860 OS << "( "; 861 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 862 if (i) OS << ", "; 863 PrintExpr(Node->getExpr(i)); 864 } 865 OS << " )"; 866} 867 868void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 869 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 870 DEnd = Node->designators_end(); 871 D != DEnd; ++D) { 872 if (D->isFieldDesignator()) { 873 if (D->getDotLoc().isInvalid()) 874 OS << D->getFieldName()->getName() << ":"; 875 else 876 OS << "." << D->getFieldName()->getName(); 877 } else { 878 OS << "["; 879 if (D->isArrayDesignator()) { 880 PrintExpr(Node->getArrayIndex(*D)); 881 } else { 882 PrintExpr(Node->getArrayRangeStart(*D)); 883 OS << " ... "; 884 PrintExpr(Node->getArrayRangeEnd(*D)); 885 } 886 OS << "]"; 887 } 888 } 889 890 OS << " = "; 891 PrintExpr(Node->getInit()); 892} 893 894void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 895 if (Policy.LangOpts.CPlusPlus) 896 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; 897 else { 898 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; 899 if (Node->getType()->isRecordType()) 900 OS << "{}"; 901 else 902 OS << 0; 903 } 904} 905 906void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 907 OS << "__builtin_va_arg("; 908 PrintExpr(Node->getSubExpr()); 909 OS << ", "; 910 OS << Node->getType().getAsString(Policy); 911 OS << ")"; 912} 913 914// C++ 915void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 916 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 917 "", 918#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 919 Spelling, 920#include "clang/Basic/OperatorKinds.def" 921 }; 922 923 OverloadedOperatorKind Kind = Node->getOperator(); 924 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 925 if (Node->getNumArgs() == 1) { 926 OS << OpStrings[Kind] << ' '; 927 PrintExpr(Node->getArg(0)); 928 } else { 929 PrintExpr(Node->getArg(0)); 930 OS << ' ' << OpStrings[Kind]; 931 } 932 } else if (Kind == OO_Call) { 933 PrintExpr(Node->getArg(0)); 934 OS << '('; 935 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 936 if (ArgIdx > 1) 937 OS << ", "; 938 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 939 PrintExpr(Node->getArg(ArgIdx)); 940 } 941 OS << ')'; 942 } else if (Kind == OO_Subscript) { 943 PrintExpr(Node->getArg(0)); 944 OS << '['; 945 PrintExpr(Node->getArg(1)); 946 OS << ']'; 947 } else if (Node->getNumArgs() == 1) { 948 OS << OpStrings[Kind] << ' '; 949 PrintExpr(Node->getArg(0)); 950 } else if (Node->getNumArgs() == 2) { 951 PrintExpr(Node->getArg(0)); 952 OS << ' ' << OpStrings[Kind] << ' '; 953 PrintExpr(Node->getArg(1)); 954 } else { 955 assert(false && "unknown overloaded operator"); 956 } 957} 958 959void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 960 VisitCallExpr(cast<CallExpr>(Node)); 961} 962 963void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 964 OS << Node->getCastName() << '<'; 965 OS << Node->getTypeAsWritten().getAsString(Policy) << ">("; 966 PrintExpr(Node->getSubExpr()); 967 OS << ")"; 968} 969 970void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 971 VisitCXXNamedCastExpr(Node); 972} 973 974void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 975 VisitCXXNamedCastExpr(Node); 976} 977 978void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 979 VisitCXXNamedCastExpr(Node); 980} 981 982void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 983 VisitCXXNamedCastExpr(Node); 984} 985 986void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 987 OS << "typeid("; 988 if (Node->isTypeOperand()) { 989 OS << Node->getTypeOperand().getAsString(Policy); 990 } else { 991 PrintExpr(Node->getExprOperand()); 992 } 993 OS << ")"; 994} 995 996void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 997 OS << "__uuidof("; 998 if (Node->isTypeOperand()) { 999 OS << Node->getTypeOperand().getAsString(Policy); 1000 } else { 1001 PrintExpr(Node->getExprOperand()); 1002 } 1003 OS << ")"; 1004} 1005 1006void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1007 OS << (Node->getValue() ? "true" : "false"); 1008} 1009 1010void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1011 OS << "nullptr"; 1012} 1013 1014void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1015 OS << "this"; 1016} 1017 1018void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1019 if (Node->getSubExpr() == 0) 1020 OS << "throw"; 1021 else { 1022 OS << "throw "; 1023 PrintExpr(Node->getSubExpr()); 1024 } 1025} 1026 1027void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1028 // Nothing to print: we picked up the default argument 1029} 1030 1031void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1032 OS << Node->getType().getAsString(Policy); 1033 OS << "("; 1034 PrintExpr(Node->getSubExpr()); 1035 OS << ")"; 1036} 1037 1038void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1039 PrintExpr(Node->getSubExpr()); 1040} 1041 1042void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1043 OS << Node->getType().getAsString(Policy); 1044 OS << "("; 1045 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1046 ArgEnd = Node->arg_end(); 1047 Arg != ArgEnd; ++Arg) { 1048 if (Arg != Node->arg_begin()) 1049 OS << ", "; 1050 PrintExpr(*Arg); 1051 } 1052 OS << ")"; 1053} 1054 1055void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1056 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1057 OS << TSInfo->getType().getAsString(Policy) << "()"; 1058 else 1059 OS << Node->getType().getAsString(Policy) << "()"; 1060} 1061 1062void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1063 if (E->isGlobalNew()) 1064 OS << "::"; 1065 OS << "new "; 1066 unsigned NumPlace = E->getNumPlacementArgs(); 1067 if (NumPlace > 0) { 1068 OS << "("; 1069 PrintExpr(E->getPlacementArg(0)); 1070 for (unsigned i = 1; i < NumPlace; ++i) { 1071 OS << ", "; 1072 PrintExpr(E->getPlacementArg(i)); 1073 } 1074 OS << ") "; 1075 } 1076 if (E->isParenTypeId()) 1077 OS << "("; 1078 std::string TypeS; 1079 if (Expr *Size = E->getArraySize()) { 1080 llvm::raw_string_ostream s(TypeS); 1081 Size->printPretty(s, Context, Helper, Policy); 1082 s.flush(); 1083 TypeS = "[" + TypeS + "]"; 1084 } 1085 E->getAllocatedType().getAsStringInternal(TypeS, Policy); 1086 OS << TypeS; 1087 if (E->isParenTypeId()) 1088 OS << ")"; 1089 1090 if (E->hasInitializer()) { 1091 OS << "("; 1092 unsigned NumCons = E->getNumConstructorArgs(); 1093 if (NumCons > 0) { 1094 PrintExpr(E->getConstructorArg(0)); 1095 for (unsigned i = 1; i < NumCons; ++i) { 1096 OS << ", "; 1097 PrintExpr(E->getConstructorArg(i)); 1098 } 1099 } 1100 OS << ")"; 1101 } 1102} 1103 1104void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1105 if (E->isGlobalDelete()) 1106 OS << "::"; 1107 OS << "delete "; 1108 if (E->isArrayForm()) 1109 OS << "[] "; 1110 PrintExpr(E->getArgument()); 1111} 1112 1113void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1114 PrintExpr(E->getBase()); 1115 if (E->isArrow()) 1116 OS << "->"; 1117 else 1118 OS << '.'; 1119 if (E->getQualifier()) 1120 E->getQualifier()->print(OS, Policy); 1121 1122 std::string TypeS; 1123 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1124 OS << II->getName(); 1125 else 1126 E->getDestroyedType().getAsStringInternal(TypeS, Policy); 1127 OS << TypeS; 1128} 1129 1130void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1131 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1132 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1133 // Don't print any defaulted arguments 1134 break; 1135 } 1136 1137 if (i) OS << ", "; 1138 PrintExpr(E->getArg(i)); 1139 } 1140} 1141 1142void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1143 // Just forward to the sub expression. 1144 PrintExpr(E->getSubExpr()); 1145} 1146 1147void 1148StmtPrinter::VisitCXXUnresolvedConstructExpr( 1149 CXXUnresolvedConstructExpr *Node) { 1150 OS << Node->getTypeAsWritten().getAsString(Policy); 1151 OS << "("; 1152 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1153 ArgEnd = Node->arg_end(); 1154 Arg != ArgEnd; ++Arg) { 1155 if (Arg != Node->arg_begin()) 1156 OS << ", "; 1157 PrintExpr(*Arg); 1158 } 1159 OS << ")"; 1160} 1161 1162void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1163 CXXDependentScopeMemberExpr *Node) { 1164 if (!Node->isImplicitAccess()) { 1165 PrintExpr(Node->getBase()); 1166 OS << (Node->isArrow() ? "->" : "."); 1167 } 1168 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1169 Qualifier->print(OS, Policy); 1170 else if (Node->hasExplicitTemplateArgs()) 1171 // FIXME: Track use of "template" keyword explicitly? 1172 OS << "template "; 1173 1174 OS << Node->getMemberNameInfo(); 1175 1176 if (Node->hasExplicitTemplateArgs()) { 1177 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1178 Node->getTemplateArgs(), 1179 Node->getNumTemplateArgs(), 1180 Policy); 1181 } 1182} 1183 1184void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1185 if (!Node->isImplicitAccess()) { 1186 PrintExpr(Node->getBase()); 1187 OS << (Node->isArrow() ? "->" : "."); 1188 } 1189 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1190 Qualifier->print(OS, Policy); 1191 1192 // FIXME: this might originally have been written with 'template' 1193 1194 OS << Node->getMemberNameInfo(); 1195 1196 if (Node->hasExplicitTemplateArgs()) { 1197 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1198 Node->getTemplateArgs(), 1199 Node->getNumTemplateArgs(), 1200 Policy); 1201 } 1202} 1203 1204static const char *getTypeTraitName(UnaryTypeTrait UTT) { 1205 switch (UTT) { 1206 default: llvm_unreachable("Unknown unary type trait"); 1207 case UTT_HasNothrowAssign: return "__has_nothrow_assign"; 1208 case UTT_HasNothrowCopy: return "__has_nothrow_copy"; 1209 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; 1210 case UTT_HasTrivialAssign: return "__has_trivial_assign"; 1211 case UTT_HasTrivialCopy: return "__has_trivial_copy"; 1212 case UTT_HasTrivialConstructor: return "__has_trivial_constructor"; 1213 case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; 1214 case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; 1215 case UTT_IsAbstract: return "__is_abstract"; 1216 case UTT_IsClass: return "__is_class"; 1217 case UTT_IsEmpty: return "__is_empty"; 1218 case UTT_IsEnum: return "__is_enum"; 1219 case UTT_IsPOD: return "__is_pod"; 1220 case UTT_IsPolymorphic: return "__is_polymorphic"; 1221 case UTT_IsUnion: return "__is_union"; 1222 } 1223 return ""; 1224} 1225 1226static const char *getTypeTraitName(BinaryTypeTrait BTT) { 1227 switch (BTT) { 1228 default: llvm_unreachable("Unknown binary type trait"); 1229 case BTT_IsBaseOf: return "__is_base_of"; 1230 case BTT_TypeCompatible: return "__builtin_types_compatible_p"; 1231 } 1232 return ""; 1233} 1234 1235void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 1236 OS << getTypeTraitName(E->getTrait()) << "(" 1237 << E->getQueriedType().getAsString(Policy) << ")"; 1238} 1239 1240void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 1241 OS << getTypeTraitName(E->getTrait()) << "(" 1242 << E->getLhsType().getAsString(Policy) << "," 1243 << E->getRhsType().getAsString(Policy) << ")"; 1244} 1245 1246void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1247 OS << "noexcept("; 1248 PrintExpr(E->getOperand()); 1249 OS << ")"; 1250} 1251 1252void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1253 PrintExpr(E->getPattern()); 1254 OS << "..."; 1255} 1256 1257void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1258 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")"; 1259} 1260 1261void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1262 SubstNonTypeTemplateParmPackExpr *Node) { 1263 OS << Node->getParameterPack()->getNameAsString(); 1264} 1265 1266// Obj-C 1267 1268void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1269 OS << "@"; 1270 VisitStringLiteral(Node->getString()); 1271} 1272 1273void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1274 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')'; 1275} 1276 1277void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1278 OS << "@selector(" << Node->getSelector().getAsString() << ')'; 1279} 1280 1281void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1282 OS << "@protocol(" << Node->getProtocol() << ')'; 1283} 1284 1285void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1286 OS << "["; 1287 switch (Mess->getReceiverKind()) { 1288 case ObjCMessageExpr::Instance: 1289 PrintExpr(Mess->getInstanceReceiver()); 1290 break; 1291 1292 case ObjCMessageExpr::Class: 1293 OS << Mess->getClassReceiver().getAsString(Policy); 1294 break; 1295 1296 case ObjCMessageExpr::SuperInstance: 1297 case ObjCMessageExpr::SuperClass: 1298 OS << "Super"; 1299 break; 1300 } 1301 1302 OS << ' '; 1303 Selector selector = Mess->getSelector(); 1304 if (selector.isUnarySelector()) { 1305 OS << selector.getIdentifierInfoForSlot(0)->getName(); 1306 } else { 1307 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1308 if (i < selector.getNumArgs()) { 1309 if (i > 0) OS << ' '; 1310 if (selector.getIdentifierInfoForSlot(i)) 1311 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1312 else 1313 OS << ":"; 1314 } 1315 else OS << ", "; // Handle variadic methods. 1316 1317 PrintExpr(Mess->getArg(i)); 1318 } 1319 } 1320 OS << "]"; 1321} 1322 1323 1324void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1325 BlockDecl *BD = Node->getBlockDecl(); 1326 OS << "^"; 1327 1328 const FunctionType *AFT = Node->getFunctionType(); 1329 1330 if (isa<FunctionNoProtoType>(AFT)) { 1331 OS << "()"; 1332 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1333 OS << '('; 1334 std::string ParamStr; 1335 for (BlockDecl::param_iterator AI = BD->param_begin(), 1336 E = BD->param_end(); AI != E; ++AI) { 1337 if (AI != BD->param_begin()) OS << ", "; 1338 ParamStr = (*AI)->getNameAsString(); 1339 (*AI)->getType().getAsStringInternal(ParamStr, Policy); 1340 OS << ParamStr; 1341 } 1342 1343 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1344 if (FT->isVariadic()) { 1345 if (!BD->param_empty()) OS << ", "; 1346 OS << "..."; 1347 } 1348 OS << ')'; 1349 } 1350} 1351 1352void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) { 1353 OS << Node->getDecl(); 1354} 1355 1356void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {} 1357 1358//===----------------------------------------------------------------------===// 1359// Stmt method implementations 1360//===----------------------------------------------------------------------===// 1361 1362void Stmt::dumpPretty(ASTContext& Context) const { 1363 printPretty(llvm::errs(), Context, 0, 1364 PrintingPolicy(Context.getLangOptions())); 1365} 1366 1367void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context, 1368 PrinterHelper* Helper, 1369 const PrintingPolicy &Policy, 1370 unsigned Indentation) const { 1371 if (this == 0) { 1372 OS << "<NULL>"; 1373 return; 1374 } 1375 1376 if (Policy.Dump && &Context) { 1377 dump(OS, Context.getSourceManager()); 1378 return; 1379 } 1380 1381 StmtPrinter P(OS, Context, Helper, Policy, Indentation); 1382 P.Visit(const_cast<Stmt*>(this)); 1383} 1384 1385//===----------------------------------------------------------------------===// 1386// PrinterHelper 1387//===----------------------------------------------------------------------===// 1388 1389// Implement virtual destructor. 1390PrinterHelper::~PrinterHelper() {} 1391