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