StmtPrinter.cpp revision 094dbf917127a1228147587076d59ca45b7c258d
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/ASTContext.h" 16#include "clang/AST/Attr.h" 17#include "clang/AST/DeclCXX.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/DeclTemplate.h" 20#include "clang/AST/Expr.h" 21#include "clang/AST/ExprCXX.h" 22#include "clang/AST/PrettyPrinter.h" 23#include "clang/AST/StmtVisitor.h" 24#include "llvm/ADT/SmallString.h" 25using namespace clang; 26 27//===----------------------------------------------------------------------===// 28// StmtPrinter Visitor 29//===----------------------------------------------------------------------===// 30 31namespace { 32 class StmtPrinter : public StmtVisitor<StmtPrinter> { 33 raw_ostream &OS; 34 unsigned IndentLevel; 35 clang::PrinterHelper* Helper; 36 PrintingPolicy Policy; 37 38 public: 39 StmtPrinter(raw_ostream &os, PrinterHelper* helper, 40 const PrintingPolicy &Policy, 41 unsigned Indentation = 0) 42 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {} 43 44 void PrintStmt(Stmt *S) { 45 PrintStmt(S, Policy.Indentation); 46 } 47 48 void PrintStmt(Stmt *S, int SubIndent) { 49 IndentLevel += SubIndent; 50 if (S && isa<Expr>(S)) { 51 // If this is an expr used in a stmt context, indent and newline it. 52 Indent(); 53 Visit(S); 54 OS << ";\n"; 55 } else if (S) { 56 Visit(S); 57 } else { 58 Indent() << "<<<NULL STATEMENT>>>\n"; 59 } 60 IndentLevel -= SubIndent; 61 } 62 63 void PrintRawCompoundStmt(CompoundStmt *S); 64 void PrintRawDecl(Decl *D); 65 void PrintRawDeclStmt(const DeclStmt *S); 66 void PrintRawIfStmt(IfStmt *If); 67 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); 68 void PrintCallArgs(CallExpr *E); 69 void PrintRawSEHExceptHandler(SEHExceptStmt *S); 70 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S); 71 72 void PrintExpr(Expr *E) { 73 if (E) 74 Visit(E); 75 else 76 OS << "<null expr>"; 77 } 78 79 raw_ostream &Indent(int Delta = 0) { 80 for (int i = 0, e = IndentLevel+Delta; i < e; ++i) 81 OS << " "; 82 return OS; 83 } 84 85 void Visit(Stmt* S) { 86 if (Helper && Helper->handledStmt(S,OS)) 87 return; 88 else StmtVisitor<StmtPrinter>::Visit(S); 89 } 90 91 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED { 92 Indent() << "<<unknown stmt type>>\n"; 93 } 94 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED { 95 OS << "<<unknown expr type>>"; 96 } 97 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); 98 99#define ABSTRACT_STMT(CLASS) 100#define STMT(CLASS, PARENT) \ 101 void Visit##CLASS(CLASS *Node); 102#include "clang/AST/StmtNodes.inc" 103 }; 104} 105 106//===----------------------------------------------------------------------===// 107// Stmt printing methods. 108//===----------------------------------------------------------------------===// 109 110/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and 111/// with no newline after the }. 112void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { 113 OS << "{\n"; 114 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); 115 I != E; ++I) 116 PrintStmt(*I); 117 118 Indent() << "}"; 119} 120 121void StmtPrinter::PrintRawDecl(Decl *D) { 122 D->print(OS, Policy, IndentLevel); 123} 124 125void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) { 126 DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end(); 127 SmallVector<Decl*, 2> Decls; 128 for ( ; Begin != End; ++Begin) 129 Decls.push_back(*Begin); 130 131 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); 132} 133 134void StmtPrinter::VisitNullStmt(NullStmt *Node) { 135 Indent() << ";\n"; 136} 137 138void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { 139 Indent(); 140 PrintRawDeclStmt(Node); 141 OS << ";\n"; 142} 143 144void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { 145 Indent(); 146 PrintRawCompoundStmt(Node); 147 OS << "\n"; 148} 149 150void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { 151 Indent(-1) << "case "; 152 PrintExpr(Node->getLHS()); 153 if (Node->getRHS()) { 154 OS << " ... "; 155 PrintExpr(Node->getRHS()); 156 } 157 OS << ":\n"; 158 159 PrintStmt(Node->getSubStmt(), 0); 160} 161 162void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { 163 Indent(-1) << "default:\n"; 164 PrintStmt(Node->getSubStmt(), 0); 165} 166 167void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { 168 Indent(-1) << Node->getName() << ":\n"; 169 PrintStmt(Node->getSubStmt(), 0); 170} 171 172void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) { 173 OS << "[["; 174 bool first = true; 175 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(), 176 end = Node->getAttrs().end(); 177 it != end; ++it) { 178 if (!first) { 179 OS << ", "; 180 first = false; 181 } 182 // TODO: check this 183 (*it)->printPretty(OS, Policy); 184 } 185 OS << "]] "; 186 PrintStmt(Node->getSubStmt(), 0); 187} 188 189void StmtPrinter::PrintRawIfStmt(IfStmt *If) { 190 OS << "if ("; 191 if (const DeclStmt *DS = If->getConditionVariableDeclStmt()) 192 PrintRawDeclStmt(DS); 193 else 194 PrintExpr(If->getCond()); 195 OS << ')'; 196 197 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { 198 OS << ' '; 199 PrintRawCompoundStmt(CS); 200 OS << (If->getElse() ? ' ' : '\n'); 201 } else { 202 OS << '\n'; 203 PrintStmt(If->getThen()); 204 if (If->getElse()) Indent(); 205 } 206 207 if (Stmt *Else = If->getElse()) { 208 OS << "else"; 209 210 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { 211 OS << ' '; 212 PrintRawCompoundStmt(CS); 213 OS << '\n'; 214 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { 215 OS << ' '; 216 PrintRawIfStmt(ElseIf); 217 } else { 218 OS << '\n'; 219 PrintStmt(If->getElse()); 220 } 221 } 222} 223 224void StmtPrinter::VisitIfStmt(IfStmt *If) { 225 Indent(); 226 PrintRawIfStmt(If); 227} 228 229void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { 230 Indent() << "switch ("; 231 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 232 PrintRawDeclStmt(DS); 233 else 234 PrintExpr(Node->getCond()); 235 OS << ")"; 236 237 // Pretty print compoundstmt bodies (very common). 238 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 239 OS << " "; 240 PrintRawCompoundStmt(CS); 241 OS << "\n"; 242 } else { 243 OS << "\n"; 244 PrintStmt(Node->getBody()); 245 } 246} 247 248void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { 249 Indent() << "while ("; 250 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 251 PrintRawDeclStmt(DS); 252 else 253 PrintExpr(Node->getCond()); 254 OS << ")\n"; 255 PrintStmt(Node->getBody()); 256} 257 258void StmtPrinter::VisitDoStmt(DoStmt *Node) { 259 Indent() << "do "; 260 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 261 PrintRawCompoundStmt(CS); 262 OS << " "; 263 } else { 264 OS << "\n"; 265 PrintStmt(Node->getBody()); 266 Indent(); 267 } 268 269 OS << "while ("; 270 PrintExpr(Node->getCond()); 271 OS << ");\n"; 272} 273 274void StmtPrinter::VisitForStmt(ForStmt *Node) { 275 Indent() << "for ("; 276 if (Node->getInit()) { 277 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) 278 PrintRawDeclStmt(DS); 279 else 280 PrintExpr(cast<Expr>(Node->getInit())); 281 } 282 OS << ";"; 283 if (Node->getCond()) { 284 OS << " "; 285 PrintExpr(Node->getCond()); 286 } 287 OS << ";"; 288 if (Node->getInc()) { 289 OS << " "; 290 PrintExpr(Node->getInc()); 291 } 292 OS << ") "; 293 294 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 295 PrintRawCompoundStmt(CS); 296 OS << "\n"; 297 } else { 298 OS << "\n"; 299 PrintStmt(Node->getBody()); 300 } 301} 302 303void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { 304 Indent() << "for ("; 305 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) 306 PrintRawDeclStmt(DS); 307 else 308 PrintExpr(cast<Expr>(Node->getElement())); 309 OS << " in "; 310 PrintExpr(Node->getCollection()); 311 OS << ") "; 312 313 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 314 PrintRawCompoundStmt(CS); 315 OS << "\n"; 316 } else { 317 OS << "\n"; 318 PrintStmt(Node->getBody()); 319 } 320} 321 322void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) { 323 Indent() << "for ("; 324 PrintingPolicy SubPolicy(Policy); 325 SubPolicy.SuppressInitializers = true; 326 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel); 327 OS << " : "; 328 PrintExpr(Node->getRangeInit()); 329 OS << ") {\n"; 330 PrintStmt(Node->getBody()); 331 Indent() << "}\n"; 332} 333 334void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) { 335 Indent(); 336 if (Node->isIfExists()) 337 OS << "__if_exists ("; 338 else 339 OS << "__if_not_exists ("; 340 341 if (NestedNameSpecifier *Qualifier 342 = Node->getQualifierLoc().getNestedNameSpecifier()) 343 Qualifier->print(OS, Policy); 344 345 OS << Node->getNameInfo() << ") "; 346 347 PrintRawCompoundStmt(Node->getSubStmt()); 348} 349 350void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { 351 Indent() << "goto " << Node->getLabel()->getName() << ";\n"; 352} 353 354void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 355 Indent() << "goto *"; 356 PrintExpr(Node->getTarget()); 357 OS << ";\n"; 358} 359 360void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 361 Indent() << "continue;\n"; 362} 363 364void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 365 Indent() << "break;\n"; 366} 367 368 369void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 370 Indent() << "return"; 371 if (Node->getRetValue()) { 372 OS << " "; 373 PrintExpr(Node->getRetValue()); 374 } 375 OS << ";\n"; 376} 377 378 379void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { 380 Indent() << "asm "; 381 382 if (Node->isVolatile()) 383 OS << "volatile "; 384 385 OS << "("; 386 VisitStringLiteral(Node->getAsmString()); 387 388 // Outputs 389 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 390 Node->getNumClobbers() != 0) 391 OS << " : "; 392 393 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 394 if (i != 0) 395 OS << ", "; 396 397 if (!Node->getOutputName(i).empty()) { 398 OS << '['; 399 OS << Node->getOutputName(i); 400 OS << "] "; 401 } 402 403 VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 404 OS << " "; 405 Visit(Node->getOutputExpr(i)); 406 } 407 408 // Inputs 409 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 410 OS << " : "; 411 412 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 413 if (i != 0) 414 OS << ", "; 415 416 if (!Node->getInputName(i).empty()) { 417 OS << '['; 418 OS << Node->getInputName(i); 419 OS << "] "; 420 } 421 422 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 423 OS << " "; 424 Visit(Node->getInputExpr(i)); 425 } 426 427 // Clobbers 428 if (Node->getNumClobbers() != 0) 429 OS << " : "; 430 431 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 432 if (i != 0) 433 OS << ", "; 434 435 VisitStringLiteral(Node->getClobberStringLiteral(i)); 436 } 437 438 OS << ");\n"; 439} 440 441void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) { 442 // FIXME: Implement MS style inline asm statement printer. 443 Indent() << "__asm "; 444 if (Node->hasBraces()) 445 OS << "{\n"; 446 OS << *(Node->getAsmString()) << "\n"; 447 if (Node->hasBraces()) 448 Indent() << "}\n"; 449} 450 451void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 452 Indent() << "@try"; 453 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 454 PrintRawCompoundStmt(TS); 455 OS << "\n"; 456 } 457 458 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 459 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 460 Indent() << "@catch("; 461 if (catchStmt->getCatchParamDecl()) { 462 if (Decl *DS = catchStmt->getCatchParamDecl()) 463 PrintRawDecl(DS); 464 } 465 OS << ")"; 466 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 467 PrintRawCompoundStmt(CS); 468 OS << "\n"; 469 } 470 } 471 472 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 473 Node->getFinallyStmt())) { 474 Indent() << "@finally"; 475 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 476 OS << "\n"; 477 } 478} 479 480void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 481} 482 483void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 484 Indent() << "@catch (...) { /* todo */ } \n"; 485} 486 487void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 488 Indent() << "@throw"; 489 if (Node->getThrowExpr()) { 490 OS << " "; 491 PrintExpr(Node->getThrowExpr()); 492 } 493 OS << ";\n"; 494} 495 496void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 497 Indent() << "@synchronized ("; 498 PrintExpr(Node->getSynchExpr()); 499 OS << ")"; 500 PrintRawCompoundStmt(Node->getSynchBody()); 501 OS << "\n"; 502} 503 504void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 505 Indent() << "@autoreleasepool"; 506 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt())); 507 OS << "\n"; 508} 509 510void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 511 OS << "catch ("; 512 if (Decl *ExDecl = Node->getExceptionDecl()) 513 PrintRawDecl(ExDecl); 514 else 515 OS << "..."; 516 OS << ") "; 517 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 518} 519 520void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 521 Indent(); 522 PrintRawCXXCatchStmt(Node); 523 OS << "\n"; 524} 525 526void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 527 Indent() << "try "; 528 PrintRawCompoundStmt(Node->getTryBlock()); 529 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 530 OS << " "; 531 PrintRawCXXCatchStmt(Node->getHandler(i)); 532 } 533 OS << "\n"; 534} 535 536void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 537 Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 538 PrintRawCompoundStmt(Node->getTryBlock()); 539 SEHExceptStmt *E = Node->getExceptHandler(); 540 SEHFinallyStmt *F = Node->getFinallyHandler(); 541 if(E) 542 PrintRawSEHExceptHandler(E); 543 else { 544 assert(F && "Must have a finally block..."); 545 PrintRawSEHFinallyStmt(F); 546 } 547 OS << "\n"; 548} 549 550void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 551 OS << "__finally "; 552 PrintRawCompoundStmt(Node->getBlock()); 553 OS << "\n"; 554} 555 556void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 557 OS << "__except ("; 558 VisitExpr(Node->getFilterExpr()); 559 OS << ")\n"; 560 PrintRawCompoundStmt(Node->getBlock()); 561 OS << "\n"; 562} 563 564void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 565 Indent(); 566 PrintRawSEHExceptHandler(Node); 567 OS << "\n"; 568} 569 570void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 571 Indent(); 572 PrintRawSEHFinallyStmt(Node); 573 OS << "\n"; 574} 575 576//===----------------------------------------------------------------------===// 577// Expr printing methods. 578//===----------------------------------------------------------------------===// 579 580void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 581 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 582 Qualifier->print(OS, Policy); 583 if (Node->hasTemplateKeyword()) 584 OS << "template "; 585 OS << Node->getNameInfo(); 586 if (Node->hasExplicitTemplateArgs()) 587 OS << TemplateSpecializationType::PrintTemplateArgumentList( 588 Node->getTemplateArgs(), 589 Node->getNumTemplateArgs(), 590 Policy); 591} 592 593void StmtPrinter::VisitDependentScopeDeclRefExpr( 594 DependentScopeDeclRefExpr *Node) { 595 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 596 Qualifier->print(OS, Policy); 597 if (Node->hasTemplateKeyword()) 598 OS << "template "; 599 OS << Node->getNameInfo(); 600 if (Node->hasExplicitTemplateArgs()) 601 OS << TemplateSpecializationType::PrintTemplateArgumentList( 602 Node->getTemplateArgs(), 603 Node->getNumTemplateArgs(), 604 Policy); 605} 606 607void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 608 if (Node->getQualifier()) 609 Node->getQualifier()->print(OS, Policy); 610 if (Node->hasTemplateKeyword()) 611 OS << "template "; 612 OS << Node->getNameInfo(); 613 if (Node->hasExplicitTemplateArgs()) 614 OS << TemplateSpecializationType::PrintTemplateArgumentList( 615 Node->getTemplateArgs(), 616 Node->getNumTemplateArgs(), 617 Policy); 618} 619 620void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 621 if (Node->getBase()) { 622 PrintExpr(Node->getBase()); 623 OS << (Node->isArrow() ? "->" : "."); 624 } 625 OS << *Node->getDecl(); 626} 627 628void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 629 if (Node->isSuperReceiver()) 630 OS << "super."; 631 else if (Node->getBase()) { 632 PrintExpr(Node->getBase()); 633 OS << "."; 634 } 635 636 if (Node->isImplicitProperty()) 637 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString(); 638 else 639 OS << Node->getExplicitProperty()->getName(); 640} 641 642void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 643 644 PrintExpr(Node->getBaseExpr()); 645 OS << "["; 646 PrintExpr(Node->getKeyExpr()); 647 OS << "]"; 648} 649 650void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 651 switch (Node->getIdentType()) { 652 default: 653 llvm_unreachable("unknown case"); 654 case PredefinedExpr::Func: 655 OS << "__func__"; 656 break; 657 case PredefinedExpr::Function: 658 OS << "__FUNCTION__"; 659 break; 660 case PredefinedExpr::LFunction: 661 OS << "L__FUNCTION__"; 662 break; 663 case PredefinedExpr::PrettyFunction: 664 OS << "__PRETTY_FUNCTION__"; 665 break; 666 } 667} 668 669void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 670 unsigned value = Node->getValue(); 671 672 switch (Node->getKind()) { 673 case CharacterLiteral::Ascii: break; // no prefix. 674 case CharacterLiteral::Wide: OS << 'L'; break; 675 case CharacterLiteral::UTF16: OS << 'u'; break; 676 case CharacterLiteral::UTF32: OS << 'U'; break; 677 } 678 679 switch (value) { 680 case '\\': 681 OS << "'\\\\'"; 682 break; 683 case '\'': 684 OS << "'\\''"; 685 break; 686 case '\a': 687 // TODO: K&R: the meaning of '\\a' is different in traditional C 688 OS << "'\\a'"; 689 break; 690 case '\b': 691 OS << "'\\b'"; 692 break; 693 // Nonstandard escape sequence. 694 /*case '\e': 695 OS << "'\\e'"; 696 break;*/ 697 case '\f': 698 OS << "'\\f'"; 699 break; 700 case '\n': 701 OS << "'\\n'"; 702 break; 703 case '\r': 704 OS << "'\\r'"; 705 break; 706 case '\t': 707 OS << "'\\t'"; 708 break; 709 case '\v': 710 OS << "'\\v'"; 711 break; 712 default: 713 if (value < 256 && isprint(value)) { 714 OS << "'" << (char)value << "'"; 715 } else if (value < 256) { 716 OS << "'\\x"; 717 OS.write_hex(value) << "'"; 718 } else { 719 // FIXME what to really do here? 720 OS << value; 721 } 722 } 723} 724 725void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 726 bool isSigned = Node->getType()->isSignedIntegerType(); 727 OS << Node->getValue().toString(10, isSigned); 728 729 // Emit suffixes. Integer literals are always a builtin integer type. 730 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 731 default: llvm_unreachable("Unexpected type for integer literal!"); 732 // FIXME: The Short and UShort cases are to handle cases where a short 733 // integeral literal is formed during template instantiation. They should 734 // be removed when template instantiation no longer needs integer literals. 735 case BuiltinType::Short: 736 case BuiltinType::UShort: 737 case BuiltinType::Int: break; // no suffix. 738 case BuiltinType::UInt: OS << 'U'; break; 739 case BuiltinType::Long: OS << 'L'; break; 740 case BuiltinType::ULong: OS << "UL"; break; 741 case BuiltinType::LongLong: OS << "LL"; break; 742 case BuiltinType::ULongLong: OS << "ULL"; break; 743 case BuiltinType::Int128: OS << "i128"; break; 744 case BuiltinType::UInt128: OS << "Ui128"; break; 745 } 746} 747 748static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 749 bool PrintSuffix) { 750 SmallString<16> Str; 751 Node->getValue().toString(Str); 752 OS << Str; 753 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 754 OS << '.'; // Trailing dot in order to separate from ints. 755 756 if (!PrintSuffix) 757 return; 758 759 // Emit suffixes. Float literals are always a builtin float type. 760 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 761 default: llvm_unreachable("Unexpected type for float literal!"); 762 case BuiltinType::Half: break; // FIXME: suffix? 763 case BuiltinType::Double: break; // no suffix. 764 case BuiltinType::Float: OS << 'F'; break; 765 case BuiltinType::LongDouble: OS << 'L'; break; 766 } 767} 768 769void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 770 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 771} 772 773void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 774 PrintExpr(Node->getSubExpr()); 775 OS << "i"; 776} 777 778void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 779 Str->outputString(OS); 780} 781void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 782 OS << "("; 783 PrintExpr(Node->getSubExpr()); 784 OS << ")"; 785} 786void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 787 if (!Node->isPostfix()) { 788 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 789 790 // Print a space if this is an "identifier operator" like __real, or if 791 // it might be concatenated incorrectly like '+'. 792 switch (Node->getOpcode()) { 793 default: break; 794 case UO_Real: 795 case UO_Imag: 796 case UO_Extension: 797 OS << ' '; 798 break; 799 case UO_Plus: 800 case UO_Minus: 801 if (isa<UnaryOperator>(Node->getSubExpr())) 802 OS << ' '; 803 break; 804 } 805 } 806 PrintExpr(Node->getSubExpr()); 807 808 if (Node->isPostfix()) 809 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 810} 811 812void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 813 OS << "__builtin_offsetof("; 814 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", "; 815 bool PrintedSomething = false; 816 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 817 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 818 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 819 // Array node 820 OS << "["; 821 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 822 OS << "]"; 823 PrintedSomething = true; 824 continue; 825 } 826 827 // Skip implicit base indirections. 828 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 829 continue; 830 831 // Field or identifier node. 832 IdentifierInfo *Id = ON.getFieldName(); 833 if (!Id) 834 continue; 835 836 if (PrintedSomething) 837 OS << "."; 838 else 839 PrintedSomething = true; 840 OS << Id->getName(); 841 } 842 OS << ")"; 843} 844 845void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 846 switch(Node->getKind()) { 847 case UETT_SizeOf: 848 OS << "sizeof"; 849 break; 850 case UETT_AlignOf: 851 if (Policy.LangOpts.CPlusPlus) 852 OS << "alignof"; 853 else if (Policy.LangOpts.C11) 854 OS << "_Alignof"; 855 else 856 OS << "__alignof"; 857 break; 858 case UETT_VecStep: 859 OS << "vec_step"; 860 break; 861 } 862 if (Node->isArgumentType()) 863 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")"; 864 else { 865 OS << " "; 866 PrintExpr(Node->getArgumentExpr()); 867 } 868} 869 870void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 871 OS << "_Generic("; 872 PrintExpr(Node->getControllingExpr()); 873 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 874 OS << ", "; 875 QualType T = Node->getAssocType(i); 876 if (T.isNull()) 877 OS << "default"; 878 else 879 OS << T.getAsString(Policy); 880 OS << ": "; 881 PrintExpr(Node->getAssocExpr(i)); 882 } 883 OS << ")"; 884} 885 886void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 887 PrintExpr(Node->getLHS()); 888 OS << "["; 889 PrintExpr(Node->getRHS()); 890 OS << "]"; 891} 892 893void StmtPrinter::PrintCallArgs(CallExpr *Call) { 894 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 895 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 896 // Don't print any defaulted arguments 897 break; 898 } 899 900 if (i) OS << ", "; 901 PrintExpr(Call->getArg(i)); 902 } 903} 904 905void StmtPrinter::VisitCallExpr(CallExpr *Call) { 906 PrintExpr(Call->getCallee()); 907 OS << "("; 908 PrintCallArgs(Call); 909 OS << ")"; 910} 911void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 912 // FIXME: Suppress printing implicit bases (like "this") 913 PrintExpr(Node->getBase()); 914 915 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 916 FieldDecl *ParentDecl = ParentMember 917 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL; 918 919 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 920 OS << (Node->isArrow() ? "->" : "."); 921 922 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 923 if (FD->isAnonymousStructOrUnion()) 924 return; 925 926 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 927 Qualifier->print(OS, Policy); 928 if (Node->hasTemplateKeyword()) 929 OS << "template "; 930 OS << Node->getMemberNameInfo(); 931 if (Node->hasExplicitTemplateArgs()) 932 OS << TemplateSpecializationType::PrintTemplateArgumentList( 933 Node->getTemplateArgs(), 934 Node->getNumTemplateArgs(), 935 Policy); 936} 937void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 938 PrintExpr(Node->getBase()); 939 OS << (Node->isArrow() ? "->isa" : ".isa"); 940} 941 942void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 943 PrintExpr(Node->getBase()); 944 OS << "."; 945 OS << Node->getAccessor().getName(); 946} 947void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 948 OS << "(" << Node->getTypeAsWritten().getAsString(Policy) << ")"; 949 PrintExpr(Node->getSubExpr()); 950} 951void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 952 OS << "(" << Node->getType().getAsString(Policy) << ")"; 953 PrintExpr(Node->getInitializer()); 954} 955void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 956 // No need to print anything, simply forward to the sub expression. 957 PrintExpr(Node->getSubExpr()); 958} 959void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 960 PrintExpr(Node->getLHS()); 961 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 962 PrintExpr(Node->getRHS()); 963} 964void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 965 PrintExpr(Node->getLHS()); 966 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 967 PrintExpr(Node->getRHS()); 968} 969void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 970 PrintExpr(Node->getCond()); 971 OS << " ? "; 972 PrintExpr(Node->getLHS()); 973 OS << " : "; 974 PrintExpr(Node->getRHS()); 975} 976 977// GNU extensions. 978 979void 980StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 981 PrintExpr(Node->getCommon()); 982 OS << " ?: "; 983 PrintExpr(Node->getFalseExpr()); 984} 985void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 986 OS << "&&" << Node->getLabel()->getName(); 987} 988 989void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 990 OS << "("; 991 PrintRawCompoundStmt(E->getSubStmt()); 992 OS << ")"; 993} 994 995void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 996 OS << "__builtin_choose_expr("; 997 PrintExpr(Node->getCond()); 998 OS << ", "; 999 PrintExpr(Node->getLHS()); 1000 OS << ", "; 1001 PrintExpr(Node->getRHS()); 1002 OS << ")"; 1003} 1004 1005void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1006 OS << "__null"; 1007} 1008 1009void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1010 OS << "__builtin_shufflevector("; 1011 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1012 if (i) OS << ", "; 1013 PrintExpr(Node->getExpr(i)); 1014 } 1015 OS << ")"; 1016} 1017 1018void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1019 if (Node->getSyntacticForm()) { 1020 Visit(Node->getSyntacticForm()); 1021 return; 1022 } 1023 1024 OS << "{ "; 1025 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1026 if (i) OS << ", "; 1027 if (Node->getInit(i)) 1028 PrintExpr(Node->getInit(i)); 1029 else 1030 OS << "0"; 1031 } 1032 OS << " }"; 1033} 1034 1035void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1036 OS << "( "; 1037 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1038 if (i) OS << ", "; 1039 PrintExpr(Node->getExpr(i)); 1040 } 1041 OS << " )"; 1042} 1043 1044void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1045 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1046 DEnd = Node->designators_end(); 1047 D != DEnd; ++D) { 1048 if (D->isFieldDesignator()) { 1049 if (D->getDotLoc().isInvalid()) 1050 OS << D->getFieldName()->getName() << ":"; 1051 else 1052 OS << "." << D->getFieldName()->getName(); 1053 } else { 1054 OS << "["; 1055 if (D->isArrayDesignator()) { 1056 PrintExpr(Node->getArrayIndex(*D)); 1057 } else { 1058 PrintExpr(Node->getArrayRangeStart(*D)); 1059 OS << " ... "; 1060 PrintExpr(Node->getArrayRangeEnd(*D)); 1061 } 1062 OS << "]"; 1063 } 1064 } 1065 1066 OS << " = "; 1067 PrintExpr(Node->getInit()); 1068} 1069 1070void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1071 if (Policy.LangOpts.CPlusPlus) 1072 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; 1073 else { 1074 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; 1075 if (Node->getType()->isRecordType()) 1076 OS << "{}"; 1077 else 1078 OS << 0; 1079 } 1080} 1081 1082void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1083 OS << "__builtin_va_arg("; 1084 PrintExpr(Node->getSubExpr()); 1085 OS << ", "; 1086 OS << Node->getType().getAsString(Policy); 1087 OS << ")"; 1088} 1089 1090void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1091 PrintExpr(Node->getSyntacticForm()); 1092} 1093 1094void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1095 const char *Name = 0; 1096 switch (Node->getOp()) { 1097#define BUILTIN(ID, TYPE, ATTRS) 1098#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1099 case AtomicExpr::AO ## ID: \ 1100 Name = #ID "("; \ 1101 break; 1102#include "clang/Basic/Builtins.def" 1103 } 1104 OS << Name; 1105 1106 // AtomicExpr stores its subexpressions in a permuted order. 1107 PrintExpr(Node->getPtr()); 1108 OS << ", "; 1109 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1110 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1111 PrintExpr(Node->getVal1()); 1112 OS << ", "; 1113 } 1114 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1115 Node->isCmpXChg()) { 1116 PrintExpr(Node->getVal2()); 1117 OS << ", "; 1118 } 1119 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1120 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1121 PrintExpr(Node->getWeak()); 1122 OS << ", "; 1123 } 1124 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) 1125 PrintExpr(Node->getOrder()); 1126 if (Node->isCmpXChg()) { 1127 OS << ", "; 1128 PrintExpr(Node->getOrderFail()); 1129 } 1130 OS << ")"; 1131} 1132 1133// C++ 1134void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1135 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1136 "", 1137#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1138 Spelling, 1139#include "clang/Basic/OperatorKinds.def" 1140 }; 1141 1142 OverloadedOperatorKind Kind = Node->getOperator(); 1143 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1144 if (Node->getNumArgs() == 1) { 1145 OS << OpStrings[Kind] << ' '; 1146 PrintExpr(Node->getArg(0)); 1147 } else { 1148 PrintExpr(Node->getArg(0)); 1149 OS << ' ' << OpStrings[Kind]; 1150 } 1151 } else if (Kind == OO_Arrow) { 1152 PrintExpr(Node->getArg(0)); 1153 } else if (Kind == OO_Call) { 1154 PrintExpr(Node->getArg(0)); 1155 OS << '('; 1156 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1157 if (ArgIdx > 1) 1158 OS << ", "; 1159 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1160 PrintExpr(Node->getArg(ArgIdx)); 1161 } 1162 OS << ')'; 1163 } else if (Kind == OO_Subscript) { 1164 PrintExpr(Node->getArg(0)); 1165 OS << '['; 1166 PrintExpr(Node->getArg(1)); 1167 OS << ']'; 1168 } else if (Node->getNumArgs() == 1) { 1169 OS << OpStrings[Kind] << ' '; 1170 PrintExpr(Node->getArg(0)); 1171 } else if (Node->getNumArgs() == 2) { 1172 PrintExpr(Node->getArg(0)); 1173 OS << ' ' << OpStrings[Kind] << ' '; 1174 PrintExpr(Node->getArg(1)); 1175 } else { 1176 llvm_unreachable("unknown overloaded operator"); 1177 } 1178} 1179 1180void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1181 VisitCallExpr(cast<CallExpr>(Node)); 1182} 1183 1184void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1185 PrintExpr(Node->getCallee()); 1186 OS << "<<<"; 1187 PrintCallArgs(Node->getConfig()); 1188 OS << ">>>("; 1189 PrintCallArgs(Node); 1190 OS << ")"; 1191} 1192 1193void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1194 OS << Node->getCastName() << '<'; 1195 OS << Node->getTypeAsWritten().getAsString(Policy) << ">("; 1196 PrintExpr(Node->getSubExpr()); 1197 OS << ")"; 1198} 1199 1200void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1201 VisitCXXNamedCastExpr(Node); 1202} 1203 1204void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1205 VisitCXXNamedCastExpr(Node); 1206} 1207 1208void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1209 VisitCXXNamedCastExpr(Node); 1210} 1211 1212void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1213 VisitCXXNamedCastExpr(Node); 1214} 1215 1216void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1217 OS << "typeid("; 1218 if (Node->isTypeOperand()) { 1219 OS << Node->getTypeOperand().getAsString(Policy); 1220 } else { 1221 PrintExpr(Node->getExprOperand()); 1222 } 1223 OS << ")"; 1224} 1225 1226void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1227 OS << "__uuidof("; 1228 if (Node->isTypeOperand()) { 1229 OS << Node->getTypeOperand().getAsString(Policy); 1230 } else { 1231 PrintExpr(Node->getExprOperand()); 1232 } 1233 OS << ")"; 1234} 1235 1236void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1237 switch (Node->getLiteralOperatorKind()) { 1238 case UserDefinedLiteral::LOK_Raw: 1239 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1240 break; 1241 case UserDefinedLiteral::LOK_Template: { 1242 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1243 const TemplateArgumentList *Args = 1244 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1245 assert(Args); 1246 const TemplateArgument &Pack = Args->get(0); 1247 for (TemplateArgument::pack_iterator I = Pack.pack_begin(), 1248 E = Pack.pack_end(); I != E; ++I) { 1249 char C = (char)I->getAsIntegral().getZExtValue(); 1250 OS << C; 1251 } 1252 break; 1253 } 1254 case UserDefinedLiteral::LOK_Integer: { 1255 // Print integer literal without suffix. 1256 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1257 OS << Int->getValue().toString(10, /*isSigned*/false); 1258 break; 1259 } 1260 case UserDefinedLiteral::LOK_Floating: { 1261 // Print floating literal without suffix. 1262 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1263 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1264 break; 1265 } 1266 case UserDefinedLiteral::LOK_String: 1267 case UserDefinedLiteral::LOK_Character: 1268 PrintExpr(Node->getCookedLiteral()); 1269 break; 1270 } 1271 OS << Node->getUDSuffix()->getName(); 1272} 1273 1274void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1275 OS << (Node->getValue() ? "true" : "false"); 1276} 1277 1278void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1279 OS << "nullptr"; 1280} 1281 1282void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1283 OS << "this"; 1284} 1285 1286void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1287 if (Node->getSubExpr() == 0) 1288 OS << "throw"; 1289 else { 1290 OS << "throw "; 1291 PrintExpr(Node->getSubExpr()); 1292 } 1293} 1294 1295void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1296 // Nothing to print: we picked up the default argument 1297} 1298 1299void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1300 OS << Node->getType().getAsString(Policy); 1301 OS << "("; 1302 PrintExpr(Node->getSubExpr()); 1303 OS << ")"; 1304} 1305 1306void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1307 PrintExpr(Node->getSubExpr()); 1308} 1309 1310void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1311 OS << Node->getType().getAsString(Policy); 1312 OS << "("; 1313 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1314 ArgEnd = Node->arg_end(); 1315 Arg != ArgEnd; ++Arg) { 1316 if (Arg != Node->arg_begin()) 1317 OS << ", "; 1318 PrintExpr(*Arg); 1319 } 1320 OS << ")"; 1321} 1322 1323void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1324 OS << '['; 1325 bool NeedComma = false; 1326 switch (Node->getCaptureDefault()) { 1327 case LCD_None: 1328 break; 1329 1330 case LCD_ByCopy: 1331 OS << '='; 1332 NeedComma = true; 1333 break; 1334 1335 case LCD_ByRef: 1336 OS << '&'; 1337 NeedComma = true; 1338 break; 1339 } 1340 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1341 CEnd = Node->explicit_capture_end(); 1342 C != CEnd; 1343 ++C) { 1344 if (NeedComma) 1345 OS << ", "; 1346 NeedComma = true; 1347 1348 switch (C->getCaptureKind()) { 1349 case LCK_This: 1350 OS << "this"; 1351 break; 1352 1353 case LCK_ByRef: 1354 if (Node->getCaptureDefault() != LCD_ByRef) 1355 OS << '&'; 1356 OS << C->getCapturedVar()->getName(); 1357 break; 1358 1359 case LCK_ByCopy: 1360 if (Node->getCaptureDefault() != LCD_ByCopy) 1361 OS << '='; 1362 OS << C->getCapturedVar()->getName(); 1363 break; 1364 } 1365 } 1366 OS << ']'; 1367 1368 if (Node->hasExplicitParameters()) { 1369 OS << " ("; 1370 CXXMethodDecl *Method = Node->getCallOperator(); 1371 NeedComma = false; 1372 for (CXXMethodDecl::param_iterator P = Method->param_begin(), 1373 PEnd = Method->param_end(); 1374 P != PEnd; ++P) { 1375 if (NeedComma) { 1376 OS << ", "; 1377 } else { 1378 NeedComma = true; 1379 } 1380 std::string ParamStr = (*P)->getNameAsString(); 1381 (*P)->getOriginalType().getAsStringInternal(ParamStr, Policy); 1382 OS << ParamStr; 1383 } 1384 if (Method->isVariadic()) { 1385 if (NeedComma) 1386 OS << ", "; 1387 OS << "..."; 1388 } 1389 OS << ')'; 1390 1391 if (Node->isMutable()) 1392 OS << " mutable"; 1393 1394 const FunctionProtoType *Proto 1395 = Method->getType()->getAs<FunctionProtoType>(); 1396 { 1397 std::string ExceptionSpec; 1398 Proto->printExceptionSpecification(ExceptionSpec, Policy); 1399 OS << ExceptionSpec; 1400 } 1401 1402 // FIXME: Attribute 1403 1404 // Print the trailing return type if it was specified in the source. 1405 if (Node->hasExplicitResultType()) 1406 OS << " -> " << Proto->getResultType().getAsString(Policy); 1407 } 1408 1409 // Print the body. 1410 CompoundStmt *Body = Node->getBody(); 1411 OS << ' '; 1412 PrintStmt(Body); 1413} 1414 1415void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1416 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1417 OS << TSInfo->getType().getAsString(Policy) << "()"; 1418 else 1419 OS << Node->getType().getAsString(Policy) << "()"; 1420} 1421 1422void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1423 if (E->isGlobalNew()) 1424 OS << "::"; 1425 OS << "new "; 1426 unsigned NumPlace = E->getNumPlacementArgs(); 1427 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1428 OS << "("; 1429 PrintExpr(E->getPlacementArg(0)); 1430 for (unsigned i = 1; i < NumPlace; ++i) { 1431 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1432 break; 1433 OS << ", "; 1434 PrintExpr(E->getPlacementArg(i)); 1435 } 1436 OS << ") "; 1437 } 1438 if (E->isParenTypeId()) 1439 OS << "("; 1440 std::string TypeS; 1441 if (Expr *Size = E->getArraySize()) { 1442 llvm::raw_string_ostream s(TypeS); 1443 Size->printPretty(s, Helper, Policy); 1444 s.flush(); 1445 TypeS = "[" + TypeS + "]"; 1446 } 1447 E->getAllocatedType().getAsStringInternal(TypeS, Policy); 1448 OS << TypeS; 1449 if (E->isParenTypeId()) 1450 OS << ")"; 1451 1452 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1453 if (InitStyle) { 1454 if (InitStyle == CXXNewExpr::CallInit) 1455 OS << "("; 1456 PrintExpr(E->getInitializer()); 1457 if (InitStyle == CXXNewExpr::CallInit) 1458 OS << ")"; 1459 } 1460} 1461 1462void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1463 if (E->isGlobalDelete()) 1464 OS << "::"; 1465 OS << "delete "; 1466 if (E->isArrayForm()) 1467 OS << "[] "; 1468 PrintExpr(E->getArgument()); 1469} 1470 1471void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1472 PrintExpr(E->getBase()); 1473 if (E->isArrow()) 1474 OS << "->"; 1475 else 1476 OS << '.'; 1477 if (E->getQualifier()) 1478 E->getQualifier()->print(OS, Policy); 1479 OS << "~"; 1480 1481 std::string TypeS; 1482 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1483 OS << II->getName(); 1484 else 1485 E->getDestroyedType().getAsStringInternal(TypeS, Policy); 1486 OS << TypeS; 1487} 1488 1489void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1490 if (E->isListInitialization()) 1491 OS << "{ "; 1492 1493 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1494 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1495 // Don't print any defaulted arguments 1496 break; 1497 } 1498 1499 if (i) OS << ", "; 1500 PrintExpr(E->getArg(i)); 1501 } 1502 1503 if (E->isListInitialization()) 1504 OS << " }"; 1505} 1506 1507void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1508 // Just forward to the sub expression. 1509 PrintExpr(E->getSubExpr()); 1510} 1511 1512void 1513StmtPrinter::VisitCXXUnresolvedConstructExpr( 1514 CXXUnresolvedConstructExpr *Node) { 1515 OS << Node->getTypeAsWritten().getAsString(Policy); 1516 OS << "("; 1517 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1518 ArgEnd = Node->arg_end(); 1519 Arg != ArgEnd; ++Arg) { 1520 if (Arg != Node->arg_begin()) 1521 OS << ", "; 1522 PrintExpr(*Arg); 1523 } 1524 OS << ")"; 1525} 1526 1527void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1528 CXXDependentScopeMemberExpr *Node) { 1529 if (!Node->isImplicitAccess()) { 1530 PrintExpr(Node->getBase()); 1531 OS << (Node->isArrow() ? "->" : "."); 1532 } 1533 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1534 Qualifier->print(OS, Policy); 1535 if (Node->hasTemplateKeyword()) 1536 OS << "template "; 1537 OS << Node->getMemberNameInfo(); 1538 if (Node->hasExplicitTemplateArgs()) { 1539 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1540 Node->getTemplateArgs(), 1541 Node->getNumTemplateArgs(), 1542 Policy); 1543 } 1544} 1545 1546void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1547 if (!Node->isImplicitAccess()) { 1548 PrintExpr(Node->getBase()); 1549 OS << (Node->isArrow() ? "->" : "."); 1550 } 1551 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1552 Qualifier->print(OS, Policy); 1553 if (Node->hasTemplateKeyword()) 1554 OS << "template "; 1555 OS << Node->getMemberNameInfo(); 1556 if (Node->hasExplicitTemplateArgs()) { 1557 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1558 Node->getTemplateArgs(), 1559 Node->getNumTemplateArgs(), 1560 Policy); 1561 } 1562} 1563 1564static const char *getTypeTraitName(UnaryTypeTrait UTT) { 1565 switch (UTT) { 1566 case UTT_HasNothrowAssign: return "__has_nothrow_assign"; 1567 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; 1568 case UTT_HasNothrowCopy: return "__has_nothrow_copy"; 1569 case UTT_HasTrivialAssign: return "__has_trivial_assign"; 1570 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor"; 1571 case UTT_HasTrivialCopy: return "__has_trivial_copy"; 1572 case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; 1573 case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; 1574 case UTT_IsAbstract: return "__is_abstract"; 1575 case UTT_IsArithmetic: return "__is_arithmetic"; 1576 case UTT_IsArray: return "__is_array"; 1577 case UTT_IsClass: return "__is_class"; 1578 case UTT_IsCompleteType: return "__is_complete_type"; 1579 case UTT_IsCompound: return "__is_compound"; 1580 case UTT_IsConst: return "__is_const"; 1581 case UTT_IsEmpty: return "__is_empty"; 1582 case UTT_IsEnum: return "__is_enum"; 1583 case UTT_IsFinal: return "__is_final"; 1584 case UTT_IsFloatingPoint: return "__is_floating_point"; 1585 case UTT_IsFunction: return "__is_function"; 1586 case UTT_IsFundamental: return "__is_fundamental"; 1587 case UTT_IsIntegral: return "__is_integral"; 1588 case UTT_IsInterfaceClass: return "__is_interface_class"; 1589 case UTT_IsLiteral: return "__is_literal"; 1590 case UTT_IsLvalueReference: return "__is_lvalue_reference"; 1591 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer"; 1592 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer"; 1593 case UTT_IsMemberPointer: return "__is_member_pointer"; 1594 case UTT_IsObject: return "__is_object"; 1595 case UTT_IsPOD: return "__is_pod"; 1596 case UTT_IsPointer: return "__is_pointer"; 1597 case UTT_IsPolymorphic: return "__is_polymorphic"; 1598 case UTT_IsReference: return "__is_reference"; 1599 case UTT_IsRvalueReference: return "__is_rvalue_reference"; 1600 case UTT_IsScalar: return "__is_scalar"; 1601 case UTT_IsSigned: return "__is_signed"; 1602 case UTT_IsStandardLayout: return "__is_standard_layout"; 1603 case UTT_IsTrivial: return "__is_trivial"; 1604 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable"; 1605 case UTT_IsUnion: return "__is_union"; 1606 case UTT_IsUnsigned: return "__is_unsigned"; 1607 case UTT_IsVoid: return "__is_void"; 1608 case UTT_IsVolatile: return "__is_volatile"; 1609 } 1610 llvm_unreachable("Type trait not covered by switch statement"); 1611} 1612 1613static const char *getTypeTraitName(BinaryTypeTrait BTT) { 1614 switch (BTT) { 1615 case BTT_IsBaseOf: return "__is_base_of"; 1616 case BTT_IsConvertible: return "__is_convertible"; 1617 case BTT_IsSame: return "__is_same"; 1618 case BTT_TypeCompatible: return "__builtin_types_compatible_p"; 1619 case BTT_IsConvertibleTo: return "__is_convertible_to"; 1620 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable"; 1621 } 1622 llvm_unreachable("Binary type trait not covered by switch"); 1623} 1624 1625static const char *getTypeTraitName(TypeTrait TT) { 1626 switch (TT) { 1627 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible"; 1628 } 1629 llvm_unreachable("Type trait not covered by switch"); 1630} 1631 1632static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1633 switch (ATT) { 1634 case ATT_ArrayRank: return "__array_rank"; 1635 case ATT_ArrayExtent: return "__array_extent"; 1636 } 1637 llvm_unreachable("Array type trait not covered by switch"); 1638} 1639 1640static const char *getExpressionTraitName(ExpressionTrait ET) { 1641 switch (ET) { 1642 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1643 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1644 } 1645 llvm_unreachable("Expression type trait not covered by switch"); 1646} 1647 1648void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 1649 OS << getTypeTraitName(E->getTrait()) << "(" 1650 << E->getQueriedType().getAsString(Policy) << ")"; 1651} 1652 1653void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 1654 OS << getTypeTraitName(E->getTrait()) << "(" 1655 << E->getLhsType().getAsString(Policy) << "," 1656 << E->getRhsType().getAsString(Policy) << ")"; 1657} 1658 1659void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 1660 OS << getTypeTraitName(E->getTrait()) << "("; 1661 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 1662 if (I > 0) 1663 OS << ", "; 1664 OS << E->getArg(I)->getType().getAsString(Policy); 1665 } 1666 OS << ")"; 1667} 1668 1669void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1670 OS << getTypeTraitName(E->getTrait()) << "(" 1671 << E->getQueriedType().getAsString(Policy) << ")"; 1672} 1673 1674void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1675 OS << getExpressionTraitName(E->getTrait()) << "("; 1676 PrintExpr(E->getQueriedExpression()); 1677 OS << ")"; 1678} 1679 1680void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1681 OS << "noexcept("; 1682 PrintExpr(E->getOperand()); 1683 OS << ")"; 1684} 1685 1686void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1687 PrintExpr(E->getPattern()); 1688 OS << "..."; 1689} 1690 1691void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1692 OS << "sizeof...(" << *E->getPack() << ")"; 1693} 1694 1695void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1696 SubstNonTypeTemplateParmPackExpr *Node) { 1697 OS << *Node->getParameterPack(); 1698} 1699 1700void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1701 SubstNonTypeTemplateParmExpr *Node) { 1702 Visit(Node->getReplacement()); 1703} 1704 1705void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 1706 OS << *E->getParameterPack(); 1707} 1708 1709void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1710 PrintExpr(Node->GetTemporaryExpr()); 1711} 1712 1713// Obj-C 1714 1715void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1716 OS << "@"; 1717 VisitStringLiteral(Node->getString()); 1718} 1719 1720void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 1721 OS << "@"; 1722 Visit(E->getSubExpr()); 1723} 1724 1725void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 1726 OS << "@[ "; 1727 StmtRange ch = E->children(); 1728 if (ch.first != ch.second) { 1729 while (1) { 1730 Visit(*ch.first); 1731 ++ch.first; 1732 if (ch.first == ch.second) break; 1733 OS << ", "; 1734 } 1735 } 1736 OS << " ]"; 1737} 1738 1739void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 1740 OS << "@{ "; 1741 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 1742 if (I > 0) 1743 OS << ", "; 1744 1745 ObjCDictionaryElement Element = E->getKeyValueElement(I); 1746 Visit(Element.Key); 1747 OS << " : "; 1748 Visit(Element.Value); 1749 if (Element.isPackExpansion()) 1750 OS << "..."; 1751 } 1752 OS << " }"; 1753} 1754 1755void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1756 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')'; 1757} 1758 1759void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1760 OS << "@selector(" << Node->getSelector().getAsString() << ')'; 1761} 1762 1763void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1764 OS << "@protocol(" << *Node->getProtocol() << ')'; 1765} 1766 1767void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1768 OS << "["; 1769 switch (Mess->getReceiverKind()) { 1770 case ObjCMessageExpr::Instance: 1771 PrintExpr(Mess->getInstanceReceiver()); 1772 break; 1773 1774 case ObjCMessageExpr::Class: 1775 OS << Mess->getClassReceiver().getAsString(Policy); 1776 break; 1777 1778 case ObjCMessageExpr::SuperInstance: 1779 case ObjCMessageExpr::SuperClass: 1780 OS << "Super"; 1781 break; 1782 } 1783 1784 OS << ' '; 1785 Selector selector = Mess->getSelector(); 1786 if (selector.isUnarySelector()) { 1787 OS << selector.getNameForSlot(0); 1788 } else { 1789 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1790 if (i < selector.getNumArgs()) { 1791 if (i > 0) OS << ' '; 1792 if (selector.getIdentifierInfoForSlot(i)) 1793 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1794 else 1795 OS << ":"; 1796 } 1797 else OS << ", "; // Handle variadic methods. 1798 1799 PrintExpr(Mess->getArg(i)); 1800 } 1801 } 1802 OS << "]"; 1803} 1804 1805void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 1806 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1807} 1808 1809void 1810StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 1811 PrintExpr(E->getSubExpr()); 1812} 1813 1814void 1815StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 1816 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy) 1817 << ")"; 1818 PrintExpr(E->getSubExpr()); 1819} 1820 1821void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1822 BlockDecl *BD = Node->getBlockDecl(); 1823 OS << "^"; 1824 1825 const FunctionType *AFT = Node->getFunctionType(); 1826 1827 if (isa<FunctionNoProtoType>(AFT)) { 1828 OS << "()"; 1829 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1830 OS << '('; 1831 std::string ParamStr; 1832 for (BlockDecl::param_iterator AI = BD->param_begin(), 1833 E = BD->param_end(); AI != E; ++AI) { 1834 if (AI != BD->param_begin()) OS << ", "; 1835 ParamStr = (*AI)->getNameAsString(); 1836 (*AI)->getType().getAsStringInternal(ParamStr, Policy); 1837 OS << ParamStr; 1838 } 1839 1840 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1841 if (FT->isVariadic()) { 1842 if (!BD->param_empty()) OS << ", "; 1843 OS << "..."; 1844 } 1845 OS << ')'; 1846 } 1847 OS << "{ }"; 1848} 1849 1850void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 1851 PrintExpr(Node->getSourceExpr()); 1852} 1853 1854void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 1855 OS << "__builtin_astype("; 1856 PrintExpr(Node->getSrcExpr()); 1857 OS << ", " << Node->getType().getAsString(); 1858 OS << ")"; 1859} 1860 1861//===----------------------------------------------------------------------===// 1862// Stmt method implementations 1863//===----------------------------------------------------------------------===// 1864 1865void Stmt::dumpPretty(ASTContext &Context) const { 1866 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts())); 1867} 1868 1869void Stmt::printPretty(raw_ostream &OS, 1870 PrinterHelper *Helper, 1871 const PrintingPolicy &Policy, 1872 unsigned Indentation) const { 1873 if (this == 0) { 1874 OS << "<NULL>"; 1875 return; 1876 } 1877 1878 if (Policy.DumpSourceManager) { 1879 dump(OS, *Policy.DumpSourceManager); 1880 return; 1881 } 1882 1883 StmtPrinter P(OS, Helper, Policy, Indentation); 1884 P.Visit(const_cast<Stmt*>(this)); 1885} 1886 1887//===----------------------------------------------------------------------===// 1888// PrinterHelper 1889//===----------------------------------------------------------------------===// 1890 1891// Implement virtual destructor. 1892PrinterHelper::~PrinterHelper() {} 1893