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