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