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