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