StmtPrinter.cpp revision 03e80030515c800d1ab44125b9052dfffd1bd04c
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 llvm::raw_ostream &OS; 32 ASTContext &Context; 33 unsigned IndentLevel; 34 clang::PrinterHelper* Helper; 35 PrintingPolicy Policy; 36 37 public: 38 StmtPrinter(llvm::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 llvm::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 llvm::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::VisitGotoStmt(GotoStmt *Node) { 309 Indent() << "goto " << Node->getLabel()->getName() << ";\n"; 310} 311 312void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 313 Indent() << "goto *"; 314 PrintExpr(Node->getTarget()); 315 OS << ";\n"; 316} 317 318void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 319 Indent() << "continue;\n"; 320} 321 322void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 323 Indent() << "break;\n"; 324} 325 326 327void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 328 Indent() << "return"; 329 if (Node->getRetValue()) { 330 OS << " "; 331 PrintExpr(Node->getRetValue()); 332 } 333 OS << ";\n"; 334} 335 336 337void StmtPrinter::VisitAsmStmt(AsmStmt *Node) { 338 Indent() << "asm "; 339 340 if (Node->isVolatile()) 341 OS << "volatile "; 342 343 OS << "("; 344 VisitStringLiteral(Node->getAsmString()); 345 346 // Outputs 347 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 348 Node->getNumClobbers() != 0) 349 OS << " : "; 350 351 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 352 if (i != 0) 353 OS << ", "; 354 355 if (!Node->getOutputName(i).empty()) { 356 OS << '['; 357 OS << Node->getOutputName(i); 358 OS << "] "; 359 } 360 361 VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 362 OS << " "; 363 Visit(Node->getOutputExpr(i)); 364 } 365 366 // Inputs 367 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 368 OS << " : "; 369 370 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 371 if (i != 0) 372 OS << ", "; 373 374 if (!Node->getInputName(i).empty()) { 375 OS << '['; 376 OS << Node->getInputName(i); 377 OS << "] "; 378 } 379 380 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 381 OS << " "; 382 Visit(Node->getInputExpr(i)); 383 } 384 385 // Clobbers 386 if (Node->getNumClobbers() != 0) 387 OS << " : "; 388 389 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 390 if (i != 0) 391 OS << ", "; 392 393 VisitStringLiteral(Node->getClobber(i)); 394 } 395 396 OS << ");\n"; 397} 398 399void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 400 Indent() << "@try"; 401 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 402 PrintRawCompoundStmt(TS); 403 OS << "\n"; 404 } 405 406 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 407 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 408 Indent() << "@catch("; 409 if (catchStmt->getCatchParamDecl()) { 410 if (Decl *DS = catchStmt->getCatchParamDecl()) 411 PrintRawDecl(DS); 412 } 413 OS << ")"; 414 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 415 PrintRawCompoundStmt(CS); 416 OS << "\n"; 417 } 418 } 419 420 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 421 Node->getFinallyStmt())) { 422 Indent() << "@finally"; 423 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 424 OS << "\n"; 425 } 426} 427 428void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 429} 430 431void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 432 Indent() << "@catch (...) { /* todo */ } \n"; 433} 434 435void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 436 Indent() << "@throw"; 437 if (Node->getThrowExpr()) { 438 OS << " "; 439 PrintExpr(Node->getThrowExpr()); 440 } 441 OS << ";\n"; 442} 443 444void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 445 Indent() << "@synchronized ("; 446 PrintExpr(Node->getSynchExpr()); 447 OS << ")"; 448 PrintRawCompoundStmt(Node->getSynchBody()); 449 OS << "\n"; 450} 451 452void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 453 Indent() << "@autoreleasepool"; 454 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt())); 455 OS << "\n"; 456} 457 458void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 459 OS << "catch ("; 460 if (Decl *ExDecl = Node->getExceptionDecl()) 461 PrintRawDecl(ExDecl); 462 else 463 OS << "..."; 464 OS << ") "; 465 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 466} 467 468void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 469 Indent(); 470 PrintRawCXXCatchStmt(Node); 471 OS << "\n"; 472} 473 474void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 475 Indent() << "try "; 476 PrintRawCompoundStmt(Node->getTryBlock()); 477 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 478 OS << " "; 479 PrintRawCXXCatchStmt(Node->getHandler(i)); 480 } 481 OS << "\n"; 482} 483 484void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 485 Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 486 PrintRawCompoundStmt(Node->getTryBlock()); 487 SEHExceptStmt *E = Node->getExceptHandler(); 488 SEHFinallyStmt *F = Node->getFinallyHandler(); 489 if(E) 490 PrintRawSEHExceptHandler(E); 491 else { 492 assert(F && "Must have a finally block..."); 493 PrintRawSEHFinallyStmt(F); 494 } 495 OS << "\n"; 496} 497 498void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 499 OS << "__finally "; 500 PrintRawCompoundStmt(Node->getBlock()); 501 OS << "\n"; 502} 503 504void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 505 OS << "__except ("; 506 VisitExpr(Node->getFilterExpr()); 507 OS << ")\n"; 508 PrintRawCompoundStmt(Node->getBlock()); 509 OS << "\n"; 510} 511 512void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 513 Indent(); 514 PrintRawSEHExceptHandler(Node); 515 OS << "\n"; 516} 517 518void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 519 Indent(); 520 PrintRawSEHFinallyStmt(Node); 521 OS << "\n"; 522} 523 524//===----------------------------------------------------------------------===// 525// Expr printing methods. 526//===----------------------------------------------------------------------===// 527 528void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 529 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 530 Qualifier->print(OS, Policy); 531 OS << Node->getNameInfo(); 532 if (Node->hasExplicitTemplateArgs()) 533 OS << TemplateSpecializationType::PrintTemplateArgumentList( 534 Node->getTemplateArgs(), 535 Node->getNumTemplateArgs(), 536 Policy); 537} 538 539void StmtPrinter::VisitDependentScopeDeclRefExpr( 540 DependentScopeDeclRefExpr *Node) { 541 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 542 Qualifier->print(OS, Policy); 543 OS << Node->getNameInfo(); 544 if (Node->hasExplicitTemplateArgs()) 545 OS << TemplateSpecializationType::PrintTemplateArgumentList( 546 Node->getTemplateArgs(), 547 Node->getNumTemplateArgs(), 548 Policy); 549} 550 551void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 552 if (Node->getQualifier()) 553 Node->getQualifier()->print(OS, Policy); 554 OS << Node->getNameInfo(); 555 if (Node->hasExplicitTemplateArgs()) 556 OS << TemplateSpecializationType::PrintTemplateArgumentList( 557 Node->getTemplateArgs(), 558 Node->getNumTemplateArgs(), 559 Policy); 560} 561 562void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 563 if (Node->getBase()) { 564 PrintExpr(Node->getBase()); 565 OS << (Node->isArrow() ? "->" : "."); 566 } 567 OS << Node->getDecl(); 568} 569 570void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 571 if (Node->isSuperReceiver()) 572 OS << "super."; 573 else if (Node->getBase()) { 574 PrintExpr(Node->getBase()); 575 OS << "."; 576 } 577 578 if (Node->isImplicitProperty()) 579 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString(); 580 else 581 OS << Node->getExplicitProperty()->getName(); 582} 583 584void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 585 switch (Node->getIdentType()) { 586 default: 587 assert(0 && "unknown case"); 588 case PredefinedExpr::Func: 589 OS << "__func__"; 590 break; 591 case PredefinedExpr::Function: 592 OS << "__FUNCTION__"; 593 break; 594 case PredefinedExpr::PrettyFunction: 595 OS << "__PRETTY_FUNCTION__"; 596 break; 597 } 598} 599 600void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 601 unsigned value = Node->getValue(); 602 if (Node->isWide()) 603 OS << "L"; 604 switch (value) { 605 case '\\': 606 OS << "'\\\\'"; 607 break; 608 case '\'': 609 OS << "'\\''"; 610 break; 611 case '\a': 612 // TODO: K&R: the meaning of '\\a' is different in traditional C 613 OS << "'\\a'"; 614 break; 615 case '\b': 616 OS << "'\\b'"; 617 break; 618 // Nonstandard escape sequence. 619 /*case '\e': 620 OS << "'\\e'"; 621 break;*/ 622 case '\f': 623 OS << "'\\f'"; 624 break; 625 case '\n': 626 OS << "'\\n'"; 627 break; 628 case '\r': 629 OS << "'\\r'"; 630 break; 631 case '\t': 632 OS << "'\\t'"; 633 break; 634 case '\v': 635 OS << "'\\v'"; 636 break; 637 default: 638 if (value < 256 && isprint(value)) { 639 OS << "'" << (char)value << "'"; 640 } else if (value < 256) { 641 OS << "'\\x" << llvm::format("%x", value) << "'"; 642 } else { 643 // FIXME what to really do here? 644 OS << value; 645 } 646 } 647} 648 649void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 650 bool isSigned = Node->getType()->isSignedIntegerType(); 651 OS << Node->getValue().toString(10, isSigned); 652 653 // Emit suffixes. Integer literals are always a builtin integer type. 654 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 655 default: assert(0 && "Unexpected type for integer literal!"); 656 case BuiltinType::Int: break; // no suffix. 657 case BuiltinType::UInt: OS << 'U'; break; 658 case BuiltinType::Long: OS << 'L'; break; 659 case BuiltinType::ULong: OS << "UL"; break; 660 case BuiltinType::LongLong: OS << "LL"; break; 661 case BuiltinType::ULongLong: OS << "ULL"; break; 662 } 663} 664void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 665 // FIXME: print value more precisely. 666 OS << Node->getValueAsApproximateDouble(); 667} 668 669void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 670 PrintExpr(Node->getSubExpr()); 671 OS << "i"; 672} 673 674void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 675 if (Str->isWide()) OS << 'L'; 676 OS << '"'; 677 678 // FIXME: this doesn't print wstrings right. 679 llvm::StringRef StrData = Str->getString(); 680 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end(); 681 I != E; ++I) { 682 unsigned char Char = *I; 683 684 switch (Char) { 685 default: 686 if (isprint(Char)) 687 OS << (char)Char; 688 else // Output anything hard as an octal escape. 689 OS << '\\' 690 << (char)('0'+ ((Char >> 6) & 7)) 691 << (char)('0'+ ((Char >> 3) & 7)) 692 << (char)('0'+ ((Char >> 0) & 7)); 693 break; 694 // Handle some common non-printable cases to make dumps prettier. 695 case '\\': OS << "\\\\"; break; 696 case '"': OS << "\\\""; break; 697 case '\n': OS << "\\n"; break; 698 case '\t': OS << "\\t"; break; 699 case '\a': OS << "\\a"; break; 700 case '\b': OS << "\\b"; break; 701 } 702 } 703 OS << '"'; 704} 705void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 706 OS << "("; 707 PrintExpr(Node->getSubExpr()); 708 OS << ")"; 709} 710void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 711 if (!Node->isPostfix()) { 712 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 713 714 // Print a space if this is an "identifier operator" like __real, or if 715 // it might be concatenated incorrectly like '+'. 716 switch (Node->getOpcode()) { 717 default: break; 718 case UO_Real: 719 case UO_Imag: 720 case UO_Extension: 721 OS << ' '; 722 break; 723 case UO_Plus: 724 case UO_Minus: 725 if (isa<UnaryOperator>(Node->getSubExpr())) 726 OS << ' '; 727 break; 728 } 729 } 730 PrintExpr(Node->getSubExpr()); 731 732 if (Node->isPostfix()) 733 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 734} 735 736void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 737 OS << "__builtin_offsetof("; 738 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", "; 739 bool PrintedSomething = false; 740 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 741 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 742 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 743 // Array node 744 OS << "["; 745 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 746 OS << "]"; 747 PrintedSomething = true; 748 continue; 749 } 750 751 // Skip implicit base indirections. 752 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 753 continue; 754 755 // Field or identifier node. 756 IdentifierInfo *Id = ON.getFieldName(); 757 if (!Id) 758 continue; 759 760 if (PrintedSomething) 761 OS << "."; 762 else 763 PrintedSomething = true; 764 OS << Id->getName(); 765 } 766 OS << ")"; 767} 768 769void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 770 switch(Node->getKind()) { 771 case UETT_SizeOf: 772 OS << "sizeof"; 773 break; 774 case UETT_AlignOf: 775 OS << "__alignof"; 776 break; 777 case UETT_VecStep: 778 OS << "vec_step"; 779 break; 780 } 781 if (Node->isArgumentType()) 782 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")"; 783 else { 784 OS << " "; 785 PrintExpr(Node->getArgumentExpr()); 786 } 787} 788 789void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 790 OS << "_Generic("; 791 PrintExpr(Node->getControllingExpr()); 792 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 793 OS << ", "; 794 QualType T = Node->getAssocType(i); 795 if (T.isNull()) 796 OS << "default"; 797 else 798 OS << T.getAsString(Policy); 799 OS << ": "; 800 PrintExpr(Node->getAssocExpr(i)); 801 } 802 OS << ")"; 803} 804 805void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 806 PrintExpr(Node->getLHS()); 807 OS << "["; 808 PrintExpr(Node->getRHS()); 809 OS << "]"; 810} 811 812void StmtPrinter::PrintCallArgs(CallExpr *Call) { 813 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 814 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 815 // Don't print any defaulted arguments 816 break; 817 } 818 819 if (i) OS << ", "; 820 PrintExpr(Call->getArg(i)); 821 } 822} 823 824void StmtPrinter::VisitCallExpr(CallExpr *Call) { 825 PrintExpr(Call->getCallee()); 826 OS << "("; 827 PrintCallArgs(Call); 828 OS << ")"; 829} 830void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 831 // FIXME: Suppress printing implicit bases (like "this") 832 PrintExpr(Node->getBase()); 833 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 834 if (FD->isAnonymousStructOrUnion()) 835 return; 836 OS << (Node->isArrow() ? "->" : "."); 837 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 838 Qualifier->print(OS, Policy); 839 840 OS << Node->getMemberNameInfo(); 841 842 if (Node->hasExplicitTemplateArgs()) 843 OS << TemplateSpecializationType::PrintTemplateArgumentList( 844 Node->getTemplateArgs(), 845 Node->getNumTemplateArgs(), 846 Policy); 847} 848void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 849 PrintExpr(Node->getBase()); 850 OS << (Node->isArrow() ? "->isa" : ".isa"); 851} 852 853void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 854 PrintExpr(Node->getBase()); 855 OS << "."; 856 OS << Node->getAccessor().getName(); 857} 858void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 859 OS << "(" << Node->getType().getAsString(Policy) << ")"; 860 PrintExpr(Node->getSubExpr()); 861} 862void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 863 OS << "(" << Node->getType().getAsString(Policy) << ")"; 864 PrintExpr(Node->getInitializer()); 865} 866void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 867 // No need to print anything, simply forward to the sub expression. 868 PrintExpr(Node->getSubExpr()); 869} 870void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 871 PrintExpr(Node->getLHS()); 872 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 873 PrintExpr(Node->getRHS()); 874} 875void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 876 PrintExpr(Node->getLHS()); 877 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 878 PrintExpr(Node->getRHS()); 879} 880void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 881 PrintExpr(Node->getCond()); 882 OS << " ? "; 883 PrintExpr(Node->getLHS()); 884 OS << " : "; 885 PrintExpr(Node->getRHS()); 886} 887 888// GNU extensions. 889 890void 891StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 892 PrintExpr(Node->getCommon()); 893 OS << " ?: "; 894 PrintExpr(Node->getFalseExpr()); 895} 896void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 897 OS << "&&" << Node->getLabel()->getName(); 898} 899 900void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 901 OS << "("; 902 PrintRawCompoundStmt(E->getSubStmt()); 903 OS << ")"; 904} 905 906void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 907 OS << "__builtin_choose_expr("; 908 PrintExpr(Node->getCond()); 909 OS << ", "; 910 PrintExpr(Node->getLHS()); 911 OS << ", "; 912 PrintExpr(Node->getRHS()); 913 OS << ")"; 914} 915 916void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 917 OS << "__null"; 918} 919 920void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 921 OS << "__builtin_shufflevector("; 922 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 923 if (i) OS << ", "; 924 PrintExpr(Node->getExpr(i)); 925 } 926 OS << ")"; 927} 928 929void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 930 if (Node->getSyntacticForm()) { 931 Visit(Node->getSyntacticForm()); 932 return; 933 } 934 935 OS << "{ "; 936 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 937 if (i) OS << ", "; 938 if (Node->getInit(i)) 939 PrintExpr(Node->getInit(i)); 940 else 941 OS << "0"; 942 } 943 OS << " }"; 944} 945 946void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 947 OS << "( "; 948 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 949 if (i) OS << ", "; 950 PrintExpr(Node->getExpr(i)); 951 } 952 OS << " )"; 953} 954 955void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 956 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 957 DEnd = Node->designators_end(); 958 D != DEnd; ++D) { 959 if (D->isFieldDesignator()) { 960 if (D->getDotLoc().isInvalid()) 961 OS << D->getFieldName()->getName() << ":"; 962 else 963 OS << "." << D->getFieldName()->getName(); 964 } else { 965 OS << "["; 966 if (D->isArrayDesignator()) { 967 PrintExpr(Node->getArrayIndex(*D)); 968 } else { 969 PrintExpr(Node->getArrayRangeStart(*D)); 970 OS << " ... "; 971 PrintExpr(Node->getArrayRangeEnd(*D)); 972 } 973 OS << "]"; 974 } 975 } 976 977 OS << " = "; 978 PrintExpr(Node->getInit()); 979} 980 981void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 982 if (Policy.LangOpts.CPlusPlus) 983 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; 984 else { 985 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; 986 if (Node->getType()->isRecordType()) 987 OS << "{}"; 988 else 989 OS << 0; 990 } 991} 992 993void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 994 OS << "__builtin_va_arg("; 995 PrintExpr(Node->getSubExpr()); 996 OS << ", "; 997 OS << Node->getType().getAsString(Policy); 998 OS << ")"; 999} 1000 1001// C++ 1002void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1003 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1004 "", 1005#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1006 Spelling, 1007#include "clang/Basic/OperatorKinds.def" 1008 }; 1009 1010 OverloadedOperatorKind Kind = Node->getOperator(); 1011 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1012 if (Node->getNumArgs() == 1) { 1013 OS << OpStrings[Kind] << ' '; 1014 PrintExpr(Node->getArg(0)); 1015 } else { 1016 PrintExpr(Node->getArg(0)); 1017 OS << ' ' << OpStrings[Kind]; 1018 } 1019 } else if (Kind == OO_Call) { 1020 PrintExpr(Node->getArg(0)); 1021 OS << '('; 1022 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1023 if (ArgIdx > 1) 1024 OS << ", "; 1025 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1026 PrintExpr(Node->getArg(ArgIdx)); 1027 } 1028 OS << ')'; 1029 } else if (Kind == OO_Subscript) { 1030 PrintExpr(Node->getArg(0)); 1031 OS << '['; 1032 PrintExpr(Node->getArg(1)); 1033 OS << ']'; 1034 } else if (Node->getNumArgs() == 1) { 1035 OS << OpStrings[Kind] << ' '; 1036 PrintExpr(Node->getArg(0)); 1037 } else if (Node->getNumArgs() == 2) { 1038 PrintExpr(Node->getArg(0)); 1039 OS << ' ' << OpStrings[Kind] << ' '; 1040 PrintExpr(Node->getArg(1)); 1041 } else { 1042 assert(false && "unknown overloaded operator"); 1043 } 1044} 1045 1046void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1047 VisitCallExpr(cast<CallExpr>(Node)); 1048} 1049 1050void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1051 PrintExpr(Node->getCallee()); 1052 OS << "<<<"; 1053 PrintCallArgs(Node->getConfig()); 1054 OS << ">>>("; 1055 PrintCallArgs(Node); 1056 OS << ")"; 1057} 1058 1059void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1060 OS << Node->getCastName() << '<'; 1061 OS << Node->getTypeAsWritten().getAsString(Policy) << ">("; 1062 PrintExpr(Node->getSubExpr()); 1063 OS << ")"; 1064} 1065 1066void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1067 VisitCXXNamedCastExpr(Node); 1068} 1069 1070void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1071 VisitCXXNamedCastExpr(Node); 1072} 1073 1074void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1075 VisitCXXNamedCastExpr(Node); 1076} 1077 1078void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1079 VisitCXXNamedCastExpr(Node); 1080} 1081 1082void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1083 OS << "typeid("; 1084 if (Node->isTypeOperand()) { 1085 OS << Node->getTypeOperand().getAsString(Policy); 1086 } else { 1087 PrintExpr(Node->getExprOperand()); 1088 } 1089 OS << ")"; 1090} 1091 1092void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1093 OS << "__uuidof("; 1094 if (Node->isTypeOperand()) { 1095 OS << Node->getTypeOperand().getAsString(Policy); 1096 } else { 1097 PrintExpr(Node->getExprOperand()); 1098 } 1099 OS << ")"; 1100} 1101 1102void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1103 OS << (Node->getValue() ? "true" : "false"); 1104} 1105 1106void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1107 OS << "nullptr"; 1108} 1109 1110void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1111 OS << "this"; 1112} 1113 1114void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1115 if (Node->getSubExpr() == 0) 1116 OS << "throw"; 1117 else { 1118 OS << "throw "; 1119 PrintExpr(Node->getSubExpr()); 1120 } 1121} 1122 1123void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1124 // Nothing to print: we picked up the default argument 1125} 1126 1127void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1128 OS << Node->getType().getAsString(Policy); 1129 OS << "("; 1130 PrintExpr(Node->getSubExpr()); 1131 OS << ")"; 1132} 1133 1134void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1135 PrintExpr(Node->getSubExpr()); 1136} 1137 1138void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1139 OS << Node->getType().getAsString(Policy); 1140 OS << "("; 1141 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1142 ArgEnd = Node->arg_end(); 1143 Arg != ArgEnd; ++Arg) { 1144 if (Arg != Node->arg_begin()) 1145 OS << ", "; 1146 PrintExpr(*Arg); 1147 } 1148 OS << ")"; 1149} 1150 1151void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1152 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1153 OS << TSInfo->getType().getAsString(Policy) << "()"; 1154 else 1155 OS << Node->getType().getAsString(Policy) << "()"; 1156} 1157 1158void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1159 if (E->isGlobalNew()) 1160 OS << "::"; 1161 OS << "new "; 1162 unsigned NumPlace = E->getNumPlacementArgs(); 1163 if (NumPlace > 0) { 1164 OS << "("; 1165 PrintExpr(E->getPlacementArg(0)); 1166 for (unsigned i = 1; i < NumPlace; ++i) { 1167 OS << ", "; 1168 PrintExpr(E->getPlacementArg(i)); 1169 } 1170 OS << ") "; 1171 } 1172 if (E->isParenTypeId()) 1173 OS << "("; 1174 std::string TypeS; 1175 if (Expr *Size = E->getArraySize()) { 1176 llvm::raw_string_ostream s(TypeS); 1177 Size->printPretty(s, Context, Helper, Policy); 1178 s.flush(); 1179 TypeS = "[" + TypeS + "]"; 1180 } 1181 E->getAllocatedType().getAsStringInternal(TypeS, Policy); 1182 OS << TypeS; 1183 if (E->isParenTypeId()) 1184 OS << ")"; 1185 1186 if (E->hasInitializer()) { 1187 OS << "("; 1188 unsigned NumCons = E->getNumConstructorArgs(); 1189 if (NumCons > 0) { 1190 PrintExpr(E->getConstructorArg(0)); 1191 for (unsigned i = 1; i < NumCons; ++i) { 1192 OS << ", "; 1193 PrintExpr(E->getConstructorArg(i)); 1194 } 1195 } 1196 OS << ")"; 1197 } 1198} 1199 1200void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1201 if (E->isGlobalDelete()) 1202 OS << "::"; 1203 OS << "delete "; 1204 if (E->isArrayForm()) 1205 OS << "[] "; 1206 PrintExpr(E->getArgument()); 1207} 1208 1209void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1210 PrintExpr(E->getBase()); 1211 if (E->isArrow()) 1212 OS << "->"; 1213 else 1214 OS << '.'; 1215 if (E->getQualifier()) 1216 E->getQualifier()->print(OS, Policy); 1217 1218 std::string TypeS; 1219 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1220 OS << II->getName(); 1221 else 1222 E->getDestroyedType().getAsStringInternal(TypeS, Policy); 1223 OS << TypeS; 1224} 1225 1226void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1227 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1228 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1229 // Don't print any defaulted arguments 1230 break; 1231 } 1232 1233 if (i) OS << ", "; 1234 PrintExpr(E->getArg(i)); 1235 } 1236} 1237 1238void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1239 // Just forward to the sub expression. 1240 PrintExpr(E->getSubExpr()); 1241} 1242 1243void 1244StmtPrinter::VisitCXXUnresolvedConstructExpr( 1245 CXXUnresolvedConstructExpr *Node) { 1246 OS << Node->getTypeAsWritten().getAsString(Policy); 1247 OS << "("; 1248 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1249 ArgEnd = Node->arg_end(); 1250 Arg != ArgEnd; ++Arg) { 1251 if (Arg != Node->arg_begin()) 1252 OS << ", "; 1253 PrintExpr(*Arg); 1254 } 1255 OS << ")"; 1256} 1257 1258void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1259 CXXDependentScopeMemberExpr *Node) { 1260 if (!Node->isImplicitAccess()) { 1261 PrintExpr(Node->getBase()); 1262 OS << (Node->isArrow() ? "->" : "."); 1263 } 1264 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1265 Qualifier->print(OS, Policy); 1266 else if (Node->hasExplicitTemplateArgs()) 1267 // FIXME: Track use of "template" keyword explicitly? 1268 OS << "template "; 1269 1270 OS << Node->getMemberNameInfo(); 1271 1272 if (Node->hasExplicitTemplateArgs()) { 1273 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1274 Node->getTemplateArgs(), 1275 Node->getNumTemplateArgs(), 1276 Policy); 1277 } 1278} 1279 1280void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1281 if (!Node->isImplicitAccess()) { 1282 PrintExpr(Node->getBase()); 1283 OS << (Node->isArrow() ? "->" : "."); 1284 } 1285 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1286 Qualifier->print(OS, Policy); 1287 1288 // FIXME: this might originally have been written with 'template' 1289 1290 OS << Node->getMemberNameInfo(); 1291 1292 if (Node->hasExplicitTemplateArgs()) { 1293 OS << TemplateSpecializationType::PrintTemplateArgumentList( 1294 Node->getTemplateArgs(), 1295 Node->getNumTemplateArgs(), 1296 Policy); 1297 } 1298} 1299 1300static const char *getTypeTraitName(UnaryTypeTrait UTT) { 1301 switch (UTT) { 1302 case UTT_HasNothrowAssign: return "__has_nothrow_assign"; 1303 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; 1304 case UTT_HasNothrowCopy: return "__has_nothrow_copy"; 1305 case UTT_HasTrivialAssign: return "__has_trivial_assign"; 1306 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor"; 1307 case UTT_HasTrivialCopy: return "__has_trivial_copy"; 1308 case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; 1309 case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; 1310 case UTT_IsAbstract: return "__is_abstract"; 1311 case UTT_IsArithmetic: return "__is_arithmetic"; 1312 case UTT_IsArray: return "__is_array"; 1313 case UTT_IsClass: return "__is_class"; 1314 case UTT_IsCompleteType: return "__is_complete_type"; 1315 case UTT_IsCompound: return "__is_compound"; 1316 case UTT_IsConst: return "__is_const"; 1317 case UTT_IsEmpty: return "__is_empty"; 1318 case UTT_IsEnum: return "__is_enum"; 1319 case UTT_IsFloatingPoint: return "__is_floating_point"; 1320 case UTT_IsFunction: return "__is_function"; 1321 case UTT_IsFundamental: return "__is_fundamental"; 1322 case UTT_IsIntegral: return "__is_integral"; 1323 case UTT_IsLiteral: return "__is_literal"; 1324 case UTT_IsLvalueReference: return "__is_lvalue_reference"; 1325 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer"; 1326 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer"; 1327 case UTT_IsMemberPointer: return "__is_member_pointer"; 1328 case UTT_IsObject: return "__is_object"; 1329 case UTT_IsPOD: return "__is_pod"; 1330 case UTT_IsPointer: return "__is_pointer"; 1331 case UTT_IsPolymorphic: return "__is_polymorphic"; 1332 case UTT_IsReference: return "__is_reference"; 1333 case UTT_IsRvalueReference: return "__is_rvalue_reference"; 1334 case UTT_IsScalar: return "__is_scalar"; 1335 case UTT_IsSigned: return "__is_signed"; 1336 case UTT_IsStandardLayout: return "__is_standard_layout"; 1337 case UTT_IsTrivial: return "__is_trivial"; 1338 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable"; 1339 case UTT_IsUnion: return "__is_union"; 1340 case UTT_IsUnsigned: return "__is_unsigned"; 1341 case UTT_IsVoid: return "__is_void"; 1342 case UTT_IsVolatile: return "__is_volatile"; 1343 } 1344 llvm_unreachable("Type trait not covered by switch statement"); 1345} 1346 1347static const char *getTypeTraitName(BinaryTypeTrait BTT) { 1348 switch (BTT) { 1349 case BTT_IsBaseOf: return "__is_base_of"; 1350 case BTT_IsConvertible: return "__is_convertible"; 1351 case BTT_IsSame: return "__is_same"; 1352 case BTT_TypeCompatible: return "__builtin_types_compatible_p"; 1353 case BTT_IsConvertibleTo: return "__is_convertible_to"; 1354 } 1355 llvm_unreachable("Binary type trait not covered by switch"); 1356} 1357 1358static const char *getTypeTraitName(ArrayTypeTrait ATT) { 1359 switch (ATT) { 1360 case ATT_ArrayRank: return "__array_rank"; 1361 case ATT_ArrayExtent: return "__array_extent"; 1362 } 1363 llvm_unreachable("Array type trait not covered by switch"); 1364} 1365 1366static const char *getExpressionTraitName(ExpressionTrait ET) { 1367 switch (ET) { 1368 case ET_IsLValueExpr: return "__is_lvalue_expr"; 1369 case ET_IsRValueExpr: return "__is_rvalue_expr"; 1370 } 1371 llvm_unreachable("Expression type trait not covered by switch"); 1372} 1373 1374void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 1375 OS << getTypeTraitName(E->getTrait()) << "(" 1376 << E->getQueriedType().getAsString(Policy) << ")"; 1377} 1378 1379void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 1380 OS << getTypeTraitName(E->getTrait()) << "(" 1381 << E->getLhsType().getAsString(Policy) << "," 1382 << E->getRhsType().getAsString(Policy) << ")"; 1383} 1384 1385void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1386 OS << getTypeTraitName(E->getTrait()) << "(" 1387 << E->getQueriedType().getAsString(Policy) << ")"; 1388} 1389 1390void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1391 OS << getExpressionTraitName(E->getTrait()) << "("; 1392 PrintExpr(E->getQueriedExpression()); 1393 OS << ")"; 1394} 1395 1396void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1397 OS << "noexcept("; 1398 PrintExpr(E->getOperand()); 1399 OS << ")"; 1400} 1401 1402void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1403 PrintExpr(E->getPattern()); 1404 OS << "..."; 1405} 1406 1407void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1408 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")"; 1409} 1410 1411void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1412 SubstNonTypeTemplateParmPackExpr *Node) { 1413 OS << Node->getParameterPack()->getNameAsString(); 1414} 1415 1416void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1417 PrintExpr(Node->GetTemporaryExpr()); 1418} 1419 1420// Obj-C 1421 1422void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1423 OS << "@"; 1424 VisitStringLiteral(Node->getString()); 1425} 1426 1427void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1428 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')'; 1429} 1430 1431void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1432 OS << "@selector(" << Node->getSelector().getAsString() << ')'; 1433} 1434 1435void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1436 OS << "@protocol(" << Node->getProtocol() << ')'; 1437} 1438 1439void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1440 OS << "["; 1441 switch (Mess->getReceiverKind()) { 1442 case ObjCMessageExpr::Instance: 1443 PrintExpr(Mess->getInstanceReceiver()); 1444 break; 1445 1446 case ObjCMessageExpr::Class: 1447 OS << Mess->getClassReceiver().getAsString(Policy); 1448 break; 1449 1450 case ObjCMessageExpr::SuperInstance: 1451 case ObjCMessageExpr::SuperClass: 1452 OS << "Super"; 1453 break; 1454 } 1455 1456 OS << ' '; 1457 Selector selector = Mess->getSelector(); 1458 if (selector.isUnarySelector()) { 1459 OS << selector.getNameForSlot(0); 1460 } else { 1461 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1462 if (i < selector.getNumArgs()) { 1463 if (i > 0) OS << ' '; 1464 if (selector.getIdentifierInfoForSlot(i)) 1465 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1466 else 1467 OS << ":"; 1468 } 1469 else OS << ", "; // Handle variadic methods. 1470 1471 PrintExpr(Mess->getArg(i)); 1472 } 1473 } 1474 OS << "]"; 1475} 1476 1477void 1478StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 1479 PrintExpr(E->getSubExpr()); 1480} 1481 1482void 1483StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 1484 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy) 1485 << ")"; 1486 PrintExpr(E->getSubExpr()); 1487} 1488 1489void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1490 BlockDecl *BD = Node->getBlockDecl(); 1491 OS << "^"; 1492 1493 const FunctionType *AFT = Node->getFunctionType(); 1494 1495 if (isa<FunctionNoProtoType>(AFT)) { 1496 OS << "()"; 1497 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1498 OS << '('; 1499 std::string ParamStr; 1500 for (BlockDecl::param_iterator AI = BD->param_begin(), 1501 E = BD->param_end(); AI != E; ++AI) { 1502 if (AI != BD->param_begin()) OS << ", "; 1503 ParamStr = (*AI)->getNameAsString(); 1504 (*AI)->getType().getAsStringInternal(ParamStr, Policy); 1505 OS << ParamStr; 1506 } 1507 1508 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1509 if (FT->isVariadic()) { 1510 if (!BD->param_empty()) OS << ", "; 1511 OS << "..."; 1512 } 1513 OS << ')'; 1514 } 1515} 1516 1517void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) { 1518 OS << Node->getDecl(); 1519} 1520 1521void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {} 1522 1523void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 1524 OS << "__builtin_astype("; 1525 PrintExpr(Node->getSrcExpr()); 1526 OS << ", " << Node->getType().getAsString(); 1527 OS << ")"; 1528} 1529 1530//===----------------------------------------------------------------------===// 1531// Stmt method implementations 1532//===----------------------------------------------------------------------===// 1533 1534void Stmt::dumpPretty(ASTContext& Context) const { 1535 printPretty(llvm::errs(), Context, 0, 1536 PrintingPolicy(Context.getLangOptions())); 1537} 1538 1539void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context, 1540 PrinterHelper* Helper, 1541 const PrintingPolicy &Policy, 1542 unsigned Indentation) const { 1543 if (this == 0) { 1544 OS << "<NULL>"; 1545 return; 1546 } 1547 1548 if (Policy.Dump && &Context) { 1549 dump(OS, Context.getSourceManager()); 1550 return; 1551 } 1552 1553 StmtPrinter P(OS, Context, Helper, Policy, Indentation); 1554 P.Visit(const_cast<Stmt*>(this)); 1555} 1556 1557//===----------------------------------------------------------------------===// 1558// PrinterHelper 1559//===----------------------------------------------------------------------===// 1560 1561// Implement virtual destructor. 1562PrinterHelper::~PrinterHelper() {} 1563