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