StmtPrinter.cpp revision 99e9b4d172f6877e6ba5ebe75bb8238721f5e01c
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 PrintRawCompoundStmt(CS); 405 OS << "\n"; 406 } 407 } 408 409 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 410 Node->getFinallyStmt())) { 411 Indent() << "@finally"; 412 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 413 OS << "\n"; 414 } 415} 416 417void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 418} 419 420void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 421 Indent() << "@catch (...) { /* todo */ } \n"; 422} 423 424void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 425 Indent() << "@throw"; 426 if (Node->getThrowExpr()) { 427 OS << " "; 428 PrintExpr(Node->getThrowExpr()); 429 } 430 OS << ";\n"; 431} 432 433void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 434 Indent() << "@synchronized ("; 435 PrintExpr(Node->getSynchExpr()); 436 OS << ")"; 437 PrintRawCompoundStmt(Node->getSynchBody()); 438 OS << "\n"; 439} 440 441void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 442 OS << "catch ("; 443 if (Decl *ExDecl = Node->getExceptionDecl()) 444 PrintRawDecl(ExDecl); 445 else 446 OS << "..."; 447 OS << ") "; 448 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 449} 450 451void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 452 Indent(); 453 PrintRawCXXCatchStmt(Node); 454 OS << "\n"; 455} 456 457void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 458 Indent() << "try "; 459 PrintRawCompoundStmt(Node->getTryBlock()); 460 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 461 OS << " "; 462 PrintRawCXXCatchStmt(Node->getHandler(i)); 463 } 464 OS << "\n"; 465} 466 467//===----------------------------------------------------------------------===// 468// Expr printing methods. 469//===----------------------------------------------------------------------===// 470 471void StmtPrinter::VisitExpr(Expr *Node) { 472 OS << "<<unknown expr type>>"; 473} 474 475void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 476 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 477 Qualifier->print(OS, Policy); 478 OS << Node->getDecl()->getNameAsString(); 479 if (Node->hasExplicitTemplateArgumentList()) 480 OS << TemplateSpecializationType::PrintTemplateArgumentList( 481 Node->getTemplateArgs(), 482 Node->getNumTemplateArgs(), 483 Policy); 484} 485 486void StmtPrinter::VisitDependentScopeDeclRefExpr( 487 DependentScopeDeclRefExpr *Node) { 488 Node->getQualifier()->print(OS, Policy); 489 OS << Node->getDeclName().getAsString(); 490 if (Node->hasExplicitTemplateArgs()) 491 OS << TemplateSpecializationType::PrintTemplateArgumentList( 492 Node->getTemplateArgs(), 493 Node->getNumTemplateArgs(), 494 Policy); 495} 496 497void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 498 if (Node->getQualifier()) 499 Node->getQualifier()->print(OS, Policy); 500 OS << Node->getName().getAsString(); 501 if (Node->hasExplicitTemplateArgs()) 502 OS << TemplateSpecializationType::PrintTemplateArgumentList( 503 Node->getTemplateArgs(), 504 Node->getNumTemplateArgs(), 505 Policy); 506} 507 508void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 509 if (Node->getBase()) { 510 PrintExpr(Node->getBase()); 511 OS << (Node->isArrow() ? "->" : "."); 512 } 513 OS << Node->getDecl()->getNameAsString(); 514} 515 516void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 517 if (Node->getBase()) { 518 PrintExpr(Node->getBase()); 519 OS << "."; 520 } 521 OS << Node->getProperty()->getNameAsCString(); 522} 523 524void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr( 525 ObjCImplicitSetterGetterRefExpr *Node) { 526 if (Node->getBase()) { 527 PrintExpr(Node->getBase()); 528 OS << "."; 529 } 530 if (Node->getGetterMethod()) 531 OS << Node->getGetterMethod()->getNameAsString(); 532 533} 534 535void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 536 switch (Node->getIdentType()) { 537 default: 538 assert(0 && "unknown case"); 539 case PredefinedExpr::Func: 540 OS << "__func__"; 541 break; 542 case PredefinedExpr::Function: 543 OS << "__FUNCTION__"; 544 break; 545 case PredefinedExpr::PrettyFunction: 546 OS << "__PRETTY_FUNCTION__"; 547 break; 548 } 549} 550 551void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 552 unsigned value = Node->getValue(); 553 if (Node->isWide()) 554 OS << "L"; 555 switch (value) { 556 case '\\': 557 OS << "'\\\\'"; 558 break; 559 case '\'': 560 OS << "'\\''"; 561 break; 562 case '\a': 563 // TODO: K&R: the meaning of '\\a' is different in traditional C 564 OS << "'\\a'"; 565 break; 566 case '\b': 567 OS << "'\\b'"; 568 break; 569 // Nonstandard escape sequence. 570 /*case '\e': 571 OS << "'\\e'"; 572 break;*/ 573 case '\f': 574 OS << "'\\f'"; 575 break; 576 case '\n': 577 OS << "'\\n'"; 578 break; 579 case '\r': 580 OS << "'\\r'"; 581 break; 582 case '\t': 583 OS << "'\\t'"; 584 break; 585 case '\v': 586 OS << "'\\v'"; 587 break; 588 default: 589 if (value < 256 && isprint(value)) { 590 OS << "'" << (char)value << "'"; 591 } else if (value < 256) { 592 OS << "'\\x" << llvm::format("%x", value) << "'"; 593 } else { 594 // FIXME what to really do here? 595 OS << value; 596 } 597 } 598} 599 600void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 601 bool isSigned = Node->getType()->isSignedIntegerType(); 602 OS << Node->getValue().toString(10, isSigned); 603 604 // Emit suffixes. Integer literals are always a builtin integer type. 605 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 606 default: assert(0 && "Unexpected type for integer literal!"); 607 case BuiltinType::Int: break; // no suffix. 608 case BuiltinType::UInt: OS << 'U'; break; 609 case BuiltinType::Long: OS << 'L'; break; 610 case BuiltinType::ULong: OS << "UL"; break; 611 case BuiltinType::LongLong: OS << "LL"; break; 612 case BuiltinType::ULongLong: OS << "ULL"; break; 613 } 614} 615void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 616 // FIXME: print value more precisely. 617 OS << Node->getValueAsApproximateDouble(); 618} 619 620void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 621 PrintExpr(Node->getSubExpr()); 622 OS << "i"; 623} 624 625void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 626 if (Str->isWide()) OS << 'L'; 627 OS << '"'; 628 629 // FIXME: this doesn't print wstrings right. 630 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { 631 unsigned char Char = Str->getStrData()[i]; 632 633 switch (Char) { 634 default: 635 if (isprint(Char)) 636 OS << (char)Char; 637 else // Output anything hard as an octal escape. 638 OS << '\\' 639 << (char)('0'+ ((Char >> 6) & 7)) 640 << (char)('0'+ ((Char >> 3) & 7)) 641 << (char)('0'+ ((Char >> 0) & 7)); 642 break; 643 // Handle some common non-printable cases to make dumps prettier. 644 case '\\': OS << "\\\\"; break; 645 case '"': OS << "\\\""; break; 646 case '\n': OS << "\\n"; break; 647 case '\t': OS << "\\t"; break; 648 case '\a': OS << "\\a"; break; 649 case '\b': OS << "\\b"; break; 650 } 651 } 652 OS << '"'; 653} 654void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 655 OS << "("; 656 PrintExpr(Node->getSubExpr()); 657 OS << ")"; 658} 659void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 660 if (!Node->isPostfix()) { 661 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 662 663 // Print a space if this is an "identifier operator" like __real, or if 664 // it might be concatenated incorrectly like '+'. 665 switch (Node->getOpcode()) { 666 default: break; 667 case UnaryOperator::Real: 668 case UnaryOperator::Imag: 669 case UnaryOperator::Extension: 670 OS << ' '; 671 break; 672 case UnaryOperator::Plus: 673 case UnaryOperator::Minus: 674 if (isa<UnaryOperator>(Node->getSubExpr())) 675 OS << ' '; 676 break; 677 } 678 } 679 PrintExpr(Node->getSubExpr()); 680 681 if (Node->isPostfix()) 682 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 683} 684 685bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) { 686 if (isa<UnaryOperator>(E)) { 687 // Base case, print the type and comma. 688 OS << E->getType().getAsString() << ", "; 689 return true; 690 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { 691 PrintOffsetOfDesignator(ASE->getLHS()); 692 OS << "["; 693 PrintExpr(ASE->getRHS()); 694 OS << "]"; 695 return false; 696 } else { 697 MemberExpr *ME = cast<MemberExpr>(E); 698 bool IsFirst = PrintOffsetOfDesignator(ME->getBase()); 699 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString(); 700 return false; 701 } 702} 703 704void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) { 705 OS << "__builtin_offsetof("; 706 PrintOffsetOfDesignator(Node->getSubExpr()); 707 OS << ")"; 708} 709 710void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) { 711 OS << (Node->isSizeOf() ? "sizeof" : "__alignof"); 712 if (Node->isArgumentType()) 713 OS << "(" << Node->getArgumentType().getAsString() << ")"; 714 else { 715 OS << " "; 716 PrintExpr(Node->getArgumentExpr()); 717 } 718} 719void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 720 PrintExpr(Node->getLHS()); 721 OS << "["; 722 PrintExpr(Node->getRHS()); 723 OS << "]"; 724} 725 726void StmtPrinter::VisitCallExpr(CallExpr *Call) { 727 PrintExpr(Call->getCallee()); 728 OS << "("; 729 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 730 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 731 // Don't print any defaulted arguments 732 break; 733 } 734 735 if (i) OS << ", "; 736 PrintExpr(Call->getArg(i)); 737 } 738 OS << ")"; 739} 740void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 741 // FIXME: Suppress printing implicit bases (like "this") 742 PrintExpr(Node->getBase()); 743 OS << (Node->isArrow() ? "->" : "."); 744 // FIXME: Suppress printing references to unnamed objects 745 // representing anonymous unions/structs 746 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 747 Qualifier->print(OS, Policy); 748 749 OS << Node->getMemberDecl()->getNameAsString(); 750 751 if (Node->hasExplicitTemplateArgumentList()) 752 OS << TemplateSpecializationType::PrintTemplateArgumentList( 753 Node->getTemplateArgs(), 754 Node->getNumTemplateArgs(), 755 Policy); 756} 757void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 758 PrintExpr(Node->getBase()); 759 OS << (Node->isArrow() ? "->isa" : ".isa"); 760} 761 762void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 763 PrintExpr(Node->getBase()); 764 OS << "."; 765 OS << Node->getAccessor().getName(); 766} 767void StmtPrinter::VisitCastExpr(CastExpr *) { 768 assert(0 && "CastExpr is an abstract class"); 769} 770void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) { 771 assert(0 && "ExplicitCastExpr is an abstract class"); 772} 773void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 774 OS << "(" << Node->getType().getAsString() << ")"; 775 PrintExpr(Node->getSubExpr()); 776} 777void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 778 OS << "(" << Node->getType().getAsString() << ")"; 779 PrintExpr(Node->getInitializer()); 780} 781void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 782 // No need to print anything, simply forward to the sub expression. 783 PrintExpr(Node->getSubExpr()); 784} 785void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 786 PrintExpr(Node->getLHS()); 787 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 788 PrintExpr(Node->getRHS()); 789} 790void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 791 PrintExpr(Node->getLHS()); 792 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 793 PrintExpr(Node->getRHS()); 794} 795void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 796 PrintExpr(Node->getCond()); 797 798 if (Node->getLHS()) { 799 OS << " ? "; 800 PrintExpr(Node->getLHS()); 801 OS << " : "; 802 } 803 else { // Handle GCC extension where LHS can be NULL. 804 OS << " ?: "; 805 } 806 807 PrintExpr(Node->getRHS()); 808} 809 810// GNU extensions. 811 812void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 813 OS << "&&" << Node->getLabel()->getName(); 814} 815 816void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 817 OS << "("; 818 PrintRawCompoundStmt(E->getSubStmt()); 819 OS << ")"; 820} 821 822void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) { 823 OS << "__builtin_types_compatible_p("; 824 OS << Node->getArgType1().getAsString() << ","; 825 OS << Node->getArgType2().getAsString() << ")"; 826} 827 828void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 829 OS << "__builtin_choose_expr("; 830 PrintExpr(Node->getCond()); 831 OS << ", "; 832 PrintExpr(Node->getLHS()); 833 OS << ", "; 834 PrintExpr(Node->getRHS()); 835 OS << ")"; 836} 837 838void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 839 OS << "__null"; 840} 841 842void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 843 OS << "__builtin_shufflevector("; 844 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 845 if (i) OS << ", "; 846 PrintExpr(Node->getExpr(i)); 847 } 848 OS << ")"; 849} 850 851void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 852 if (Node->getSyntacticForm()) { 853 Visit(Node->getSyntacticForm()); 854 return; 855 } 856 857 OS << "{ "; 858 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 859 if (i) OS << ", "; 860 if (Node->getInit(i)) 861 PrintExpr(Node->getInit(i)); 862 else 863 OS << "0"; 864 } 865 OS << " }"; 866} 867 868void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 869 OS << "( "; 870 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 871 if (i) OS << ", "; 872 PrintExpr(Node->getExpr(i)); 873 } 874 OS << " )"; 875} 876 877void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 878 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 879 DEnd = Node->designators_end(); 880 D != DEnd; ++D) { 881 if (D->isFieldDesignator()) { 882 if (D->getDotLoc().isInvalid()) 883 OS << D->getFieldName()->getName() << ":"; 884 else 885 OS << "." << D->getFieldName()->getName(); 886 } else { 887 OS << "["; 888 if (D->isArrayDesignator()) { 889 PrintExpr(Node->getArrayIndex(*D)); 890 } else { 891 PrintExpr(Node->getArrayRangeStart(*D)); 892 OS << " ... "; 893 PrintExpr(Node->getArrayRangeEnd(*D)); 894 } 895 OS << "]"; 896 } 897 } 898 899 OS << " = "; 900 PrintExpr(Node->getInit()); 901} 902 903void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 904 if (Policy.LangOpts.CPlusPlus) 905 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; 906 else { 907 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; 908 if (Node->getType()->isRecordType()) 909 OS << "{}"; 910 else 911 OS << 0; 912 } 913} 914 915void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 916 OS << "__builtin_va_arg("; 917 PrintExpr(Node->getSubExpr()); 918 OS << ", "; 919 OS << Node->getType().getAsString(); 920 OS << ")"; 921} 922 923// C++ 924void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 925 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 926 "", 927#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 928 Spelling, 929#include "clang/Basic/OperatorKinds.def" 930 }; 931 932 OverloadedOperatorKind Kind = Node->getOperator(); 933 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 934 if (Node->getNumArgs() == 1) { 935 OS << OpStrings[Kind] << ' '; 936 PrintExpr(Node->getArg(0)); 937 } else { 938 PrintExpr(Node->getArg(0)); 939 OS << ' ' << OpStrings[Kind]; 940 } 941 } else if (Kind == OO_Call) { 942 PrintExpr(Node->getArg(0)); 943 OS << '('; 944 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 945 if (ArgIdx > 1) 946 OS << ", "; 947 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 948 PrintExpr(Node->getArg(ArgIdx)); 949 } 950 OS << ')'; 951 } else if (Kind == OO_Subscript) { 952 PrintExpr(Node->getArg(0)); 953 OS << '['; 954 PrintExpr(Node->getArg(1)); 955 OS << ']'; 956 } else if (Node->getNumArgs() == 1) { 957 OS << OpStrings[Kind] << ' '; 958 PrintExpr(Node->getArg(0)); 959 } else if (Node->getNumArgs() == 2) { 960 PrintExpr(Node->getArg(0)); 961 OS << ' ' << OpStrings[Kind] << ' '; 962 PrintExpr(Node->getArg(1)); 963 } else { 964 assert(false && "unknown overloaded operator"); 965 } 966} 967 968void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 969 VisitCallExpr(cast<CallExpr>(Node)); 970} 971 972void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 973 OS << Node->getCastName() << '<'; 974 OS << Node->getTypeAsWritten().getAsString() << ">("; 975 PrintExpr(Node->getSubExpr()); 976 OS << ")"; 977} 978 979void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 980 VisitCXXNamedCastExpr(Node); 981} 982 983void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 984 VisitCXXNamedCastExpr(Node); 985} 986 987void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 988 VisitCXXNamedCastExpr(Node); 989} 990 991void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 992 VisitCXXNamedCastExpr(Node); 993} 994 995void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 996 OS << "typeid("; 997 if (Node->isTypeOperand()) { 998 OS << Node->getTypeOperand().getAsString(); 999 } else { 1000 PrintExpr(Node->getExprOperand()); 1001 } 1002 OS << ")"; 1003} 1004 1005void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1006 OS << (Node->getValue() ? "true" : "false"); 1007} 1008 1009void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1010 OS << "nullptr"; 1011} 1012 1013void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1014 OS << "this"; 1015} 1016 1017void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1018 if (Node->getSubExpr() == 0) 1019 OS << "throw"; 1020 else { 1021 OS << "throw "; 1022 PrintExpr(Node->getSubExpr()); 1023 } 1024} 1025 1026void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1027 // Nothing to print: we picked up the default argument 1028} 1029 1030void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1031 OS << Node->getType().getAsString(); 1032 OS << "("; 1033 PrintExpr(Node->getSubExpr()); 1034 OS << ")"; 1035} 1036 1037void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1038 PrintExpr(Node->getSubExpr()); 1039} 1040 1041void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1042 OS << Node->getType().getAsString(); 1043 OS << "("; 1044 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1045 ArgEnd = Node->arg_end(); 1046 Arg != ArgEnd; ++Arg) { 1047 if (Arg != Node->arg_begin()) 1048 OS << ", "; 1049 PrintExpr(*Arg); 1050 } 1051 OS << ")"; 1052} 1053 1054void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) { 1055 OS << Node->getType().getAsString() << "()"; 1056} 1057 1058void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1059 if (E->isGlobalNew()) 1060 OS << "::"; 1061 OS << "new "; 1062 unsigned NumPlace = E->getNumPlacementArgs(); 1063 if (NumPlace > 0) { 1064 OS << "("; 1065 PrintExpr(E->getPlacementArg(0)); 1066 for (unsigned i = 1; i < NumPlace; ++i) { 1067 OS << ", "; 1068 PrintExpr(E->getPlacementArg(i)); 1069 } 1070 OS << ") "; 1071 } 1072 if (E->isParenTypeId()) 1073 OS << "("; 1074 std::string TypeS; 1075 if (Expr *Size = E->getArraySize()) { 1076 llvm::raw_string_ostream s(TypeS); 1077 Size->printPretty(s, Context, Helper, Policy); 1078 s.flush(); 1079 TypeS = "[" + TypeS + "]"; 1080 } 1081 E->getAllocatedType().getAsStringInternal(TypeS, Policy); 1082 OS << TypeS; 1083 if (E->isParenTypeId()) 1084 OS << ")"; 1085 1086 if (E->hasInitializer()) { 1087 OS << "("; 1088 unsigned NumCons = E->getNumConstructorArgs(); 1089 if (NumCons > 0) { 1090 PrintExpr(E->getConstructorArg(0)); 1091 for (unsigned i = 1; i < NumCons; ++i) { 1092 OS << ", "; 1093 PrintExpr(E->getConstructorArg(i)); 1094 } 1095 } 1096 OS << ")"; 1097 } 1098} 1099 1100void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1101 if (E->isGlobalDelete()) 1102 OS << "::"; 1103 OS << "delete "; 1104 if (E->isArrayForm()) 1105 OS << "[] "; 1106 PrintExpr(E->getArgument()); 1107} 1108 1109void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1110 PrintExpr(E->getBase()); 1111 if (E->isArrow()) 1112 OS << "->"; 1113 else 1114 OS << '.'; 1115 if (E->getQualifier()) 1116 E->getQualifier()->print(OS, Policy); 1117 1118 std::string TypeS; 1119 E->getDestroyedType().getAsStringInternal(TypeS, Policy); 1120 OS << TypeS; 1121} 1122 1123void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1124 // Nothing to print. 1125} 1126 1127void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 1128 // Just forward to the sub expression. 1129 PrintExpr(E->getSubExpr()); 1130} 1131 1132void 1133StmtPrinter::VisitCXXUnresolvedConstructExpr( 1134 CXXUnresolvedConstructExpr *Node) { 1135 OS << Node->getTypeAsWritten().getAsString(); 1136 OS << "("; 1137 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1138 ArgEnd = Node->arg_end(); 1139 Arg != ArgEnd; ++Arg) { 1140 if (Arg != Node->arg_begin()) 1141 OS << ", "; 1142 PrintExpr(*Arg); 1143 } 1144 OS << ")"; 1145} 1146 1147void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1148 CXXDependentScopeMemberExpr *Node) { 1149 PrintExpr(Node->getBase()); 1150 OS << (Node->isArrow() ? "->" : "."); 1151 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1152 Qualifier->print(OS, Policy); 1153 else if (Node->hasExplicitTemplateArgumentList()) 1154 // FIXME: Track use of "template" keyword explicitly? 1155 OS << "template "; 1156 1157 OS << Node->getMember().getAsString(); 1158 1159 if (Node->hasExplicitTemplateArgumentList()) { 1160 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1161 Node->getTemplateArgs(), 1162 Node->getNumTemplateArgs(), 1163 Policy); 1164 } 1165} 1166 1167static const char *getTypeTraitName(UnaryTypeTrait UTT) { 1168 switch (UTT) { 1169 default: assert(false && "Unknown type trait"); 1170 case UTT_HasNothrowAssign: return "__has_nothrow_assign"; 1171 case UTT_HasNothrowCopy: return "__has_nothrow_copy"; 1172 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; 1173 case UTT_HasTrivialAssign: return "__has_trivial_assign"; 1174 case UTT_HasTrivialCopy: return "__has_trivial_copy"; 1175 case UTT_HasTrivialConstructor: return "__has_trivial_constructor"; 1176 case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; 1177 case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; 1178 case UTT_IsAbstract: return "__is_abstract"; 1179 case UTT_IsClass: return "__is_class"; 1180 case UTT_IsEmpty: return "__is_empty"; 1181 case UTT_IsEnum: return "__is_enum"; 1182 case UTT_IsPOD: return "__is_pod"; 1183 case UTT_IsPolymorphic: return "__is_polymorphic"; 1184 case UTT_IsUnion: return "__is_union"; 1185 } 1186} 1187 1188void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 1189 OS << getTypeTraitName(E->getTrait()) << "(" 1190 << E->getQueriedType().getAsString() << ")"; 1191} 1192 1193// Obj-C 1194 1195void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1196 OS << "@"; 1197 VisitStringLiteral(Node->getString()); 1198} 1199 1200void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1201 OS << "@encode(" << Node->getEncodedType().getAsString() << ')'; 1202} 1203 1204void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1205 OS << "@selector(" << Node->getSelector().getAsString() << ')'; 1206} 1207 1208void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1209 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')'; 1210} 1211 1212void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1213 OS << "["; 1214 Expr *receiver = Mess->getReceiver(); 1215 if (receiver) PrintExpr(receiver); 1216 else OS << Mess->getClassName()->getName(); 1217 OS << ' '; 1218 Selector selector = Mess->getSelector(); 1219 if (selector.isUnarySelector()) { 1220 OS << selector.getIdentifierInfoForSlot(0)->getName(); 1221 } else { 1222 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1223 if (i < selector.getNumArgs()) { 1224 if (i > 0) OS << ' '; 1225 if (selector.getIdentifierInfoForSlot(i)) 1226 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1227 else 1228 OS << ":"; 1229 } 1230 else OS << ", "; // Handle variadic methods. 1231 1232 PrintExpr(Mess->getArg(i)); 1233 } 1234 } 1235 OS << "]"; 1236} 1237 1238void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) { 1239 OS << "super"; 1240} 1241 1242void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1243 BlockDecl *BD = Node->getBlockDecl(); 1244 OS << "^"; 1245 1246 const FunctionType *AFT = Node->getFunctionType(); 1247 1248 if (isa<FunctionNoProtoType>(AFT)) { 1249 OS << "()"; 1250 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1251 OS << '('; 1252 std::string ParamStr; 1253 for (BlockDecl::param_iterator AI = BD->param_begin(), 1254 E = BD->param_end(); AI != E; ++AI) { 1255 if (AI != BD->param_begin()) OS << ", "; 1256 ParamStr = (*AI)->getNameAsString(); 1257 (*AI)->getType().getAsStringInternal(ParamStr, Policy); 1258 OS << ParamStr; 1259 } 1260 1261 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1262 if (FT->isVariadic()) { 1263 if (!BD->param_empty()) OS << ", "; 1264 OS << "..."; 1265 } 1266 OS << ')'; 1267 } 1268} 1269 1270void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) { 1271 OS << Node->getDecl()->getNameAsString(); 1272} 1273//===----------------------------------------------------------------------===// 1274// Stmt method implementations 1275//===----------------------------------------------------------------------===// 1276 1277void Stmt::dumpPretty(ASTContext& Context) const { 1278 printPretty(llvm::errs(), Context, 0, 1279 PrintingPolicy(Context.getLangOptions())); 1280} 1281 1282void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context, 1283 PrinterHelper* Helper, 1284 const PrintingPolicy &Policy, 1285 unsigned Indentation) const { 1286 if (this == 0) { 1287 OS << "<NULL>"; 1288 return; 1289 } 1290 1291 if (Policy.Dump && &Context) { 1292 dump(Context.getSourceManager()); 1293 return; 1294 } 1295 1296 StmtPrinter P(OS, Context, Helper, Policy, Indentation); 1297 P.Visit(const_cast<Stmt*>(this)); 1298} 1299 1300//===----------------------------------------------------------------------===// 1301// PrinterHelper 1302//===----------------------------------------------------------------------===// 1303 1304// Implement virtual destructor. 1305PrinterHelper::~PrinterHelper() {} 1306