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