StmtPrinter.cpp revision 4debc82d9d967501b8650599cd44003d7026f56c
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 static char Hex[] = "0123456789ABCDEF"; 714 715 for (unsigned I = 0, N = Str->getLength(); I != N; ++I) { 716 switch (uint32_t Char = Str->getCodeUnit(I)) { 717 default: 718 // FIXME: Is this the best way to print wchar_t? 719 if (Char > 0xff) { 720 // char32_t values are <= 0x10ffff. 721 if (Char > 0xffff) 722 OS << "\\U00" 723 << Hex[(Char >> 20) & 15] 724 << Hex[(Char >> 16) & 15]; 725 else 726 OS << "\\u"; 727 OS << Hex[(Char >> 12) & 15] 728 << Hex[(Char >> 8) & 15] 729 << Hex[(Char >> 4) & 15] 730 << Hex[(Char >> 0) & 15]; 731 break; 732 } 733 if (Char <= 0xff && isprint(Char)) 734 OS << (char)Char; 735 else // Output anything hard as an octal escape. 736 OS << '\\' 737 << (char)('0'+ ((Char >> 6) & 7)) 738 << (char)('0'+ ((Char >> 3) & 7)) 739 << (char)('0'+ ((Char >> 0) & 7)); 740 break; 741 // Handle some common non-printable cases to make dumps prettier. 742 case '\\': OS << "\\\\"; break; 743 case '"': OS << "\\\""; break; 744 case '\n': OS << "\\n"; break; 745 case '\t': OS << "\\t"; break; 746 case '\a': OS << "\\a"; break; 747 case '\b': OS << "\\b"; break; 748 } 749 } 750 OS << '"'; 751} 752void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 753 OS << "("; 754 PrintExpr(Node->getSubExpr()); 755 OS << ")"; 756} 757void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 758 if (!Node->isPostfix()) { 759 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 760 761 // Print a space if this is an "identifier operator" like __real, or if 762 // it might be concatenated incorrectly like '+'. 763 switch (Node->getOpcode()) { 764 default: break; 765 case UO_Real: 766 case UO_Imag: 767 case UO_Extension: 768 OS << ' '; 769 break; 770 case UO_Plus: 771 case UO_Minus: 772 if (isa<UnaryOperator>(Node->getSubExpr())) 773 OS << ' '; 774 break; 775 } 776 } 777 PrintExpr(Node->getSubExpr()); 778 779 if (Node->isPostfix()) 780 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 781} 782 783void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 784 OS << "__builtin_offsetof("; 785 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", "; 786 bool PrintedSomething = false; 787 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 788 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 789 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 790 // Array node 791 OS << "["; 792 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 793 OS << "]"; 794 PrintedSomething = true; 795 continue; 796 } 797 798 // Skip implicit base indirections. 799 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 800 continue; 801 802 // Field or identifier node. 803 IdentifierInfo *Id = ON.getFieldName(); 804 if (!Id) 805 continue; 806 807 if (PrintedSomething) 808 OS << "."; 809 else 810 PrintedSomething = true; 811 OS << Id->getName(); 812 } 813 OS << ")"; 814} 815 816void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 817 switch(Node->getKind()) { 818 case UETT_SizeOf: 819 OS << "sizeof"; 820 break; 821 case UETT_AlignOf: 822 OS << "__alignof"; 823 break; 824 case UETT_VecStep: 825 OS << "vec_step"; 826 break; 827 } 828 if (Node->isArgumentType()) 829 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")"; 830 else { 831 OS << " "; 832 PrintExpr(Node->getArgumentExpr()); 833 } 834} 835 836void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 837 OS << "_Generic("; 838 PrintExpr(Node->getControllingExpr()); 839 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 840 OS << ", "; 841 QualType T = Node->getAssocType(i); 842 if (T.isNull()) 843 OS << "default"; 844 else 845 OS << T.getAsString(Policy); 846 OS << ": "; 847 PrintExpr(Node->getAssocExpr(i)); 848 } 849 OS << ")"; 850} 851 852void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 853 PrintExpr(Node->getLHS()); 854 OS << "["; 855 PrintExpr(Node->getRHS()); 856 OS << "]"; 857} 858 859void StmtPrinter::PrintCallArgs(CallExpr *Call) { 860 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 861 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 862 // Don't print any defaulted arguments 863 break; 864 } 865 866 if (i) OS << ", "; 867 PrintExpr(Call->getArg(i)); 868 } 869} 870 871void StmtPrinter::VisitCallExpr(CallExpr *Call) { 872 PrintExpr(Call->getCallee()); 873 OS << "("; 874 PrintCallArgs(Call); 875 OS << ")"; 876} 877void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 878 // FIXME: Suppress printing implicit bases (like "this") 879 PrintExpr(Node->getBase()); 880 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 881 if (FD->isAnonymousStructOrUnion()) 882 return; 883 OS << (Node->isArrow() ? "->" : "."); 884 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 885 Qualifier->print(OS, Policy); 886 887 OS << Node->getMemberNameInfo(); 888 889 if (Node->hasExplicitTemplateArgs()) 890 OS << TemplateSpecializationType::PrintTemplateArgumentList( 891 Node->getTemplateArgs(), 892 Node->getNumTemplateArgs(), 893 Policy); 894} 895void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 896 PrintExpr(Node->getBase()); 897 OS << (Node->isArrow() ? "->isa" : ".isa"); 898} 899 900void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 901 PrintExpr(Node->getBase()); 902 OS << "."; 903 OS << Node->getAccessor().getName(); 904} 905void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 906 OS << "(" << Node->getType().getAsString(Policy) << ")"; 907 PrintExpr(Node->getSubExpr()); 908} 909void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 910 OS << "(" << Node->getType().getAsString(Policy) << ")"; 911 PrintExpr(Node->getInitializer()); 912} 913void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 914 // No need to print anything, simply forward to the sub expression. 915 PrintExpr(Node->getSubExpr()); 916} 917void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 918 PrintExpr(Node->getLHS()); 919 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 920 PrintExpr(Node->getRHS()); 921} 922void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 923 PrintExpr(Node->getLHS()); 924 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 925 PrintExpr(Node->getRHS()); 926} 927void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 928 PrintExpr(Node->getCond()); 929 OS << " ? "; 930 PrintExpr(Node->getLHS()); 931 OS << " : "; 932 PrintExpr(Node->getRHS()); 933} 934 935// GNU extensions. 936 937void 938StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 939 PrintExpr(Node->getCommon()); 940 OS << " ?: "; 941 PrintExpr(Node->getFalseExpr()); 942} 943void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 944 OS << "&&" << Node->getLabel()->getName(); 945} 946 947void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 948 OS << "("; 949 PrintRawCompoundStmt(E->getSubStmt()); 950 OS << ")"; 951} 952 953void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 954 OS << "__builtin_choose_expr("; 955 PrintExpr(Node->getCond()); 956 OS << ", "; 957 PrintExpr(Node->getLHS()); 958 OS << ", "; 959 PrintExpr(Node->getRHS()); 960 OS << ")"; 961} 962 963void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 964 OS << "__null"; 965} 966 967void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 968 OS << "__builtin_shufflevector("; 969 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 970 if (i) OS << ", "; 971 PrintExpr(Node->getExpr(i)); 972 } 973 OS << ")"; 974} 975 976void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 977 if (Node->getSyntacticForm()) { 978 Visit(Node->getSyntacticForm()); 979 return; 980 } 981 982 OS << "{ "; 983 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 984 if (i) OS << ", "; 985 if (Node->getInit(i)) 986 PrintExpr(Node->getInit(i)); 987 else 988 OS << "0"; 989 } 990 OS << " }"; 991} 992 993void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 994 OS << "( "; 995 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 996 if (i) OS << ", "; 997 PrintExpr(Node->getExpr(i)); 998 } 999 OS << " )"; 1000} 1001 1002void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1003 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1004 DEnd = Node->designators_end(); 1005 D != DEnd; ++D) { 1006 if (D->isFieldDesignator()) { 1007 if (D->getDotLoc().isInvalid()) 1008 OS << D->getFieldName()->getName() << ":"; 1009 else 1010 OS << "." << D->getFieldName()->getName(); 1011 } else { 1012 OS << "["; 1013 if (D->isArrayDesignator()) { 1014 PrintExpr(Node->getArrayIndex(*D)); 1015 } else { 1016 PrintExpr(Node->getArrayRangeStart(*D)); 1017 OS << " ... "; 1018 PrintExpr(Node->getArrayRangeEnd(*D)); 1019 } 1020 OS << "]"; 1021 } 1022 } 1023 1024 OS << " = "; 1025 PrintExpr(Node->getInit()); 1026} 1027 1028void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1029 if (Policy.LangOpts.CPlusPlus) 1030 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; 1031 else { 1032 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; 1033 if (Node->getType()->isRecordType()) 1034 OS << "{}"; 1035 else 1036 OS << 0; 1037 } 1038} 1039 1040void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1041 OS << "__builtin_va_arg("; 1042 PrintExpr(Node->getSubExpr()); 1043 OS << ", "; 1044 OS << Node->getType().getAsString(Policy); 1045 OS << ")"; 1046} 1047 1048void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1049 PrintExpr(Node->getSyntacticForm()); 1050} 1051 1052void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1053 const char *Name = 0; 1054 switch (Node->getOp()) { 1055 case AtomicExpr::Load: 1056 Name = "__atomic_load("; 1057 break; 1058 case AtomicExpr::Store: 1059 Name = "__atomic_store("; 1060 break; 1061 case AtomicExpr::CmpXchgStrong: 1062 Name = "__atomic_compare_exchange_strong("; 1063 break; 1064 case AtomicExpr::CmpXchgWeak: 1065 Name = "__atomic_compare_exchange_weak("; 1066 break; 1067 case AtomicExpr::Xchg: 1068 Name = "__atomic_exchange("; 1069 break; 1070 case AtomicExpr::Add: 1071 Name = "__atomic_fetch_add("; 1072 break; 1073 case AtomicExpr::Sub: 1074 Name = "__atomic_fetch_sub("; 1075 break; 1076 case AtomicExpr::And: 1077 Name = "__atomic_fetch_and("; 1078 break; 1079 case AtomicExpr::Or: 1080 Name = "__atomic_fetch_or("; 1081 break; 1082 case AtomicExpr::Xor: 1083 Name = "__atomic_fetch_xor("; 1084 break; 1085 } 1086 OS << Name; 1087 PrintExpr(Node->getPtr()); 1088 OS << ", "; 1089 if (Node->getOp() != AtomicExpr::Load) { 1090 PrintExpr(Node->getVal1()); 1091 OS << ", "; 1092 } 1093 if (Node->isCmpXChg()) { 1094 PrintExpr(Node->getVal2()); 1095 OS << ", "; 1096 } 1097 PrintExpr(Node->getOrder()); 1098 if (Node->isCmpXChg()) { 1099 OS << ", "; 1100 PrintExpr(Node->getOrderFail()); 1101 } 1102 OS << ")"; 1103} 1104 1105// C++ 1106void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1107 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1108 "", 1109#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1110 Spelling, 1111#include "clang/Basic/OperatorKinds.def" 1112 }; 1113 1114 OverloadedOperatorKind Kind = Node->getOperator(); 1115 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1116 if (Node->getNumArgs() == 1) { 1117 OS << OpStrings[Kind] << ' '; 1118 PrintExpr(Node->getArg(0)); 1119 } else { 1120 PrintExpr(Node->getArg(0)); 1121 OS << ' ' << OpStrings[Kind]; 1122 } 1123 } else if (Kind == OO_Call) { 1124 PrintExpr(Node->getArg(0)); 1125 OS << '('; 1126 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1127 if (ArgIdx > 1) 1128 OS << ", "; 1129 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1130 PrintExpr(Node->getArg(ArgIdx)); 1131 } 1132 OS << ')'; 1133 } else if (Kind == OO_Subscript) { 1134 PrintExpr(Node->getArg(0)); 1135 OS << '['; 1136 PrintExpr(Node->getArg(1)); 1137 OS << ']'; 1138 } else if (Node->getNumArgs() == 1) { 1139 OS << OpStrings[Kind] << ' '; 1140 PrintExpr(Node->getArg(0)); 1141 } else if (Node->getNumArgs() == 2) { 1142 PrintExpr(Node->getArg(0)); 1143 OS << ' ' << OpStrings[Kind] << ' '; 1144 PrintExpr(Node->getArg(1)); 1145 } else { 1146 llvm_unreachable("unknown overloaded operator"); 1147 } 1148} 1149 1150void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1151 VisitCallExpr(cast<CallExpr>(Node)); 1152} 1153 1154void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1155 PrintExpr(Node->getCallee()); 1156 OS << "<<<"; 1157 PrintCallArgs(Node->getConfig()); 1158 OS << ">>>("; 1159 PrintCallArgs(Node); 1160 OS << ")"; 1161} 1162 1163void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1164 OS << Node->getCastName() << '<'; 1165 OS << Node->getTypeAsWritten().getAsString(Policy) << ">("; 1166 PrintExpr(Node->getSubExpr()); 1167 OS << ")"; 1168} 1169 1170void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1171 VisitCXXNamedCastExpr(Node); 1172} 1173 1174void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1175 VisitCXXNamedCastExpr(Node); 1176} 1177 1178void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1179 VisitCXXNamedCastExpr(Node); 1180} 1181 1182void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1183 VisitCXXNamedCastExpr(Node); 1184} 1185 1186void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1187 OS << "typeid("; 1188 if (Node->isTypeOperand()) { 1189 OS << Node->getTypeOperand().getAsString(Policy); 1190 } else { 1191 PrintExpr(Node->getExprOperand()); 1192 } 1193 OS << ")"; 1194} 1195 1196void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1197 OS << "__uuidof("; 1198 if (Node->isTypeOperand()) { 1199 OS << Node->getTypeOperand().getAsString(Policy); 1200 } else { 1201 PrintExpr(Node->getExprOperand()); 1202 } 1203 OS << ")"; 1204} 1205 1206void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1207 OS << (Node->getValue() ? "true" : "false"); 1208} 1209 1210void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1211 OS << "nullptr"; 1212} 1213 1214void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1215 OS << "this"; 1216} 1217 1218void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1219 if (Node->getSubExpr() == 0) 1220 OS << "throw"; 1221 else { 1222 OS << "throw "; 1223 PrintExpr(Node->getSubExpr()); 1224 } 1225} 1226 1227void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1228 // Nothing to print: we picked up the default argument 1229} 1230 1231void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1232 OS << Node->getType().getAsString(Policy); 1233 OS << "("; 1234 PrintExpr(Node->getSubExpr()); 1235 OS << ")"; 1236} 1237 1238void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1239 PrintExpr(Node->getSubExpr()); 1240} 1241 1242void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1243 OS << Node->getType().getAsString(Policy); 1244 OS << "("; 1245 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1246 ArgEnd = Node->arg_end(); 1247 Arg != ArgEnd; ++Arg) { 1248 if (Arg != Node->arg_begin()) 1249 OS << ", "; 1250 PrintExpr(*Arg); 1251 } 1252 OS << ")"; 1253} 1254 1255void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1256 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1257 OS << TSInfo->getType().getAsString(Policy) << "()"; 1258 else 1259 OS << Node->getType().getAsString(Policy) << "()"; 1260} 1261 1262void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1263 if (E->isGlobalNew()) 1264 OS << "::"; 1265 OS << "new "; 1266 unsigned NumPlace = E->getNumPlacementArgs(); 1267 if (NumPlace > 0) { 1268 OS << "("; 1269 PrintExpr(E->getPlacementArg(0)); 1270 for (unsigned i = 1; i < NumPlace; ++i) { 1271 OS << ", "; 1272 PrintExpr(E->getPlacementArg(i)); 1273 } 1274 OS << ") "; 1275 } 1276 if (E->isParenTypeId()) 1277 OS << "("; 1278 std::string TypeS; 1279 if (Expr *Size = E->getArraySize()) { 1280 llvm::raw_string_ostream s(TypeS); 1281 Size->printPretty(s, Context, Helper, Policy); 1282 s.flush(); 1283 TypeS = "[" + TypeS + "]"; 1284 } 1285 E->getAllocatedType().getAsStringInternal(TypeS, Policy); 1286 OS << TypeS; 1287 if (E->isParenTypeId()) 1288 OS << ")"; 1289 1290 if (E->hasInitializer()) { 1291 OS << "("; 1292 unsigned NumCons = E->getNumConstructorArgs(); 1293 if (NumCons > 0) { 1294 PrintExpr(E->getConstructorArg(0)); 1295 for (unsigned i = 1; i < NumCons; ++i) { 1296 OS << ", "; 1297 PrintExpr(E->getConstructorArg(i)); 1298 } 1299 } 1300 OS << ")"; 1301 } 1302} 1303 1304void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1305 if (E->isGlobalDelete()) 1306 OS << "::"; 1307 OS << "delete "; 1308 if (E->isArrayForm()) 1309 OS << "[] "; 1310 PrintExpr(E->getArgument()); 1311} 1312 1313void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1314 PrintExpr(E->getBase()); 1315 if (E->isArrow()) 1316 OS << "->"; 1317 else 1318 OS << '.'; 1319 if (E->getQualifier()) 1320 E->getQualifier()->print(OS, Policy); 1321 1322 std::string TypeS; 1323 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1324 OS << II->getName(); 1325 else 1326 E->getDestroyedType().getAsStringInternal(TypeS, Policy); 1327 OS << TypeS; 1328} 1329 1330void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1331 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1332 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1333 // Don't print any defaulted arguments 1334 break; 1335 } 1336 1337 if (i) OS << ", "; 1338 PrintExpr(E->getArg(i)); 1339 } 1340} 1341 1342void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1343 // Just forward to the sub expression. 1344 PrintExpr(E->getSubExpr()); 1345} 1346 1347void 1348StmtPrinter::VisitCXXUnresolvedConstructExpr( 1349 CXXUnresolvedConstructExpr *Node) { 1350 OS << Node->getTypeAsWritten().getAsString(Policy); 1351 OS << "("; 1352 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1353 ArgEnd = Node->arg_end(); 1354 Arg != ArgEnd; ++Arg) { 1355 if (Arg != Node->arg_begin()) 1356 OS << ", "; 1357 PrintExpr(*Arg); 1358 } 1359 OS << ")"; 1360} 1361 1362void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1363 CXXDependentScopeMemberExpr *Node) { 1364 if (!Node->isImplicitAccess()) { 1365 PrintExpr(Node->getBase()); 1366 OS << (Node->isArrow() ? "->" : "."); 1367 } 1368 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1369 Qualifier->print(OS, Policy); 1370 else if (Node->hasExplicitTemplateArgs()) 1371 // FIXME: Track use of "template" keyword explicitly? 1372 OS << "template "; 1373 1374 OS << Node->getMemberNameInfo(); 1375 1376 if (Node->hasExplicitTemplateArgs()) { 1377 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1378 Node->getTemplateArgs(), 1379 Node->getNumTemplateArgs(), 1380 Policy); 1381 } 1382} 1383 1384void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1385 if (!Node->isImplicitAccess()) { 1386 PrintExpr(Node->getBase()); 1387 OS << (Node->isArrow() ? "->" : "."); 1388 } 1389 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1390 Qualifier->print(OS, Policy); 1391 1392 // FIXME: this might originally have been written with 'template' 1393 1394 OS << Node->getMemberNameInfo(); 1395 1396 if (Node->hasExplicitTemplateArgs()) { 1397 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1398 Node->getTemplateArgs(), 1399 Node->getNumTemplateArgs(), 1400 Policy); 1401 } 1402} 1403 1404static const char *getTypeTraitName(UnaryTypeTrait UTT) { 1405 switch (UTT) { 1406 case UTT_HasNothrowAssign: return "__has_nothrow_assign"; 1407 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; 1408 case UTT_HasNothrowCopy: return "__has_nothrow_copy"; 1409 case UTT_HasTrivialAssign: return "__has_trivial_assign"; 1410 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor"; 1411 case UTT_HasTrivialCopy: return "__has_trivial_copy"; 1412 case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; 1413 case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; 1414 case UTT_IsAbstract: return "__is_abstract"; 1415 case UTT_IsArithmetic: return "__is_arithmetic"; 1416 case UTT_IsArray: return "__is_array"; 1417 case UTT_IsClass: return "__is_class"; 1418 case UTT_IsCompleteType: return "__is_complete_type"; 1419 case UTT_IsCompound: return "__is_compound"; 1420 case UTT_IsConst: return "__is_const"; 1421 case UTT_IsEmpty: return "__is_empty"; 1422 case UTT_IsEnum: return "__is_enum"; 1423 case UTT_IsFinal: return "__is_final"; 1424 case UTT_IsFloatingPoint: return "__is_floating_point"; 1425 case UTT_IsFunction: return "__is_function"; 1426 case UTT_IsFundamental: return "__is_fundamental"; 1427 case UTT_IsIntegral: return "__is_integral"; 1428 case UTT_IsLiteral: return "__is_literal"; 1429 case UTT_IsLvalueReference: return "__is_lvalue_reference"; 1430 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer"; 1431 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer"; 1432 case UTT_IsMemberPointer: return "__is_member_pointer"; 1433 case UTT_IsObject: return "__is_object"; 1434 case UTT_IsPOD: return "__is_pod"; 1435 case UTT_IsPointer: return "__is_pointer"; 1436 case UTT_IsPolymorphic: return "__is_polymorphic"; 1437 case UTT_IsReference: return "__is_reference"; 1438 case UTT_IsRvalueReference: return "__is_rvalue_reference"; 1439 case UTT_IsScalar: return "__is_scalar"; 1440 case UTT_IsSigned: return "__is_signed"; 1441 case UTT_IsStandardLayout: return "__is_standard_layout"; 1442 case UTT_IsTrivial: return "__is_trivial"; 1443 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable"; 1444 case UTT_IsUnion: return "__is_union"; 1445 case UTT_IsUnsigned: return "__is_unsigned"; 1446 case UTT_IsVoid: return "__is_void"; 1447 case UTT_IsVolatile: return "__is_volatile"; 1448 } 1449 llvm_unreachable("Type trait not covered by switch statement"); 1450} 1451 1452static const char *getTypeTraitName(BinaryTypeTrait BTT) { 1453 switch (BTT) { 1454 case BTT_IsBaseOf: return "__is_base_of"; 1455 case BTT_IsConvertible: return "__is_convertible"; 1456 case BTT_IsSame: return "__is_same"; 1457 case BTT_TypeCompatible: return "__builtin_types_compatible_p"; 1458 case BTT_IsConvertibleTo: return "__is_convertible_to"; 1459 } 1460 llvm_unreachable("Binary type trait not covered by switch"); 1461} 1462 1463static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1464 switch (ATT) { 1465 case ATT_ArrayRank: return "__array_rank"; 1466 case ATT_ArrayExtent: return "__array_extent"; 1467 } 1468 llvm_unreachable("Array type trait not covered by switch"); 1469} 1470 1471static const char *getExpressionTraitName(ExpressionTrait ET) { 1472 switch (ET) { 1473 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1474 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1475 } 1476 llvm_unreachable("Expression type trait not covered by switch"); 1477} 1478 1479void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 1480 OS << getTypeTraitName(E->getTrait()) << "(" 1481 << E->getQueriedType().getAsString(Policy) << ")"; 1482} 1483 1484void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 1485 OS << getTypeTraitName(E->getTrait()) << "(" 1486 << E->getLhsType().getAsString(Policy) << "," 1487 << E->getRhsType().getAsString(Policy) << ")"; 1488} 1489 1490void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1491 OS << getTypeTraitName(E->getTrait()) << "(" 1492 << E->getQueriedType().getAsString(Policy) << ")"; 1493} 1494 1495void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1496 OS << getExpressionTraitName(E->getTrait()) << "("; 1497 PrintExpr(E->getQueriedExpression()); 1498 OS << ")"; 1499} 1500 1501void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1502 OS << "noexcept("; 1503 PrintExpr(E->getOperand()); 1504 OS << ")"; 1505} 1506 1507void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1508 PrintExpr(E->getPattern()); 1509 OS << "..."; 1510} 1511 1512void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1513 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")"; 1514} 1515 1516void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1517 SubstNonTypeTemplateParmPackExpr *Node) { 1518 OS << Node->getParameterPack()->getNameAsString(); 1519} 1520 1521void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1522 SubstNonTypeTemplateParmExpr *Node) { 1523 Visit(Node->getReplacement()); 1524} 1525 1526void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1527 PrintExpr(Node->GetTemporaryExpr()); 1528} 1529 1530// Obj-C 1531 1532void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1533 OS << "@"; 1534 VisitStringLiteral(Node->getString()); 1535} 1536 1537void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1538 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')'; 1539} 1540 1541void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1542 OS << "@selector(" << Node->getSelector().getAsString() << ')'; 1543} 1544 1545void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1546 OS << "@protocol(" << *Node->getProtocol() << ')'; 1547} 1548 1549void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1550 OS << "["; 1551 switch (Mess->getReceiverKind()) { 1552 case ObjCMessageExpr::Instance: 1553 PrintExpr(Mess->getInstanceReceiver()); 1554 break; 1555 1556 case ObjCMessageExpr::Class: 1557 OS << Mess->getClassReceiver().getAsString(Policy); 1558 break; 1559 1560 case ObjCMessageExpr::SuperInstance: 1561 case ObjCMessageExpr::SuperClass: 1562 OS << "Super"; 1563 break; 1564 } 1565 1566 OS << ' '; 1567 Selector selector = Mess->getSelector(); 1568 if (selector.isUnarySelector()) { 1569 OS << selector.getNameForSlot(0); 1570 } else { 1571 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1572 if (i < selector.getNumArgs()) { 1573 if (i > 0) OS << ' '; 1574 if (selector.getIdentifierInfoForSlot(i)) 1575 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1576 else 1577 OS << ":"; 1578 } 1579 else OS << ", "; // Handle variadic methods. 1580 1581 PrintExpr(Mess->getArg(i)); 1582 } 1583 } 1584 OS << "]"; 1585} 1586 1587void 1588StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 1589 PrintExpr(E->getSubExpr()); 1590} 1591 1592void 1593StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 1594 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy) 1595 << ")"; 1596 PrintExpr(E->getSubExpr()); 1597} 1598 1599void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1600 BlockDecl *BD = Node->getBlockDecl(); 1601 OS << "^"; 1602 1603 const FunctionType *AFT = Node->getFunctionType(); 1604 1605 if (isa<FunctionNoProtoType>(AFT)) { 1606 OS << "()"; 1607 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1608 OS << '('; 1609 std::string ParamStr; 1610 for (BlockDecl::param_iterator AI = BD->param_begin(), 1611 E = BD->param_end(); AI != E; ++AI) { 1612 if (AI != BD->param_begin()) OS << ", "; 1613 ParamStr = (*AI)->getNameAsString(); 1614 (*AI)->getType().getAsStringInternal(ParamStr, Policy); 1615 OS << ParamStr; 1616 } 1617 1618 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1619 if (FT->isVariadic()) { 1620 if (!BD->param_empty()) OS << ", "; 1621 OS << "..."; 1622 } 1623 OS << ')'; 1624 } 1625} 1626 1627void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) { 1628 OS << *Node->getDecl(); 1629} 1630 1631void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 1632 PrintExpr(Node->getSourceExpr()); 1633} 1634 1635void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 1636 OS << "__builtin_astype("; 1637 PrintExpr(Node->getSrcExpr()); 1638 OS << ", " << Node->getType().getAsString(); 1639 OS << ")"; 1640} 1641 1642//===----------------------------------------------------------------------===// 1643// Stmt method implementations 1644//===----------------------------------------------------------------------===// 1645 1646void Stmt::dumpPretty(ASTContext& Context) const { 1647 printPretty(llvm::errs(), Context, 0, 1648 PrintingPolicy(Context.getLangOptions())); 1649} 1650 1651void Stmt::printPretty(raw_ostream &OS, ASTContext& Context, 1652 PrinterHelper* Helper, 1653 const PrintingPolicy &Policy, 1654 unsigned Indentation) const { 1655 if (this == 0) { 1656 OS << "<NULL>"; 1657 return; 1658 } 1659 1660 if (Policy.Dump && &Context) { 1661 dump(OS, Context.getSourceManager()); 1662 return; 1663 } 1664 1665 StmtPrinter P(OS, Context, Helper, Policy, Indentation); 1666 P.Visit(const_cast<Stmt*>(this)); 1667} 1668 1669//===----------------------------------------------------------------------===// 1670// PrinterHelper 1671//===----------------------------------------------------------------------===// 1672 1673// Implement virtual destructor. 1674PrinterHelper::~PrinterHelper() {} 1675