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