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