StmtPrinter.cpp revision f4b88a45902af1802a1cb42ba48b1c474474f228
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 "clang/AST/Expr.h" 21#include "clang/AST/ExprCXX.h" 22#include "llvm/ADT/SmallString.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 if (Node->hasTemplateKeyword()) 548 OS << "template "; 549 OS << Node->getNameInfo(); 550 if (Node->hasExplicitTemplateArgs()) 551 OS << TemplateSpecializationType::PrintTemplateArgumentList( 552 Node->getTemplateArgs(), 553 Node->getNumTemplateArgs(), 554 Policy); 555} 556 557void StmtPrinter::VisitDependentScopeDeclRefExpr( 558 DependentScopeDeclRefExpr *Node) { 559 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 560 Qualifier->print(OS, Policy); 561 if (Node->hasTemplateKeyword()) 562 OS << "template "; 563 OS << Node->getNameInfo(); 564 if (Node->hasExplicitTemplateArgs()) 565 OS << TemplateSpecializationType::PrintTemplateArgumentList( 566 Node->getTemplateArgs(), 567 Node->getNumTemplateArgs(), 568 Policy); 569} 570 571void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 572 if (Node->getQualifier()) 573 Node->getQualifier()->print(OS, Policy); 574 if (Node->hasTemplateKeyword()) 575 OS << "template "; 576 OS << Node->getNameInfo(); 577 if (Node->hasExplicitTemplateArgs()) 578 OS << TemplateSpecializationType::PrintTemplateArgumentList( 579 Node->getTemplateArgs(), 580 Node->getNumTemplateArgs(), 581 Policy); 582} 583 584void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 585 if (Node->getBase()) { 586 PrintExpr(Node->getBase()); 587 OS << (Node->isArrow() ? "->" : "."); 588 } 589 OS << *Node->getDecl(); 590} 591 592void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 593 if (Node->isSuperReceiver()) 594 OS << "super."; 595 else if (Node->getBase()) { 596 PrintExpr(Node->getBase()); 597 OS << "."; 598 } 599 600 if (Node->isImplicitProperty()) 601 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString(); 602 else 603 OS << Node->getExplicitProperty()->getName(); 604} 605 606void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 607 608 PrintExpr(Node->getBaseExpr()); 609 OS << "["; 610 PrintExpr(Node->getKeyExpr()); 611 OS << "]"; 612} 613 614void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 615 switch (Node->getIdentType()) { 616 default: 617 llvm_unreachable("unknown case"); 618 case PredefinedExpr::Func: 619 OS << "__func__"; 620 break; 621 case PredefinedExpr::Function: 622 OS << "__FUNCTION__"; 623 break; 624 case PredefinedExpr::PrettyFunction: 625 OS << "__PRETTY_FUNCTION__"; 626 break; 627 } 628} 629 630void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 631 unsigned value = Node->getValue(); 632 633 switch (Node->getKind()) { 634 case CharacterLiteral::Ascii: break; // no prefix. 635 case CharacterLiteral::Wide: OS << 'L'; break; 636 case CharacterLiteral::UTF16: OS << 'u'; break; 637 case CharacterLiteral::UTF32: OS << 'U'; break; 638 } 639 640 switch (value) { 641 case '\\': 642 OS << "'\\\\'"; 643 break; 644 case '\'': 645 OS << "'\\''"; 646 break; 647 case '\a': 648 // TODO: K&R: the meaning of '\\a' is different in traditional C 649 OS << "'\\a'"; 650 break; 651 case '\b': 652 OS << "'\\b'"; 653 break; 654 // Nonstandard escape sequence. 655 /*case '\e': 656 OS << "'\\e'"; 657 break;*/ 658 case '\f': 659 OS << "'\\f'"; 660 break; 661 case '\n': 662 OS << "'\\n'"; 663 break; 664 case '\r': 665 OS << "'\\r'"; 666 break; 667 case '\t': 668 OS << "'\\t'"; 669 break; 670 case '\v': 671 OS << "'\\v'"; 672 break; 673 default: 674 if (value < 256 && isprint(value)) { 675 OS << "'" << (char)value << "'"; 676 } else if (value < 256) { 677 OS << "'\\x"; 678 OS.write_hex(value) << "'"; 679 } else { 680 // FIXME what to really do here? 681 OS << value; 682 } 683 } 684} 685 686void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 687 bool isSigned = Node->getType()->isSignedIntegerType(); 688 OS << Node->getValue().toString(10, isSigned); 689 690 // Emit suffixes. Integer literals are always a builtin integer type. 691 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 692 default: llvm_unreachable("Unexpected type for integer literal!"); 693 // FIXME: The Short and UShort cases are to handle cases where a short 694 // integeral literal is formed during template instantiation. They should 695 // be removed when template instantiation no longer needs integer literals. 696 case BuiltinType::Short: 697 case BuiltinType::UShort: 698 case BuiltinType::Int: break; // no suffix. 699 case BuiltinType::UInt: OS << 'U'; break; 700 case BuiltinType::Long: OS << 'L'; break; 701 case BuiltinType::ULong: OS << "UL"; break; 702 case BuiltinType::LongLong: OS << "LL"; break; 703 case BuiltinType::ULongLong: OS << "ULL"; break; 704 case BuiltinType::Int128: OS << "i128"; break; 705 case BuiltinType::UInt128: OS << "Ui128"; break; 706 } 707} 708void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 709 SmallString<16> Str; 710 Node->getValue().toString(Str); 711 OS << Str; 712} 713 714void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 715 PrintExpr(Node->getSubExpr()); 716 OS << "i"; 717} 718 719void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 720 switch (Str->getKind()) { 721 case StringLiteral::Ascii: break; // no prefix. 722 case StringLiteral::Wide: OS << 'L'; break; 723 case StringLiteral::UTF8: OS << "u8"; break; 724 case StringLiteral::UTF16: OS << 'u'; break; 725 case StringLiteral::UTF32: OS << 'U'; break; 726 } 727 OS << '"'; 728 static char Hex[] = "0123456789ABCDEF"; 729 730 for (unsigned I = 0, N = Str->getLength(); I != N; ++I) { 731 switch (uint32_t Char = Str->getCodeUnit(I)) { 732 default: 733 // FIXME: Is this the best way to print wchar_t? 734 if (Char > 0xff) { 735 assert(Char <= 0x10ffff && "invalid unicode codepoint"); 736 if (Char > 0xffff) 737 OS << "\\U00" 738 << Hex[(Char >> 20) & 15] 739 << Hex[(Char >> 16) & 15]; 740 else 741 OS << "\\u"; 742 OS << Hex[(Char >> 12) & 15] 743 << Hex[(Char >> 8) & 15] 744 << Hex[(Char >> 4) & 15] 745 << Hex[(Char >> 0) & 15]; 746 break; 747 } 748 if (Char <= 0xff && isprint(Char)) 749 OS << (char)Char; 750 else // Output anything hard as an octal escape. 751 OS << '\\' 752 << (char)('0'+ ((Char >> 6) & 7)) 753 << (char)('0'+ ((Char >> 3) & 7)) 754 << (char)('0'+ ((Char >> 0) & 7)); 755 break; 756 // Handle some common non-printable cases to make dumps prettier. 757 case '\\': OS << "\\\\"; break; 758 case '"': OS << "\\\""; break; 759 case '\n': OS << "\\n"; break; 760 case '\t': OS << "\\t"; break; 761 case '\a': OS << "\\a"; break; 762 case '\b': OS << "\\b"; break; 763 } 764 } 765 OS << '"'; 766} 767void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 768 OS << "("; 769 PrintExpr(Node->getSubExpr()); 770 OS << ")"; 771} 772void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 773 if (!Node->isPostfix()) { 774 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 775 776 // Print a space if this is an "identifier operator" like __real, or if 777 // it might be concatenated incorrectly like '+'. 778 switch (Node->getOpcode()) { 779 default: break; 780 case UO_Real: 781 case UO_Imag: 782 case UO_Extension: 783 OS << ' '; 784 break; 785 case UO_Plus: 786 case UO_Minus: 787 if (isa<UnaryOperator>(Node->getSubExpr())) 788 OS << ' '; 789 break; 790 } 791 } 792 PrintExpr(Node->getSubExpr()); 793 794 if (Node->isPostfix()) 795 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 796} 797 798void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 799 OS << "__builtin_offsetof("; 800 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", "; 801 bool PrintedSomething = false; 802 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 803 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 804 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 805 // Array node 806 OS << "["; 807 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 808 OS << "]"; 809 PrintedSomething = true; 810 continue; 811 } 812 813 // Skip implicit base indirections. 814 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 815 continue; 816 817 // Field or identifier node. 818 IdentifierInfo *Id = ON.getFieldName(); 819 if (!Id) 820 continue; 821 822 if (PrintedSomething) 823 OS << "."; 824 else 825 PrintedSomething = true; 826 OS << Id->getName(); 827 } 828 OS << ")"; 829} 830 831void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 832 switch(Node->getKind()) { 833 case UETT_SizeOf: 834 OS << "sizeof"; 835 break; 836 case UETT_AlignOf: 837 OS << "__alignof"; 838 break; 839 case UETT_VecStep: 840 OS << "vec_step"; 841 break; 842 } 843 if (Node->isArgumentType()) 844 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")"; 845 else { 846 OS << " "; 847 PrintExpr(Node->getArgumentExpr()); 848 } 849} 850 851void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 852 OS << "_Generic("; 853 PrintExpr(Node->getControllingExpr()); 854 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 855 OS << ", "; 856 QualType T = Node->getAssocType(i); 857 if (T.isNull()) 858 OS << "default"; 859 else 860 OS << T.getAsString(Policy); 861 OS << ": "; 862 PrintExpr(Node->getAssocExpr(i)); 863 } 864 OS << ")"; 865} 866 867void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 868 PrintExpr(Node->getLHS()); 869 OS << "["; 870 PrintExpr(Node->getRHS()); 871 OS << "]"; 872} 873 874void StmtPrinter::PrintCallArgs(CallExpr *Call) { 875 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 876 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 877 // Don't print any defaulted arguments 878 break; 879 } 880 881 if (i) OS << ", "; 882 PrintExpr(Call->getArg(i)); 883 } 884} 885 886void StmtPrinter::VisitCallExpr(CallExpr *Call) { 887 PrintExpr(Call->getCallee()); 888 OS << "("; 889 PrintCallArgs(Call); 890 OS << ")"; 891} 892void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 893 // FIXME: Suppress printing implicit bases (like "this") 894 PrintExpr(Node->getBase()); 895 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 896 if (FD->isAnonymousStructOrUnion()) 897 return; 898 OS << (Node->isArrow() ? "->" : "."); 899 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 900 Qualifier->print(OS, Policy); 901 if (Node->hasTemplateKeyword()) 902 OS << "template "; 903 OS << Node->getMemberNameInfo(); 904 if (Node->hasExplicitTemplateArgs()) 905 OS << TemplateSpecializationType::PrintTemplateArgumentList( 906 Node->getTemplateArgs(), 907 Node->getNumTemplateArgs(), 908 Policy); 909} 910void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 911 PrintExpr(Node->getBase()); 912 OS << (Node->isArrow() ? "->isa" : ".isa"); 913} 914 915void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 916 PrintExpr(Node->getBase()); 917 OS << "."; 918 OS << Node->getAccessor().getName(); 919} 920void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 921 OS << "(" << Node->getType().getAsString(Policy) << ")"; 922 PrintExpr(Node->getSubExpr()); 923} 924void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 925 OS << "(" << Node->getType().getAsString(Policy) << ")"; 926 PrintExpr(Node->getInitializer()); 927} 928void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 929 // No need to print anything, simply forward to the sub expression. 930 PrintExpr(Node->getSubExpr()); 931} 932void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 933 PrintExpr(Node->getLHS()); 934 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 935 PrintExpr(Node->getRHS()); 936} 937void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 938 PrintExpr(Node->getLHS()); 939 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 940 PrintExpr(Node->getRHS()); 941} 942void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 943 PrintExpr(Node->getCond()); 944 OS << " ? "; 945 PrintExpr(Node->getLHS()); 946 OS << " : "; 947 PrintExpr(Node->getRHS()); 948} 949 950// GNU extensions. 951 952void 953StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 954 PrintExpr(Node->getCommon()); 955 OS << " ?: "; 956 PrintExpr(Node->getFalseExpr()); 957} 958void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 959 OS << "&&" << Node->getLabel()->getName(); 960} 961 962void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 963 OS << "("; 964 PrintRawCompoundStmt(E->getSubStmt()); 965 OS << ")"; 966} 967 968void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 969 OS << "__builtin_choose_expr("; 970 PrintExpr(Node->getCond()); 971 OS << ", "; 972 PrintExpr(Node->getLHS()); 973 OS << ", "; 974 PrintExpr(Node->getRHS()); 975 OS << ")"; 976} 977 978void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 979 OS << "__null"; 980} 981 982void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 983 OS << "__builtin_shufflevector("; 984 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 985 if (i) OS << ", "; 986 PrintExpr(Node->getExpr(i)); 987 } 988 OS << ")"; 989} 990 991void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 992 if (Node->getSyntacticForm()) { 993 Visit(Node->getSyntacticForm()); 994 return; 995 } 996 997 OS << "{ "; 998 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 999 if (i) OS << ", "; 1000 if (Node->getInit(i)) 1001 PrintExpr(Node->getInit(i)); 1002 else 1003 OS << "0"; 1004 } 1005 OS << " }"; 1006} 1007 1008void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1009 OS << "( "; 1010 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1011 if (i) OS << ", "; 1012 PrintExpr(Node->getExpr(i)); 1013 } 1014 OS << " )"; 1015} 1016 1017void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1018 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1019 DEnd = Node->designators_end(); 1020 D != DEnd; ++D) { 1021 if (D->isFieldDesignator()) { 1022 if (D->getDotLoc().isInvalid()) 1023 OS << D->getFieldName()->getName() << ":"; 1024 else 1025 OS << "." << D->getFieldName()->getName(); 1026 } else { 1027 OS << "["; 1028 if (D->isArrayDesignator()) { 1029 PrintExpr(Node->getArrayIndex(*D)); 1030 } else { 1031 PrintExpr(Node->getArrayRangeStart(*D)); 1032 OS << " ... "; 1033 PrintExpr(Node->getArrayRangeEnd(*D)); 1034 } 1035 OS << "]"; 1036 } 1037 } 1038 1039 OS << " = "; 1040 PrintExpr(Node->getInit()); 1041} 1042 1043void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1044 if (Policy.LangOpts.CPlusPlus) 1045 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; 1046 else { 1047 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; 1048 if (Node->getType()->isRecordType()) 1049 OS << "{}"; 1050 else 1051 OS << 0; 1052 } 1053} 1054 1055void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1056 OS << "__builtin_va_arg("; 1057 PrintExpr(Node->getSubExpr()); 1058 OS << ", "; 1059 OS << Node->getType().getAsString(Policy); 1060 OS << ")"; 1061} 1062 1063void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1064 PrintExpr(Node->getSyntacticForm()); 1065} 1066 1067void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1068 const char *Name = 0; 1069 switch (Node->getOp()) { 1070 case AtomicExpr::Init: 1071 Name = "__atomic_init("; 1072 break; 1073 case AtomicExpr::Load: 1074 Name = "__atomic_load("; 1075 break; 1076 case AtomicExpr::Store: 1077 Name = "__atomic_store("; 1078 break; 1079 case AtomicExpr::CmpXchgStrong: 1080 Name = "__atomic_compare_exchange_strong("; 1081 break; 1082 case AtomicExpr::CmpXchgWeak: 1083 Name = "__atomic_compare_exchange_weak("; 1084 break; 1085 case AtomicExpr::Xchg: 1086 Name = "__atomic_exchange("; 1087 break; 1088 case AtomicExpr::Add: 1089 Name = "__atomic_fetch_add("; 1090 break; 1091 case AtomicExpr::Sub: 1092 Name = "__atomic_fetch_sub("; 1093 break; 1094 case AtomicExpr::And: 1095 Name = "__atomic_fetch_and("; 1096 break; 1097 case AtomicExpr::Or: 1098 Name = "__atomic_fetch_or("; 1099 break; 1100 case AtomicExpr::Xor: 1101 Name = "__atomic_fetch_xor("; 1102 break; 1103 } 1104 OS << Name; 1105 PrintExpr(Node->getPtr()); 1106 OS << ", "; 1107 if (Node->getOp() != AtomicExpr::Load) { 1108 PrintExpr(Node->getVal1()); 1109 OS << ", "; 1110 } 1111 if (Node->isCmpXChg()) { 1112 PrintExpr(Node->getVal2()); 1113 OS << ", "; 1114 } 1115 if (Node->getOp() != AtomicExpr::Init) 1116 PrintExpr(Node->getOrder()); 1117 if (Node->isCmpXChg()) { 1118 OS << ", "; 1119 PrintExpr(Node->getOrderFail()); 1120 } 1121 OS << ")"; 1122} 1123 1124// C++ 1125void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1126 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1127 "", 1128#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1129 Spelling, 1130#include "clang/Basic/OperatorKinds.def" 1131 }; 1132 1133 OverloadedOperatorKind Kind = Node->getOperator(); 1134 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1135 if (Node->getNumArgs() == 1) { 1136 OS << OpStrings[Kind] << ' '; 1137 PrintExpr(Node->getArg(0)); 1138 } else { 1139 PrintExpr(Node->getArg(0)); 1140 OS << ' ' << OpStrings[Kind]; 1141 } 1142 } else if (Kind == OO_Call) { 1143 PrintExpr(Node->getArg(0)); 1144 OS << '('; 1145 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1146 if (ArgIdx > 1) 1147 OS << ", "; 1148 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1149 PrintExpr(Node->getArg(ArgIdx)); 1150 } 1151 OS << ')'; 1152 } else if (Kind == OO_Subscript) { 1153 PrintExpr(Node->getArg(0)); 1154 OS << '['; 1155 PrintExpr(Node->getArg(1)); 1156 OS << ']'; 1157 } else if (Node->getNumArgs() == 1) { 1158 OS << OpStrings[Kind] << ' '; 1159 PrintExpr(Node->getArg(0)); 1160 } else if (Node->getNumArgs() == 2) { 1161 PrintExpr(Node->getArg(0)); 1162 OS << ' ' << OpStrings[Kind] << ' '; 1163 PrintExpr(Node->getArg(1)); 1164 } else { 1165 llvm_unreachable("unknown overloaded operator"); 1166 } 1167} 1168 1169void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1170 VisitCallExpr(cast<CallExpr>(Node)); 1171} 1172 1173void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1174 PrintExpr(Node->getCallee()); 1175 OS << "<<<"; 1176 PrintCallArgs(Node->getConfig()); 1177 OS << ">>>("; 1178 PrintCallArgs(Node); 1179 OS << ")"; 1180} 1181 1182void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1183 OS << Node->getCastName() << '<'; 1184 OS << Node->getTypeAsWritten().getAsString(Policy) << ">("; 1185 PrintExpr(Node->getSubExpr()); 1186 OS << ")"; 1187} 1188 1189void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1190 VisitCXXNamedCastExpr(Node); 1191} 1192 1193void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1194 VisitCXXNamedCastExpr(Node); 1195} 1196 1197void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1198 VisitCXXNamedCastExpr(Node); 1199} 1200 1201void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1202 VisitCXXNamedCastExpr(Node); 1203} 1204 1205void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1206 OS << "typeid("; 1207 if (Node->isTypeOperand()) { 1208 OS << Node->getTypeOperand().getAsString(Policy); 1209 } else { 1210 PrintExpr(Node->getExprOperand()); 1211 } 1212 OS << ")"; 1213} 1214 1215void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1216 OS << "__uuidof("; 1217 if (Node->isTypeOperand()) { 1218 OS << Node->getTypeOperand().getAsString(Policy); 1219 } else { 1220 PrintExpr(Node->getExprOperand()); 1221 } 1222 OS << ")"; 1223} 1224 1225void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1226 switch (Node->getLiteralOperatorKind()) { 1227 case UserDefinedLiteral::LOK_Raw: 1228 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1229 break; 1230 case UserDefinedLiteral::LOK_Template: { 1231 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1232 const TemplateArgumentList *Args = 1233 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1234 assert(Args); 1235 const TemplateArgument &Pack = Args->get(0); 1236 for (TemplateArgument::pack_iterator I = Pack.pack_begin(), 1237 E = Pack.pack_end(); I != E; ++I) { 1238 char C = (char)I->getAsIntegral()->getZExtValue(); 1239 OS << C; 1240 } 1241 break; 1242 } 1243 case UserDefinedLiteral::LOK_Integer: { 1244 // Print integer literal without suffix. 1245 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1246 OS << Int->getValue().toString(10, /*isSigned*/false); 1247 break; 1248 } 1249 case UserDefinedLiteral::LOK_Floating: 1250 case UserDefinedLiteral::LOK_String: 1251 case UserDefinedLiteral::LOK_Character: 1252 PrintExpr(Node->getCookedLiteral()); 1253 break; 1254 } 1255 OS << Node->getUDSuffix()->getName(); 1256} 1257 1258void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1259 OS << (Node->getValue() ? "true" : "false"); 1260} 1261 1262void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1263 OS << "nullptr"; 1264} 1265 1266void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1267 OS << "this"; 1268} 1269 1270void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1271 if (Node->getSubExpr() == 0) 1272 OS << "throw"; 1273 else { 1274 OS << "throw "; 1275 PrintExpr(Node->getSubExpr()); 1276 } 1277} 1278 1279void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1280 // Nothing to print: we picked up the default argument 1281} 1282 1283void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1284 OS << Node->getType().getAsString(Policy); 1285 OS << "("; 1286 PrintExpr(Node->getSubExpr()); 1287 OS << ")"; 1288} 1289 1290void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1291 PrintExpr(Node->getSubExpr()); 1292} 1293 1294void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1295 OS << Node->getType().getAsString(Policy); 1296 OS << "("; 1297 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1298 ArgEnd = Node->arg_end(); 1299 Arg != ArgEnd; ++Arg) { 1300 if (Arg != Node->arg_begin()) 1301 OS << ", "; 1302 PrintExpr(*Arg); 1303 } 1304 OS << ")"; 1305} 1306 1307void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1308 OS << '['; 1309 bool NeedComma = false; 1310 switch (Node->getCaptureDefault()) { 1311 case LCD_None: 1312 break; 1313 1314 case LCD_ByCopy: 1315 OS << '='; 1316 NeedComma = true; 1317 break; 1318 1319 case LCD_ByRef: 1320 OS << '&'; 1321 NeedComma = true; 1322 break; 1323 } 1324 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1325 CEnd = Node->explicit_capture_end(); 1326 C != CEnd; 1327 ++C) { 1328 if (NeedComma) 1329 OS << ", "; 1330 NeedComma = true; 1331 1332 switch (C->getCaptureKind()) { 1333 case LCK_This: 1334 OS << "this"; 1335 break; 1336 1337 case LCK_ByRef: 1338 if (Node->getCaptureDefault() != LCD_ByRef) 1339 OS << '&'; 1340 OS << C->getCapturedVar()->getName(); 1341 break; 1342 1343 case LCK_ByCopy: 1344 if (Node->getCaptureDefault() != LCD_ByCopy) 1345 OS << '='; 1346 OS << C->getCapturedVar()->getName(); 1347 break; 1348 } 1349 } 1350 OS << ']'; 1351 1352 if (Node->hasExplicitParameters()) { 1353 OS << " ("; 1354 CXXMethodDecl *Method = Node->getCallOperator(); 1355 NeedComma = false; 1356 for (CXXMethodDecl::param_iterator P = Method->param_begin(), 1357 PEnd = Method->param_end(); 1358 P != PEnd; ++P) { 1359 if (NeedComma) { 1360 OS << ", "; 1361 } else { 1362 NeedComma = true; 1363 } 1364 std::string ParamStr = (*P)->getNameAsString(); 1365 (*P)->getOriginalType().getAsStringInternal(ParamStr, Policy); 1366 OS << ParamStr; 1367 } 1368 if (Method->isVariadic()) { 1369 if (NeedComma) 1370 OS << ", "; 1371 OS << "..."; 1372 } 1373 OS << ')'; 1374 1375 if (Node->isMutable()) 1376 OS << " mutable"; 1377 1378 const FunctionProtoType *Proto 1379 = Method->getType()->getAs<FunctionProtoType>(); 1380 { 1381 std::string ExceptionSpec; 1382 Proto->printExceptionSpecification(ExceptionSpec, Policy); 1383 OS << ExceptionSpec; 1384 } 1385 1386 // FIXME: Attributes 1387 1388 // Print the trailing return type if it was specified in the source. 1389 if (Node->hasExplicitResultType()) 1390 OS << " -> " << Proto->getResultType().getAsString(Policy); 1391 } 1392 1393 // Print the body. 1394 CompoundStmt *Body = Node->getBody(); 1395 OS << ' '; 1396 PrintStmt(Body); 1397} 1398 1399void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1400 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1401 OS << TSInfo->getType().getAsString(Policy) << "()"; 1402 else 1403 OS << Node->getType().getAsString(Policy) << "()"; 1404} 1405 1406void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1407 if (E->isGlobalNew()) 1408 OS << "::"; 1409 OS << "new "; 1410 unsigned NumPlace = E->getNumPlacementArgs(); 1411 if (NumPlace > 0) { 1412 OS << "("; 1413 PrintExpr(E->getPlacementArg(0)); 1414 for (unsigned i = 1; i < NumPlace; ++i) { 1415 OS << ", "; 1416 PrintExpr(E->getPlacementArg(i)); 1417 } 1418 OS << ") "; 1419 } 1420 if (E->isParenTypeId()) 1421 OS << "("; 1422 std::string TypeS; 1423 if (Expr *Size = E->getArraySize()) { 1424 llvm::raw_string_ostream s(TypeS); 1425 Size->printPretty(s, Context, Helper, Policy); 1426 s.flush(); 1427 TypeS = "[" + TypeS + "]"; 1428 } 1429 E->getAllocatedType().getAsStringInternal(TypeS, Policy); 1430 OS << TypeS; 1431 if (E->isParenTypeId()) 1432 OS << ")"; 1433 1434 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1435 if (InitStyle) { 1436 if (InitStyle == CXXNewExpr::CallInit) 1437 OS << "("; 1438 PrintExpr(E->getInitializer()); 1439 if (InitStyle == CXXNewExpr::CallInit) 1440 OS << ")"; 1441 } 1442} 1443 1444void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1445 if (E->isGlobalDelete()) 1446 OS << "::"; 1447 OS << "delete "; 1448 if (E->isArrayForm()) 1449 OS << "[] "; 1450 PrintExpr(E->getArgument()); 1451} 1452 1453void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1454 PrintExpr(E->getBase()); 1455 if (E->isArrow()) 1456 OS << "->"; 1457 else 1458 OS << '.'; 1459 if (E->getQualifier()) 1460 E->getQualifier()->print(OS, Policy); 1461 1462 std::string TypeS; 1463 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1464 OS << II->getName(); 1465 else 1466 E->getDestroyedType().getAsStringInternal(TypeS, Policy); 1467 OS << TypeS; 1468} 1469 1470void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1471 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1472 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1473 // Don't print any defaulted arguments 1474 break; 1475 } 1476 1477 if (i) OS << ", "; 1478 PrintExpr(E->getArg(i)); 1479 } 1480} 1481 1482void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1483 // Just forward to the sub expression. 1484 PrintExpr(E->getSubExpr()); 1485} 1486 1487void 1488StmtPrinter::VisitCXXUnresolvedConstructExpr( 1489 CXXUnresolvedConstructExpr *Node) { 1490 OS << Node->getTypeAsWritten().getAsString(Policy); 1491 OS << "("; 1492 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1493 ArgEnd = Node->arg_end(); 1494 Arg != ArgEnd; ++Arg) { 1495 if (Arg != Node->arg_begin()) 1496 OS << ", "; 1497 PrintExpr(*Arg); 1498 } 1499 OS << ")"; 1500} 1501 1502void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1503 CXXDependentScopeMemberExpr *Node) { 1504 if (!Node->isImplicitAccess()) { 1505 PrintExpr(Node->getBase()); 1506 OS << (Node->isArrow() ? "->" : "."); 1507 } 1508 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1509 Qualifier->print(OS, Policy); 1510 if (Node->hasTemplateKeyword()) 1511 OS << "template "; 1512 OS << Node->getMemberNameInfo(); 1513 if (Node->hasExplicitTemplateArgs()) { 1514 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1515 Node->getTemplateArgs(), 1516 Node->getNumTemplateArgs(), 1517 Policy); 1518 } 1519} 1520 1521void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1522 if (!Node->isImplicitAccess()) { 1523 PrintExpr(Node->getBase()); 1524 OS << (Node->isArrow() ? "->" : "."); 1525 } 1526 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1527 Qualifier->print(OS, Policy); 1528 if (Node->hasTemplateKeyword()) 1529 OS << "template "; 1530 OS << Node->getMemberNameInfo(); 1531 if (Node->hasExplicitTemplateArgs()) { 1532 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1533 Node->getTemplateArgs(), 1534 Node->getNumTemplateArgs(), 1535 Policy); 1536 } 1537} 1538 1539static const char *getTypeTraitName(UnaryTypeTrait UTT) { 1540 switch (UTT) { 1541 case UTT_HasNothrowAssign: return "__has_nothrow_assign"; 1542 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; 1543 case UTT_HasNothrowCopy: return "__has_nothrow_copy"; 1544 case UTT_HasTrivialAssign: return "__has_trivial_assign"; 1545 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor"; 1546 case UTT_HasTrivialCopy: return "__has_trivial_copy"; 1547 case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; 1548 case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; 1549 case UTT_IsAbstract: return "__is_abstract"; 1550 case UTT_IsArithmetic: return "__is_arithmetic"; 1551 case UTT_IsArray: return "__is_array"; 1552 case UTT_IsClass: return "__is_class"; 1553 case UTT_IsCompleteType: return "__is_complete_type"; 1554 case UTT_IsCompound: return "__is_compound"; 1555 case UTT_IsConst: return "__is_const"; 1556 case UTT_IsEmpty: return "__is_empty"; 1557 case UTT_IsEnum: return "__is_enum"; 1558 case UTT_IsFinal: return "__is_final"; 1559 case UTT_IsFloatingPoint: return "__is_floating_point"; 1560 case UTT_IsFunction: return "__is_function"; 1561 case UTT_IsFundamental: return "__is_fundamental"; 1562 case UTT_IsIntegral: return "__is_integral"; 1563 case UTT_IsLiteral: return "__is_literal"; 1564 case UTT_IsLvalueReference: return "__is_lvalue_reference"; 1565 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer"; 1566 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer"; 1567 case UTT_IsMemberPointer: return "__is_member_pointer"; 1568 case UTT_IsObject: return "__is_object"; 1569 case UTT_IsPOD: return "__is_pod"; 1570 case UTT_IsPointer: return "__is_pointer"; 1571 case UTT_IsPolymorphic: return "__is_polymorphic"; 1572 case UTT_IsReference: return "__is_reference"; 1573 case UTT_IsRvalueReference: return "__is_rvalue_reference"; 1574 case UTT_IsScalar: return "__is_scalar"; 1575 case UTT_IsSigned: return "__is_signed"; 1576 case UTT_IsStandardLayout: return "__is_standard_layout"; 1577 case UTT_IsTrivial: return "__is_trivial"; 1578 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable"; 1579 case UTT_IsUnion: return "__is_union"; 1580 case UTT_IsUnsigned: return "__is_unsigned"; 1581 case UTT_IsVoid: return "__is_void"; 1582 case UTT_IsVolatile: return "__is_volatile"; 1583 } 1584 llvm_unreachable("Type trait not covered by switch statement"); 1585} 1586 1587static const char *getTypeTraitName(BinaryTypeTrait BTT) { 1588 switch (BTT) { 1589 case BTT_IsBaseOf: return "__is_base_of"; 1590 case BTT_IsConvertible: return "__is_convertible"; 1591 case BTT_IsSame: return "__is_same"; 1592 case BTT_TypeCompatible: return "__builtin_types_compatible_p"; 1593 case BTT_IsConvertibleTo: return "__is_convertible_to"; 1594 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable"; 1595 } 1596 llvm_unreachable("Binary type trait not covered by switch"); 1597} 1598 1599static const char *getTypeTraitName(TypeTrait TT) { 1600 switch (TT) { 1601 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible"; 1602 } 1603 llvm_unreachable("Type trait not covered by switch"); 1604} 1605 1606static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1607 switch (ATT) { 1608 case ATT_ArrayRank: return "__array_rank"; 1609 case ATT_ArrayExtent: return "__array_extent"; 1610 } 1611 llvm_unreachable("Array type trait not covered by switch"); 1612} 1613 1614static const char *getExpressionTraitName(ExpressionTrait ET) { 1615 switch (ET) { 1616 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1617 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1618 } 1619 llvm_unreachable("Expression type trait not covered by switch"); 1620} 1621 1622void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 1623 OS << getTypeTraitName(E->getTrait()) << "(" 1624 << E->getQueriedType().getAsString(Policy) << ")"; 1625} 1626 1627void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 1628 OS << getTypeTraitName(E->getTrait()) << "(" 1629 << E->getLhsType().getAsString(Policy) << "," 1630 << E->getRhsType().getAsString(Policy) << ")"; 1631} 1632 1633void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 1634 OS << getTypeTraitName(E->getTrait()) << "("; 1635 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 1636 if (I > 0) 1637 OS << ", "; 1638 OS << E->getArg(I)->getType().getAsString(Policy); 1639 } 1640 OS << ")"; 1641} 1642 1643void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1644 OS << getTypeTraitName(E->getTrait()) << "(" 1645 << E->getQueriedType().getAsString(Policy) << ")"; 1646} 1647 1648void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1649 OS << getExpressionTraitName(E->getTrait()) << "("; 1650 PrintExpr(E->getQueriedExpression()); 1651 OS << ")"; 1652} 1653 1654void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1655 OS << "noexcept("; 1656 PrintExpr(E->getOperand()); 1657 OS << ")"; 1658} 1659 1660void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1661 PrintExpr(E->getPattern()); 1662 OS << "..."; 1663} 1664 1665void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1666 OS << "sizeof...(" << *E->getPack() << ")"; 1667} 1668 1669void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1670 SubstNonTypeTemplateParmPackExpr *Node) { 1671 OS << *Node->getParameterPack(); 1672} 1673 1674void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1675 SubstNonTypeTemplateParmExpr *Node) { 1676 Visit(Node->getReplacement()); 1677} 1678 1679void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1680 PrintExpr(Node->GetTemporaryExpr()); 1681} 1682 1683// Obj-C 1684 1685void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1686 OS << "@"; 1687 VisitStringLiteral(Node->getString()); 1688} 1689 1690void StmtPrinter::VisitObjCNumericLiteral(ObjCNumericLiteral *E) { 1691 OS << "@"; 1692 Visit(E->getNumber()); 1693} 1694 1695void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 1696 OS << "@[ "; 1697 StmtRange ch = E->children(); 1698 if (ch.first != ch.second) { 1699 while (1) { 1700 Visit(*ch.first); 1701 ++ch.first; 1702 if (ch.first == ch.second) break; 1703 OS << ", "; 1704 } 1705 } 1706 OS << " ]"; 1707} 1708 1709void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 1710 OS << "@{ "; 1711 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 1712 if (I > 0) 1713 OS << ", "; 1714 1715 ObjCDictionaryElement Element = E->getKeyValueElement(I); 1716 Visit(Element.Key); 1717 OS << " : "; 1718 Visit(Element.Value); 1719 if (Element.isPackExpansion()) 1720 OS << "..."; 1721 } 1722 OS << " }"; 1723} 1724 1725void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1726 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')'; 1727} 1728 1729void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1730 OS << "@selector(" << Node->getSelector().getAsString() << ')'; 1731} 1732 1733void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1734 OS << "@protocol(" << *Node->getProtocol() << ')'; 1735} 1736 1737void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1738 OS << "["; 1739 switch (Mess->getReceiverKind()) { 1740 case ObjCMessageExpr::Instance: 1741 PrintExpr(Mess->getInstanceReceiver()); 1742 break; 1743 1744 case ObjCMessageExpr::Class: 1745 OS << Mess->getClassReceiver().getAsString(Policy); 1746 break; 1747 1748 case ObjCMessageExpr::SuperInstance: 1749 case ObjCMessageExpr::SuperClass: 1750 OS << "Super"; 1751 break; 1752 } 1753 1754 OS << ' '; 1755 Selector selector = Mess->getSelector(); 1756 if (selector.isUnarySelector()) { 1757 OS << selector.getNameForSlot(0); 1758 } else { 1759 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1760 if (i < selector.getNumArgs()) { 1761 if (i > 0) OS << ' '; 1762 if (selector.getIdentifierInfoForSlot(i)) 1763 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1764 else 1765 OS << ":"; 1766 } 1767 else OS << ", "; // Handle variadic methods. 1768 1769 PrintExpr(Mess->getArg(i)); 1770 } 1771 } 1772 OS << "]"; 1773} 1774 1775void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 1776 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1777} 1778 1779void 1780StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 1781 PrintExpr(E->getSubExpr()); 1782} 1783 1784void 1785StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 1786 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy) 1787 << ")"; 1788 PrintExpr(E->getSubExpr()); 1789} 1790 1791void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1792 BlockDecl *BD = Node->getBlockDecl(); 1793 OS << "^"; 1794 1795 const FunctionType *AFT = Node->getFunctionType(); 1796 1797 if (isa<FunctionNoProtoType>(AFT)) { 1798 OS << "()"; 1799 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1800 OS << '('; 1801 std::string ParamStr; 1802 for (BlockDecl::param_iterator AI = BD->param_begin(), 1803 E = BD->param_end(); AI != E; ++AI) { 1804 if (AI != BD->param_begin()) OS << ", "; 1805 ParamStr = (*AI)->getNameAsString(); 1806 (*AI)->getType().getAsStringInternal(ParamStr, Policy); 1807 OS << ParamStr; 1808 } 1809 1810 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1811 if (FT->isVariadic()) { 1812 if (!BD->param_empty()) OS << ", "; 1813 OS << "..."; 1814 } 1815 OS << ')'; 1816 } 1817} 1818 1819void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 1820 PrintExpr(Node->getSourceExpr()); 1821} 1822 1823void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 1824 OS << "__builtin_astype("; 1825 PrintExpr(Node->getSrcExpr()); 1826 OS << ", " << Node->getType().getAsString(); 1827 OS << ")"; 1828} 1829 1830//===----------------------------------------------------------------------===// 1831// Stmt method implementations 1832//===----------------------------------------------------------------------===// 1833 1834void Stmt::dumpPretty(ASTContext& Context) const { 1835 printPretty(llvm::errs(), Context, 0, 1836 PrintingPolicy(Context.getLangOptions())); 1837} 1838 1839void Stmt::printPretty(raw_ostream &OS, ASTContext& Context, 1840 PrinterHelper* Helper, 1841 const PrintingPolicy &Policy, 1842 unsigned Indentation) const { 1843 if (this == 0) { 1844 OS << "<NULL>"; 1845 return; 1846 } 1847 1848 if (Policy.Dump && &Context) { 1849 dump(OS, Context.getSourceManager()); 1850 return; 1851 } 1852 1853 StmtPrinter P(OS, Context, Helper, Policy, Indentation); 1854 P.Visit(const_cast<Stmt*>(this)); 1855} 1856 1857//===----------------------------------------------------------------------===// 1858// PrinterHelper 1859//===----------------------------------------------------------------------===// 1860 1861// Implement virtual destructor. 1862PrinterHelper::~PrinterHelper() {} 1863