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