StmtPrinter.cpp revision 288943
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/ASTContext.h" 16#include "clang/AST/Attr.h" 17#include "clang/AST/DeclCXX.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/DeclTemplate.h" 20#include "clang/AST/Expr.h" 21#include "clang/AST/ExprCXX.h" 22#include "clang/AST/PrettyPrinter.h" 23#include "clang/AST/StmtVisitor.h" 24#include "clang/Basic/CharInfo.h" 25#include "llvm/ADT/SmallString.h" 26#include "llvm/Support/Format.h" 27using namespace clang; 28 29//===----------------------------------------------------------------------===// 30// StmtPrinter Visitor 31//===----------------------------------------------------------------------===// 32 33namespace { 34 class StmtPrinter : public StmtVisitor<StmtPrinter> { 35 raw_ostream &OS; 36 unsigned IndentLevel; 37 clang::PrinterHelper* Helper; 38 PrintingPolicy Policy; 39 40 public: 41 StmtPrinter(raw_ostream &os, PrinterHelper* helper, 42 const PrintingPolicy &Policy, 43 unsigned Indentation = 0) 44 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {} 45 46 void PrintStmt(Stmt *S) { 47 PrintStmt(S, Policy.Indentation); 48 } 49 50 void PrintStmt(Stmt *S, int SubIndent) { 51 IndentLevel += SubIndent; 52 if (S && isa<Expr>(S)) { 53 // If this is an expr used in a stmt context, indent and newline it. 54 Indent(); 55 Visit(S); 56 OS << ";\n"; 57 } else if (S) { 58 Visit(S); 59 } else { 60 Indent() << "<<<NULL STATEMENT>>>\n"; 61 } 62 IndentLevel -= SubIndent; 63 } 64 65 void PrintRawCompoundStmt(CompoundStmt *S); 66 void PrintRawDecl(Decl *D); 67 void PrintRawDeclStmt(const DeclStmt *S); 68 void PrintRawIfStmt(IfStmt *If); 69 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); 70 void PrintCallArgs(CallExpr *E); 71 void PrintRawSEHExceptHandler(SEHExceptStmt *S); 72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S); 73 void PrintOMPExecutableDirective(OMPExecutableDirective *S); 74 75 void PrintExpr(Expr *E) { 76 if (E) 77 Visit(E); 78 else 79 OS << "<null expr>"; 80 } 81 82 raw_ostream &Indent(int Delta = 0) { 83 for (int i = 0, e = IndentLevel+Delta; i < e; ++i) 84 OS << " "; 85 return OS; 86 } 87 88 void Visit(Stmt* S) { 89 if (Helper && Helper->handledStmt(S,OS)) 90 return; 91 else StmtVisitor<StmtPrinter>::Visit(S); 92 } 93 94 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED { 95 Indent() << "<<unknown stmt type>>\n"; 96 } 97 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED { 98 OS << "<<unknown expr type>>"; 99 } 100 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); 101 102#define ABSTRACT_STMT(CLASS) 103#define STMT(CLASS, PARENT) \ 104 void Visit##CLASS(CLASS *Node); 105#include "clang/AST/StmtNodes.inc" 106 }; 107} 108 109//===----------------------------------------------------------------------===// 110// Stmt printing methods. 111//===----------------------------------------------------------------------===// 112 113/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and 114/// with no newline after the }. 115void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { 116 OS << "{\n"; 117 for (auto *I : Node->body()) 118 PrintStmt(I); 119 120 Indent() << "}"; 121} 122 123void StmtPrinter::PrintRawDecl(Decl *D) { 124 D->print(OS, Policy, IndentLevel); 125} 126 127void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) { 128 SmallVector<Decl*, 2> Decls(S->decls()); 129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); 130} 131 132void StmtPrinter::VisitNullStmt(NullStmt *Node) { 133 Indent() << ";\n"; 134} 135 136void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { 137 Indent(); 138 PrintRawDeclStmt(Node); 139 OS << ";\n"; 140} 141 142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { 143 Indent(); 144 PrintRawCompoundStmt(Node); 145 OS << "\n"; 146} 147 148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { 149 Indent(-1) << "case "; 150 PrintExpr(Node->getLHS()); 151 if (Node->getRHS()) { 152 OS << " ... "; 153 PrintExpr(Node->getRHS()); 154 } 155 OS << ":\n"; 156 157 PrintStmt(Node->getSubStmt(), 0); 158} 159 160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { 161 Indent(-1) << "default:\n"; 162 PrintStmt(Node->getSubStmt(), 0); 163} 164 165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { 166 Indent(-1) << Node->getName() << ":\n"; 167 PrintStmt(Node->getSubStmt(), 0); 168} 169 170void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) { 171 for (const auto *Attr : Node->getAttrs()) { 172 Attr->printPretty(OS, Policy); 173 } 174 175 PrintStmt(Node->getSubStmt(), 0); 176} 177 178void StmtPrinter::PrintRawIfStmt(IfStmt *If) { 179 OS << "if ("; 180 if (const DeclStmt *DS = If->getConditionVariableDeclStmt()) 181 PrintRawDeclStmt(DS); 182 else 183 PrintExpr(If->getCond()); 184 OS << ')'; 185 186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { 187 OS << ' '; 188 PrintRawCompoundStmt(CS); 189 OS << (If->getElse() ? ' ' : '\n'); 190 } else { 191 OS << '\n'; 192 PrintStmt(If->getThen()); 193 if (If->getElse()) Indent(); 194 } 195 196 if (Stmt *Else = If->getElse()) { 197 OS << "else"; 198 199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { 200 OS << ' '; 201 PrintRawCompoundStmt(CS); 202 OS << '\n'; 203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { 204 OS << ' '; 205 PrintRawIfStmt(ElseIf); 206 } else { 207 OS << '\n'; 208 PrintStmt(If->getElse()); 209 } 210 } 211} 212 213void StmtPrinter::VisitIfStmt(IfStmt *If) { 214 Indent(); 215 PrintRawIfStmt(If); 216} 217 218void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { 219 Indent() << "switch ("; 220 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 221 PrintRawDeclStmt(DS); 222 else 223 PrintExpr(Node->getCond()); 224 OS << ")"; 225 226 // Pretty print compoundstmt bodies (very common). 227 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 228 OS << " "; 229 PrintRawCompoundStmt(CS); 230 OS << "\n"; 231 } else { 232 OS << "\n"; 233 PrintStmt(Node->getBody()); 234 } 235} 236 237void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { 238 Indent() << "while ("; 239 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 240 PrintRawDeclStmt(DS); 241 else 242 PrintExpr(Node->getCond()); 243 OS << ")\n"; 244 PrintStmt(Node->getBody()); 245} 246 247void StmtPrinter::VisitDoStmt(DoStmt *Node) { 248 Indent() << "do "; 249 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 250 PrintRawCompoundStmt(CS); 251 OS << " "; 252 } else { 253 OS << "\n"; 254 PrintStmt(Node->getBody()); 255 Indent(); 256 } 257 258 OS << "while ("; 259 PrintExpr(Node->getCond()); 260 OS << ");\n"; 261} 262 263void StmtPrinter::VisitForStmt(ForStmt *Node) { 264 Indent() << "for ("; 265 if (Node->getInit()) { 266 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) 267 PrintRawDeclStmt(DS); 268 else 269 PrintExpr(cast<Expr>(Node->getInit())); 270 } 271 OS << ";"; 272 if (Node->getCond()) { 273 OS << " "; 274 PrintExpr(Node->getCond()); 275 } 276 OS << ";"; 277 if (Node->getInc()) { 278 OS << " "; 279 PrintExpr(Node->getInc()); 280 } 281 OS << ") "; 282 283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 284 PrintRawCompoundStmt(CS); 285 OS << "\n"; 286 } else { 287 OS << "\n"; 288 PrintStmt(Node->getBody()); 289 } 290} 291 292void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { 293 Indent() << "for ("; 294 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) 295 PrintRawDeclStmt(DS); 296 else 297 PrintExpr(cast<Expr>(Node->getElement())); 298 OS << " in "; 299 PrintExpr(Node->getCollection()); 300 OS << ") "; 301 302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 303 PrintRawCompoundStmt(CS); 304 OS << "\n"; 305 } else { 306 OS << "\n"; 307 PrintStmt(Node->getBody()); 308 } 309} 310 311void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) { 312 Indent() << "for ("; 313 PrintingPolicy SubPolicy(Policy); 314 SubPolicy.SuppressInitializers = true; 315 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel); 316 OS << " : "; 317 PrintExpr(Node->getRangeInit()); 318 OS << ") {\n"; 319 PrintStmt(Node->getBody()); 320 Indent() << "}"; 321 if (Policy.IncludeNewlines) OS << "\n"; 322} 323 324void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) { 325 Indent(); 326 if (Node->isIfExists()) 327 OS << "__if_exists ("; 328 else 329 OS << "__if_not_exists ("; 330 331 if (NestedNameSpecifier *Qualifier 332 = Node->getQualifierLoc().getNestedNameSpecifier()) 333 Qualifier->print(OS, Policy); 334 335 OS << Node->getNameInfo() << ") "; 336 337 PrintRawCompoundStmt(Node->getSubStmt()); 338} 339 340void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { 341 Indent() << "goto " << Node->getLabel()->getName() << ";"; 342 if (Policy.IncludeNewlines) OS << "\n"; 343} 344 345void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 346 Indent() << "goto *"; 347 PrintExpr(Node->getTarget()); 348 OS << ";"; 349 if (Policy.IncludeNewlines) OS << "\n"; 350} 351 352void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 353 Indent() << "continue;"; 354 if (Policy.IncludeNewlines) OS << "\n"; 355} 356 357void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 358 Indent() << "break;"; 359 if (Policy.IncludeNewlines) OS << "\n"; 360} 361 362 363void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 364 Indent() << "return"; 365 if (Node->getRetValue()) { 366 OS << " "; 367 PrintExpr(Node->getRetValue()); 368 } 369 OS << ";"; 370 if (Policy.IncludeNewlines) OS << "\n"; 371} 372 373 374void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { 375 Indent() << "asm "; 376 377 if (Node->isVolatile()) 378 OS << "volatile "; 379 380 OS << "("; 381 VisitStringLiteral(Node->getAsmString()); 382 383 // Outputs 384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 385 Node->getNumClobbers() != 0) 386 OS << " : "; 387 388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 389 if (i != 0) 390 OS << ", "; 391 392 if (!Node->getOutputName(i).empty()) { 393 OS << '['; 394 OS << Node->getOutputName(i); 395 OS << "] "; 396 } 397 398 VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 399 OS << " ("; 400 Visit(Node->getOutputExpr(i)); 401 OS << ")"; 402 } 403 404 // Inputs 405 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 406 OS << " : "; 407 408 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 409 if (i != 0) 410 OS << ", "; 411 412 if (!Node->getInputName(i).empty()) { 413 OS << '['; 414 OS << Node->getInputName(i); 415 OS << "] "; 416 } 417 418 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 419 OS << " ("; 420 Visit(Node->getInputExpr(i)); 421 OS << ")"; 422 } 423 424 // Clobbers 425 if (Node->getNumClobbers() != 0) 426 OS << " : "; 427 428 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 429 if (i != 0) 430 OS << ", "; 431 432 VisitStringLiteral(Node->getClobberStringLiteral(i)); 433 } 434 435 OS << ");"; 436 if (Policy.IncludeNewlines) OS << "\n"; 437} 438 439void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) { 440 // FIXME: Implement MS style inline asm statement printer. 441 Indent() << "__asm "; 442 if (Node->hasBraces()) 443 OS << "{\n"; 444 OS << Node->getAsmString() << "\n"; 445 if (Node->hasBraces()) 446 Indent() << "}\n"; 447} 448 449void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) { 450 PrintStmt(Node->getCapturedDecl()->getBody()); 451} 452 453void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 454 Indent() << "@try"; 455 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 456 PrintRawCompoundStmt(TS); 457 OS << "\n"; 458 } 459 460 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 461 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 462 Indent() << "@catch("; 463 if (catchStmt->getCatchParamDecl()) { 464 if (Decl *DS = catchStmt->getCatchParamDecl()) 465 PrintRawDecl(DS); 466 } 467 OS << ")"; 468 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 469 PrintRawCompoundStmt(CS); 470 OS << "\n"; 471 } 472 } 473 474 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 475 Node->getFinallyStmt())) { 476 Indent() << "@finally"; 477 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 478 OS << "\n"; 479 } 480} 481 482void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 483} 484 485void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 486 Indent() << "@catch (...) { /* todo */ } \n"; 487} 488 489void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 490 Indent() << "@throw"; 491 if (Node->getThrowExpr()) { 492 OS << " "; 493 PrintExpr(Node->getThrowExpr()); 494 } 495 OS << ";\n"; 496} 497 498void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 499 Indent() << "@synchronized ("; 500 PrintExpr(Node->getSynchExpr()); 501 OS << ")"; 502 PrintRawCompoundStmt(Node->getSynchBody()); 503 OS << "\n"; 504} 505 506void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 507 Indent() << "@autoreleasepool"; 508 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt())); 509 OS << "\n"; 510} 511 512void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 513 OS << "catch ("; 514 if (Decl *ExDecl = Node->getExceptionDecl()) 515 PrintRawDecl(ExDecl); 516 else 517 OS << "..."; 518 OS << ") "; 519 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 520} 521 522void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 523 Indent(); 524 PrintRawCXXCatchStmt(Node); 525 OS << "\n"; 526} 527 528void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 529 Indent() << "try "; 530 PrintRawCompoundStmt(Node->getTryBlock()); 531 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 532 OS << " "; 533 PrintRawCXXCatchStmt(Node->getHandler(i)); 534 } 535 OS << "\n"; 536} 537 538void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 539 Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 540 PrintRawCompoundStmt(Node->getTryBlock()); 541 SEHExceptStmt *E = Node->getExceptHandler(); 542 SEHFinallyStmt *F = Node->getFinallyHandler(); 543 if(E) 544 PrintRawSEHExceptHandler(E); 545 else { 546 assert(F && "Must have a finally block..."); 547 PrintRawSEHFinallyStmt(F); 548 } 549 OS << "\n"; 550} 551 552void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 553 OS << "__finally "; 554 PrintRawCompoundStmt(Node->getBlock()); 555 OS << "\n"; 556} 557 558void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 559 OS << "__except ("; 560 VisitExpr(Node->getFilterExpr()); 561 OS << ")\n"; 562 PrintRawCompoundStmt(Node->getBlock()); 563 OS << "\n"; 564} 565 566void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 567 Indent(); 568 PrintRawSEHExceptHandler(Node); 569 OS << "\n"; 570} 571 572void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 573 Indent(); 574 PrintRawSEHFinallyStmt(Node); 575 OS << "\n"; 576} 577 578void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) { 579 Indent() << "__leave;"; 580 if (Policy.IncludeNewlines) OS << "\n"; 581} 582 583//===----------------------------------------------------------------------===// 584// OpenMP clauses printing methods 585//===----------------------------------------------------------------------===// 586 587namespace { 588class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> { 589 raw_ostream &OS; 590 const PrintingPolicy &Policy; 591 /// \brief Process clauses with list of variables. 592 template <typename T> 593 void VisitOMPClauseList(T *Node, char StartSym); 594public: 595 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy) 596 : OS(OS), Policy(Policy) { } 597#define OPENMP_CLAUSE(Name, Class) \ 598 void Visit##Class(Class *S); 599#include "clang/Basic/OpenMPKinds.def" 600}; 601 602void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) { 603 OS << "if("; 604 Node->getCondition()->printPretty(OS, nullptr, Policy, 0); 605 OS << ")"; 606} 607 608void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) { 609 OS << "final("; 610 Node->getCondition()->printPretty(OS, nullptr, Policy, 0); 611 OS << ")"; 612} 613 614void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) { 615 OS << "num_threads("; 616 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0); 617 OS << ")"; 618} 619 620void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) { 621 OS << "safelen("; 622 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0); 623 OS << ")"; 624} 625 626void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) { 627 OS << "collapse("; 628 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0); 629 OS << ")"; 630} 631 632void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) { 633 OS << "default(" 634 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind()) 635 << ")"; 636} 637 638void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) { 639 OS << "proc_bind(" 640 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind()) 641 << ")"; 642} 643 644void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) { 645 OS << "schedule(" 646 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind()); 647 if (Node->getChunkSize()) { 648 OS << ", "; 649 Node->getChunkSize()->printPretty(OS, nullptr, Policy); 650 } 651 OS << ")"; 652} 653 654void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) { 655 OS << "ordered"; 656} 657 658void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) { 659 OS << "nowait"; 660} 661 662void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) { 663 OS << "untied"; 664} 665 666void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) { 667 OS << "mergeable"; 668} 669 670void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; } 671 672void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; } 673 674void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) { 675 OS << "update"; 676} 677 678void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) { 679 OS << "capture"; 680} 681 682void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) { 683 OS << "seq_cst"; 684} 685 686template<typename T> 687void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) { 688 for (typename T::varlist_iterator I = Node->varlist_begin(), 689 E = Node->varlist_end(); 690 I != E; ++I) { 691 assert(*I && "Expected non-null Stmt"); 692 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) { 693 OS << (I == Node->varlist_begin() ? StartSym : ','); 694 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS); 695 } else { 696 OS << (I == Node->varlist_begin() ? StartSym : ','); 697 (*I)->printPretty(OS, nullptr, Policy, 0); 698 } 699 } 700} 701 702void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) { 703 if (!Node->varlist_empty()) { 704 OS << "private"; 705 VisitOMPClauseList(Node, '('); 706 OS << ")"; 707 } 708} 709 710void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) { 711 if (!Node->varlist_empty()) { 712 OS << "firstprivate"; 713 VisitOMPClauseList(Node, '('); 714 OS << ")"; 715 } 716} 717 718void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) { 719 if (!Node->varlist_empty()) { 720 OS << "lastprivate"; 721 VisitOMPClauseList(Node, '('); 722 OS << ")"; 723 } 724} 725 726void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) { 727 if (!Node->varlist_empty()) { 728 OS << "shared"; 729 VisitOMPClauseList(Node, '('); 730 OS << ")"; 731 } 732} 733 734void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) { 735 if (!Node->varlist_empty()) { 736 OS << "reduction("; 737 NestedNameSpecifier *QualifierLoc = 738 Node->getQualifierLoc().getNestedNameSpecifier(); 739 OverloadedOperatorKind OOK = 740 Node->getNameInfo().getName().getCXXOverloadedOperator(); 741 if (QualifierLoc == nullptr && OOK != OO_None) { 742 // Print reduction identifier in C format 743 OS << getOperatorSpelling(OOK); 744 } else { 745 // Use C++ format 746 if (QualifierLoc != nullptr) 747 QualifierLoc->print(OS, Policy); 748 OS << Node->getNameInfo(); 749 } 750 OS << ":"; 751 VisitOMPClauseList(Node, ' '); 752 OS << ")"; 753 } 754} 755 756void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) { 757 if (!Node->varlist_empty()) { 758 OS << "linear"; 759 VisitOMPClauseList(Node, '('); 760 if (Node->getStep() != nullptr) { 761 OS << ": "; 762 Node->getStep()->printPretty(OS, nullptr, Policy, 0); 763 } 764 OS << ")"; 765 } 766} 767 768void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) { 769 if (!Node->varlist_empty()) { 770 OS << "aligned"; 771 VisitOMPClauseList(Node, '('); 772 if (Node->getAlignment() != nullptr) { 773 OS << ": "; 774 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0); 775 } 776 OS << ")"; 777 } 778} 779 780void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) { 781 if (!Node->varlist_empty()) { 782 OS << "copyin"; 783 VisitOMPClauseList(Node, '('); 784 OS << ")"; 785 } 786} 787 788void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) { 789 if (!Node->varlist_empty()) { 790 OS << "copyprivate"; 791 VisitOMPClauseList(Node, '('); 792 OS << ")"; 793 } 794} 795 796void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) { 797 if (!Node->varlist_empty()) { 798 VisitOMPClauseList(Node, '('); 799 OS << ")"; 800 } 801} 802 803void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) { 804 if (!Node->varlist_empty()) { 805 OS << "depend("; 806 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), 807 Node->getDependencyKind()) 808 << " :"; 809 VisitOMPClauseList(Node, ' '); 810 OS << ")"; 811 } 812} 813} 814 815//===----------------------------------------------------------------------===// 816// OpenMP directives printing methods 817//===----------------------------------------------------------------------===// 818 819void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) { 820 OMPClausePrinter Printer(OS, Policy); 821 ArrayRef<OMPClause *> Clauses = S->clauses(); 822 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 823 I != E; ++I) 824 if (*I && !(*I)->isImplicit()) { 825 Printer.Visit(*I); 826 OS << ' '; 827 } 828 OS << "\n"; 829 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) { 830 assert(isa<CapturedStmt>(S->getAssociatedStmt()) && 831 "Expected captured statement!"); 832 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt(); 833 PrintStmt(CS); 834 } 835} 836 837void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) { 838 Indent() << "#pragma omp parallel "; 839 PrintOMPExecutableDirective(Node); 840} 841 842void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) { 843 Indent() << "#pragma omp simd "; 844 PrintOMPExecutableDirective(Node); 845} 846 847void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) { 848 Indent() << "#pragma omp for "; 849 PrintOMPExecutableDirective(Node); 850} 851 852void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) { 853 Indent() << "#pragma omp for simd "; 854 PrintOMPExecutableDirective(Node); 855} 856 857void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) { 858 Indent() << "#pragma omp sections "; 859 PrintOMPExecutableDirective(Node); 860} 861 862void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) { 863 Indent() << "#pragma omp section"; 864 PrintOMPExecutableDirective(Node); 865} 866 867void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) { 868 Indent() << "#pragma omp single "; 869 PrintOMPExecutableDirective(Node); 870} 871 872void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) { 873 Indent() << "#pragma omp master"; 874 PrintOMPExecutableDirective(Node); 875} 876 877void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) { 878 Indent() << "#pragma omp critical"; 879 if (Node->getDirectiveName().getName()) { 880 OS << " ("; 881 Node->getDirectiveName().printName(OS); 882 OS << ")"; 883 } 884 PrintOMPExecutableDirective(Node); 885} 886 887void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) { 888 Indent() << "#pragma omp parallel for "; 889 PrintOMPExecutableDirective(Node); 890} 891 892void StmtPrinter::VisitOMPParallelForSimdDirective( 893 OMPParallelForSimdDirective *Node) { 894 Indent() << "#pragma omp parallel for simd "; 895 PrintOMPExecutableDirective(Node); 896} 897 898void StmtPrinter::VisitOMPParallelSectionsDirective( 899 OMPParallelSectionsDirective *Node) { 900 Indent() << "#pragma omp parallel sections "; 901 PrintOMPExecutableDirective(Node); 902} 903 904void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) { 905 Indent() << "#pragma omp task "; 906 PrintOMPExecutableDirective(Node); 907} 908 909void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) { 910 Indent() << "#pragma omp taskyield"; 911 PrintOMPExecutableDirective(Node); 912} 913 914void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) { 915 Indent() << "#pragma omp barrier"; 916 PrintOMPExecutableDirective(Node); 917} 918 919void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) { 920 Indent() << "#pragma omp taskwait"; 921 PrintOMPExecutableDirective(Node); 922} 923 924void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) { 925 Indent() << "#pragma omp taskgroup"; 926 PrintOMPExecutableDirective(Node); 927} 928 929void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) { 930 Indent() << "#pragma omp flush "; 931 PrintOMPExecutableDirective(Node); 932} 933 934void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) { 935 Indent() << "#pragma omp ordered"; 936 PrintOMPExecutableDirective(Node); 937} 938 939void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) { 940 Indent() << "#pragma omp atomic "; 941 PrintOMPExecutableDirective(Node); 942} 943 944void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) { 945 Indent() << "#pragma omp target "; 946 PrintOMPExecutableDirective(Node); 947} 948 949void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) { 950 Indent() << "#pragma omp teams "; 951 PrintOMPExecutableDirective(Node); 952} 953 954void StmtPrinter::VisitOMPCancellationPointDirective( 955 OMPCancellationPointDirective *Node) { 956 Indent() << "#pragma omp cancellation point " 957 << getOpenMPDirectiveName(Node->getCancelRegion()); 958 PrintOMPExecutableDirective(Node); 959} 960 961void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) { 962 Indent() << "#pragma omp cancel " 963 << getOpenMPDirectiveName(Node->getCancelRegion()); 964 PrintOMPExecutableDirective(Node); 965} 966//===----------------------------------------------------------------------===// 967// Expr printing methods. 968//===----------------------------------------------------------------------===// 969 970void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 971 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 972 Qualifier->print(OS, Policy); 973 if (Node->hasTemplateKeyword()) 974 OS << "template "; 975 OS << Node->getNameInfo(); 976 if (Node->hasExplicitTemplateArgs()) 977 TemplateSpecializationType::PrintTemplateArgumentList( 978 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 979} 980 981void StmtPrinter::VisitDependentScopeDeclRefExpr( 982 DependentScopeDeclRefExpr *Node) { 983 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 984 Qualifier->print(OS, Policy); 985 if (Node->hasTemplateKeyword()) 986 OS << "template "; 987 OS << Node->getNameInfo(); 988 if (Node->hasExplicitTemplateArgs()) 989 TemplateSpecializationType::PrintTemplateArgumentList( 990 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 991} 992 993void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 994 if (Node->getQualifier()) 995 Node->getQualifier()->print(OS, Policy); 996 if (Node->hasTemplateKeyword()) 997 OS << "template "; 998 OS << Node->getNameInfo(); 999 if (Node->hasExplicitTemplateArgs()) 1000 TemplateSpecializationType::PrintTemplateArgumentList( 1001 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1002} 1003 1004void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 1005 if (Node->getBase()) { 1006 PrintExpr(Node->getBase()); 1007 OS << (Node->isArrow() ? "->" : "."); 1008 } 1009 OS << *Node->getDecl(); 1010} 1011 1012void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 1013 if (Node->isSuperReceiver()) 1014 OS << "super."; 1015 else if (Node->isObjectReceiver() && Node->getBase()) { 1016 PrintExpr(Node->getBase()); 1017 OS << "."; 1018 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 1019 OS << Node->getClassReceiver()->getName() << "."; 1020 } 1021 1022 if (Node->isImplicitProperty()) 1023 Node->getImplicitPropertyGetter()->getSelector().print(OS); 1024 else 1025 OS << Node->getExplicitProperty()->getName(); 1026} 1027 1028void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 1029 1030 PrintExpr(Node->getBaseExpr()); 1031 OS << "["; 1032 PrintExpr(Node->getKeyExpr()); 1033 OS << "]"; 1034} 1035 1036void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 1037 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType()); 1038} 1039 1040void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 1041 unsigned value = Node->getValue(); 1042 1043 switch (Node->getKind()) { 1044 case CharacterLiteral::Ascii: break; // no prefix. 1045 case CharacterLiteral::Wide: OS << 'L'; break; 1046 case CharacterLiteral::UTF16: OS << 'u'; break; 1047 case CharacterLiteral::UTF32: OS << 'U'; break; 1048 } 1049 1050 switch (value) { 1051 case '\\': 1052 OS << "'\\\\'"; 1053 break; 1054 case '\'': 1055 OS << "'\\''"; 1056 break; 1057 case '\a': 1058 // TODO: K&R: the meaning of '\\a' is different in traditional C 1059 OS << "'\\a'"; 1060 break; 1061 case '\b': 1062 OS << "'\\b'"; 1063 break; 1064 // Nonstandard escape sequence. 1065 /*case '\e': 1066 OS << "'\\e'"; 1067 break;*/ 1068 case '\f': 1069 OS << "'\\f'"; 1070 break; 1071 case '\n': 1072 OS << "'\\n'"; 1073 break; 1074 case '\r': 1075 OS << "'\\r'"; 1076 break; 1077 case '\t': 1078 OS << "'\\t'"; 1079 break; 1080 case '\v': 1081 OS << "'\\v'"; 1082 break; 1083 default: 1084 if (value < 256 && isPrintable((unsigned char)value)) 1085 OS << "'" << (char)value << "'"; 1086 else if (value < 256) 1087 OS << "'\\x" << llvm::format("%02x", value) << "'"; 1088 else if (value <= 0xFFFF) 1089 OS << "'\\u" << llvm::format("%04x", value) << "'"; 1090 else 1091 OS << "'\\U" << llvm::format("%08x", value) << "'"; 1092 } 1093} 1094 1095void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 1096 bool isSigned = Node->getType()->isSignedIntegerType(); 1097 OS << Node->getValue().toString(10, isSigned); 1098 1099 // Emit suffixes. Integer literals are always a builtin integer type. 1100 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1101 default: llvm_unreachable("Unexpected type for integer literal!"); 1102 case BuiltinType::Char_S: 1103 case BuiltinType::Char_U: OS << "i8"; break; 1104 case BuiltinType::UChar: OS << "Ui8"; break; 1105 case BuiltinType::Short: OS << "i16"; break; 1106 case BuiltinType::UShort: OS << "Ui16"; break; 1107 case BuiltinType::Int: break; // no suffix. 1108 case BuiltinType::UInt: OS << 'U'; break; 1109 case BuiltinType::Long: OS << 'L'; break; 1110 case BuiltinType::ULong: OS << "UL"; break; 1111 case BuiltinType::LongLong: OS << "LL"; break; 1112 case BuiltinType::ULongLong: OS << "ULL"; break; 1113 case BuiltinType::Int128: OS << "i128"; break; 1114 case BuiltinType::UInt128: OS << "Ui128"; break; 1115 } 1116} 1117 1118static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 1119 bool PrintSuffix) { 1120 SmallString<16> Str; 1121 Node->getValue().toString(Str); 1122 OS << Str; 1123 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 1124 OS << '.'; // Trailing dot in order to separate from ints. 1125 1126 if (!PrintSuffix) 1127 return; 1128 1129 // Emit suffixes. Float literals are always a builtin float type. 1130 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1131 default: llvm_unreachable("Unexpected type for float literal!"); 1132 case BuiltinType::Half: break; // FIXME: suffix? 1133 case BuiltinType::Double: break; // no suffix. 1134 case BuiltinType::Float: OS << 'F'; break; 1135 case BuiltinType::LongDouble: OS << 'L'; break; 1136 } 1137} 1138 1139void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 1140 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 1141} 1142 1143void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 1144 PrintExpr(Node->getSubExpr()); 1145 OS << "i"; 1146} 1147 1148void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 1149 Str->outputString(OS); 1150} 1151void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 1152 OS << "("; 1153 PrintExpr(Node->getSubExpr()); 1154 OS << ")"; 1155} 1156void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1157 if (!Node->isPostfix()) { 1158 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1159 1160 // Print a space if this is an "identifier operator" like __real, or if 1161 // it might be concatenated incorrectly like '+'. 1162 switch (Node->getOpcode()) { 1163 default: break; 1164 case UO_Real: 1165 case UO_Imag: 1166 case UO_Extension: 1167 OS << ' '; 1168 break; 1169 case UO_Plus: 1170 case UO_Minus: 1171 if (isa<UnaryOperator>(Node->getSubExpr())) 1172 OS << ' '; 1173 break; 1174 } 1175 } 1176 PrintExpr(Node->getSubExpr()); 1177 1178 if (Node->isPostfix()) 1179 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1180} 1181 1182void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1183 OS << "__builtin_offsetof("; 1184 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1185 OS << ", "; 1186 bool PrintedSomething = false; 1187 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1188 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 1189 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 1190 // Array node 1191 OS << "["; 1192 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1193 OS << "]"; 1194 PrintedSomething = true; 1195 continue; 1196 } 1197 1198 // Skip implicit base indirections. 1199 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 1200 continue; 1201 1202 // Field or identifier node. 1203 IdentifierInfo *Id = ON.getFieldName(); 1204 if (!Id) 1205 continue; 1206 1207 if (PrintedSomething) 1208 OS << "."; 1209 else 1210 PrintedSomething = true; 1211 OS << Id->getName(); 1212 } 1213 OS << ")"; 1214} 1215 1216void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 1217 switch(Node->getKind()) { 1218 case UETT_SizeOf: 1219 OS << "sizeof"; 1220 break; 1221 case UETT_AlignOf: 1222 if (Policy.LangOpts.CPlusPlus) 1223 OS << "alignof"; 1224 else if (Policy.LangOpts.C11) 1225 OS << "_Alignof"; 1226 else 1227 OS << "__alignof"; 1228 break; 1229 case UETT_VecStep: 1230 OS << "vec_step"; 1231 break; 1232 case UETT_OpenMPRequiredSimdAlign: 1233 OS << "__builtin_omp_required_simd_align"; 1234 break; 1235 } 1236 if (Node->isArgumentType()) { 1237 OS << '('; 1238 Node->getArgumentType().print(OS, Policy); 1239 OS << ')'; 1240 } else { 1241 OS << " "; 1242 PrintExpr(Node->getArgumentExpr()); 1243 } 1244} 1245 1246void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1247 OS << "_Generic("; 1248 PrintExpr(Node->getControllingExpr()); 1249 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 1250 OS << ", "; 1251 QualType T = Node->getAssocType(i); 1252 if (T.isNull()) 1253 OS << "default"; 1254 else 1255 T.print(OS, Policy); 1256 OS << ": "; 1257 PrintExpr(Node->getAssocExpr(i)); 1258 } 1259 OS << ")"; 1260} 1261 1262void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1263 PrintExpr(Node->getLHS()); 1264 OS << "["; 1265 PrintExpr(Node->getRHS()); 1266 OS << "]"; 1267} 1268 1269void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1270 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1271 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1272 // Don't print any defaulted arguments 1273 break; 1274 } 1275 1276 if (i) OS << ", "; 1277 PrintExpr(Call->getArg(i)); 1278 } 1279} 1280 1281void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1282 PrintExpr(Call->getCallee()); 1283 OS << "("; 1284 PrintCallArgs(Call); 1285 OS << ")"; 1286} 1287void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1288 // FIXME: Suppress printing implicit bases (like "this") 1289 PrintExpr(Node->getBase()); 1290 1291 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1292 FieldDecl *ParentDecl = ParentMember 1293 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr; 1294 1295 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1296 OS << (Node->isArrow() ? "->" : "."); 1297 1298 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1299 if (FD->isAnonymousStructOrUnion()) 1300 return; 1301 1302 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1303 Qualifier->print(OS, Policy); 1304 if (Node->hasTemplateKeyword()) 1305 OS << "template "; 1306 OS << Node->getMemberNameInfo(); 1307 if (Node->hasExplicitTemplateArgs()) 1308 TemplateSpecializationType::PrintTemplateArgumentList( 1309 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1310} 1311void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1312 PrintExpr(Node->getBase()); 1313 OS << (Node->isArrow() ? "->isa" : ".isa"); 1314} 1315 1316void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1317 PrintExpr(Node->getBase()); 1318 OS << "."; 1319 OS << Node->getAccessor().getName(); 1320} 1321void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1322 OS << '('; 1323 Node->getTypeAsWritten().print(OS, Policy); 1324 OS << ')'; 1325 PrintExpr(Node->getSubExpr()); 1326} 1327void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1328 OS << '('; 1329 Node->getType().print(OS, Policy); 1330 OS << ')'; 1331 PrintExpr(Node->getInitializer()); 1332} 1333void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1334 // No need to print anything, simply forward to the subexpression. 1335 PrintExpr(Node->getSubExpr()); 1336} 1337void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1338 PrintExpr(Node->getLHS()); 1339 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1340 PrintExpr(Node->getRHS()); 1341} 1342void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1343 PrintExpr(Node->getLHS()); 1344 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1345 PrintExpr(Node->getRHS()); 1346} 1347void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1348 PrintExpr(Node->getCond()); 1349 OS << " ? "; 1350 PrintExpr(Node->getLHS()); 1351 OS << " : "; 1352 PrintExpr(Node->getRHS()); 1353} 1354 1355// GNU extensions. 1356 1357void 1358StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1359 PrintExpr(Node->getCommon()); 1360 OS << " ?: "; 1361 PrintExpr(Node->getFalseExpr()); 1362} 1363void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1364 OS << "&&" << Node->getLabel()->getName(); 1365} 1366 1367void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1368 OS << "("; 1369 PrintRawCompoundStmt(E->getSubStmt()); 1370 OS << ")"; 1371} 1372 1373void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1374 OS << "__builtin_choose_expr("; 1375 PrintExpr(Node->getCond()); 1376 OS << ", "; 1377 PrintExpr(Node->getLHS()); 1378 OS << ", "; 1379 PrintExpr(Node->getRHS()); 1380 OS << ")"; 1381} 1382 1383void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1384 OS << "__null"; 1385} 1386 1387void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1388 OS << "__builtin_shufflevector("; 1389 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1390 if (i) OS << ", "; 1391 PrintExpr(Node->getExpr(i)); 1392 } 1393 OS << ")"; 1394} 1395 1396void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1397 OS << "__builtin_convertvector("; 1398 PrintExpr(Node->getSrcExpr()); 1399 OS << ", "; 1400 Node->getType().print(OS, Policy); 1401 OS << ")"; 1402} 1403 1404void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1405 if (Node->getSyntacticForm()) { 1406 Visit(Node->getSyntacticForm()); 1407 return; 1408 } 1409 1410 OS << "{"; 1411 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1412 if (i) OS << ", "; 1413 if (Node->getInit(i)) 1414 PrintExpr(Node->getInit(i)); 1415 else 1416 OS << "{}"; 1417 } 1418 OS << "}"; 1419} 1420 1421void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1422 OS << "("; 1423 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1424 if (i) OS << ", "; 1425 PrintExpr(Node->getExpr(i)); 1426 } 1427 OS << ")"; 1428} 1429 1430void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1431 bool NeedsEquals = true; 1432 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1433 DEnd = Node->designators_end(); 1434 D != DEnd; ++D) { 1435 if (D->isFieldDesignator()) { 1436 if (D->getDotLoc().isInvalid()) { 1437 if (IdentifierInfo *II = D->getFieldName()) { 1438 OS << II->getName() << ":"; 1439 NeedsEquals = false; 1440 } 1441 } else { 1442 OS << "." << D->getFieldName()->getName(); 1443 } 1444 } else { 1445 OS << "["; 1446 if (D->isArrayDesignator()) { 1447 PrintExpr(Node->getArrayIndex(*D)); 1448 } else { 1449 PrintExpr(Node->getArrayRangeStart(*D)); 1450 OS << " ... "; 1451 PrintExpr(Node->getArrayRangeEnd(*D)); 1452 } 1453 OS << "]"; 1454 } 1455 } 1456 1457 if (NeedsEquals) 1458 OS << " = "; 1459 else 1460 OS << " "; 1461 PrintExpr(Node->getInit()); 1462} 1463 1464void StmtPrinter::VisitDesignatedInitUpdateExpr( 1465 DesignatedInitUpdateExpr *Node) { 1466 OS << "{"; 1467 OS << "/*base*/"; 1468 PrintExpr(Node->getBase()); 1469 OS << ", "; 1470 1471 OS << "/*updater*/"; 1472 PrintExpr(Node->getUpdater()); 1473 OS << "}"; 1474} 1475 1476void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) { 1477 OS << "/*no init*/"; 1478} 1479 1480void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1481 if (Policy.LangOpts.CPlusPlus) { 1482 OS << "/*implicit*/"; 1483 Node->getType().print(OS, Policy); 1484 OS << "()"; 1485 } else { 1486 OS << "/*implicit*/("; 1487 Node->getType().print(OS, Policy); 1488 OS << ')'; 1489 if (Node->getType()->isRecordType()) 1490 OS << "{}"; 1491 else 1492 OS << 0; 1493 } 1494} 1495 1496void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1497 OS << "__builtin_va_arg("; 1498 PrintExpr(Node->getSubExpr()); 1499 OS << ", "; 1500 Node->getType().print(OS, Policy); 1501 OS << ")"; 1502} 1503 1504void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1505 PrintExpr(Node->getSyntacticForm()); 1506} 1507 1508void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1509 const char *Name = nullptr; 1510 switch (Node->getOp()) { 1511#define BUILTIN(ID, TYPE, ATTRS) 1512#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1513 case AtomicExpr::AO ## ID: \ 1514 Name = #ID "("; \ 1515 break; 1516#include "clang/Basic/Builtins.def" 1517 } 1518 OS << Name; 1519 1520 // AtomicExpr stores its subexpressions in a permuted order. 1521 PrintExpr(Node->getPtr()); 1522 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1523 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1524 OS << ", "; 1525 PrintExpr(Node->getVal1()); 1526 } 1527 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1528 Node->isCmpXChg()) { 1529 OS << ", "; 1530 PrintExpr(Node->getVal2()); 1531 } 1532 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1533 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1534 OS << ", "; 1535 PrintExpr(Node->getWeak()); 1536 } 1537 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1538 OS << ", "; 1539 PrintExpr(Node->getOrder()); 1540 } 1541 if (Node->isCmpXChg()) { 1542 OS << ", "; 1543 PrintExpr(Node->getOrderFail()); 1544 } 1545 OS << ")"; 1546} 1547 1548// C++ 1549void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1550 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1551 "", 1552#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1553 Spelling, 1554#include "clang/Basic/OperatorKinds.def" 1555 }; 1556 1557 OverloadedOperatorKind Kind = Node->getOperator(); 1558 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1559 if (Node->getNumArgs() == 1) { 1560 OS << OpStrings[Kind] << ' '; 1561 PrintExpr(Node->getArg(0)); 1562 } else { 1563 PrintExpr(Node->getArg(0)); 1564 OS << ' ' << OpStrings[Kind]; 1565 } 1566 } else if (Kind == OO_Arrow) { 1567 PrintExpr(Node->getArg(0)); 1568 } else if (Kind == OO_Call) { 1569 PrintExpr(Node->getArg(0)); 1570 OS << '('; 1571 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1572 if (ArgIdx > 1) 1573 OS << ", "; 1574 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1575 PrintExpr(Node->getArg(ArgIdx)); 1576 } 1577 OS << ')'; 1578 } else if (Kind == OO_Subscript) { 1579 PrintExpr(Node->getArg(0)); 1580 OS << '['; 1581 PrintExpr(Node->getArg(1)); 1582 OS << ']'; 1583 } else if (Node->getNumArgs() == 1) { 1584 OS << OpStrings[Kind] << ' '; 1585 PrintExpr(Node->getArg(0)); 1586 } else if (Node->getNumArgs() == 2) { 1587 PrintExpr(Node->getArg(0)); 1588 OS << ' ' << OpStrings[Kind] << ' '; 1589 PrintExpr(Node->getArg(1)); 1590 } else { 1591 llvm_unreachable("unknown overloaded operator"); 1592 } 1593} 1594 1595void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1596 // If we have a conversion operator call only print the argument. 1597 CXXMethodDecl *MD = Node->getMethodDecl(); 1598 if (MD && isa<CXXConversionDecl>(MD)) { 1599 PrintExpr(Node->getImplicitObjectArgument()); 1600 return; 1601 } 1602 VisitCallExpr(cast<CallExpr>(Node)); 1603} 1604 1605void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1606 PrintExpr(Node->getCallee()); 1607 OS << "<<<"; 1608 PrintCallArgs(Node->getConfig()); 1609 OS << ">>>("; 1610 PrintCallArgs(Node); 1611 OS << ")"; 1612} 1613 1614void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1615 OS << Node->getCastName() << '<'; 1616 Node->getTypeAsWritten().print(OS, Policy); 1617 OS << ">("; 1618 PrintExpr(Node->getSubExpr()); 1619 OS << ")"; 1620} 1621 1622void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1623 VisitCXXNamedCastExpr(Node); 1624} 1625 1626void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1627 VisitCXXNamedCastExpr(Node); 1628} 1629 1630void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1631 VisitCXXNamedCastExpr(Node); 1632} 1633 1634void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1635 VisitCXXNamedCastExpr(Node); 1636} 1637 1638void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1639 OS << "typeid("; 1640 if (Node->isTypeOperand()) { 1641 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1642 } else { 1643 PrintExpr(Node->getExprOperand()); 1644 } 1645 OS << ")"; 1646} 1647 1648void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1649 OS << "__uuidof("; 1650 if (Node->isTypeOperand()) { 1651 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1652 } else { 1653 PrintExpr(Node->getExprOperand()); 1654 } 1655 OS << ")"; 1656} 1657 1658void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1659 PrintExpr(Node->getBaseExpr()); 1660 if (Node->isArrow()) 1661 OS << "->"; 1662 else 1663 OS << "."; 1664 if (NestedNameSpecifier *Qualifier = 1665 Node->getQualifierLoc().getNestedNameSpecifier()) 1666 Qualifier->print(OS, Policy); 1667 OS << Node->getPropertyDecl()->getDeclName(); 1668} 1669 1670void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1671 switch (Node->getLiteralOperatorKind()) { 1672 case UserDefinedLiteral::LOK_Raw: 1673 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1674 break; 1675 case UserDefinedLiteral::LOK_Template: { 1676 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1677 const TemplateArgumentList *Args = 1678 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1679 assert(Args); 1680 1681 if (Args->size() != 1) { 1682 OS << "operator \"\" " << Node->getUDSuffix()->getName(); 1683 TemplateSpecializationType::PrintTemplateArgumentList( 1684 OS, Args->data(), Args->size(), Policy); 1685 OS << "()"; 1686 return; 1687 } 1688 1689 const TemplateArgument &Pack = Args->get(0); 1690 for (const auto &P : Pack.pack_elements()) { 1691 char C = (char)P.getAsIntegral().getZExtValue(); 1692 OS << C; 1693 } 1694 break; 1695 } 1696 case UserDefinedLiteral::LOK_Integer: { 1697 // Print integer literal without suffix. 1698 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1699 OS << Int->getValue().toString(10, /*isSigned*/false); 1700 break; 1701 } 1702 case UserDefinedLiteral::LOK_Floating: { 1703 // Print floating literal without suffix. 1704 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1705 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1706 break; 1707 } 1708 case UserDefinedLiteral::LOK_String: 1709 case UserDefinedLiteral::LOK_Character: 1710 PrintExpr(Node->getCookedLiteral()); 1711 break; 1712 } 1713 OS << Node->getUDSuffix()->getName(); 1714} 1715 1716void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1717 OS << (Node->getValue() ? "true" : "false"); 1718} 1719 1720void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1721 OS << "nullptr"; 1722} 1723 1724void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1725 OS << "this"; 1726} 1727 1728void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1729 if (!Node->getSubExpr()) 1730 OS << "throw"; 1731 else { 1732 OS << "throw "; 1733 PrintExpr(Node->getSubExpr()); 1734 } 1735} 1736 1737void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1738 // Nothing to print: we picked up the default argument. 1739} 1740 1741void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1742 // Nothing to print: we picked up the default initializer. 1743} 1744 1745void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1746 Node->getType().print(OS, Policy); 1747 // If there are no parens, this is list-initialization, and the braces are 1748 // part of the syntax of the inner construct. 1749 if (Node->getLParenLoc().isValid()) 1750 OS << "("; 1751 PrintExpr(Node->getSubExpr()); 1752 if (Node->getLParenLoc().isValid()) 1753 OS << ")"; 1754} 1755 1756void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1757 PrintExpr(Node->getSubExpr()); 1758} 1759 1760void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1761 Node->getType().print(OS, Policy); 1762 if (Node->isStdInitListInitialization()) 1763 /* Nothing to do; braces are part of creating the std::initializer_list. */; 1764 else if (Node->isListInitialization()) 1765 OS << "{"; 1766 else 1767 OS << "("; 1768 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1769 ArgEnd = Node->arg_end(); 1770 Arg != ArgEnd; ++Arg) { 1771 if (Arg->isDefaultArgument()) 1772 break; 1773 if (Arg != Node->arg_begin()) 1774 OS << ", "; 1775 PrintExpr(*Arg); 1776 } 1777 if (Node->isStdInitListInitialization()) 1778 /* See above. */; 1779 else if (Node->isListInitialization()) 1780 OS << "}"; 1781 else 1782 OS << ")"; 1783} 1784 1785void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1786 OS << '['; 1787 bool NeedComma = false; 1788 switch (Node->getCaptureDefault()) { 1789 case LCD_None: 1790 break; 1791 1792 case LCD_ByCopy: 1793 OS << '='; 1794 NeedComma = true; 1795 break; 1796 1797 case LCD_ByRef: 1798 OS << '&'; 1799 NeedComma = true; 1800 break; 1801 } 1802 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1803 CEnd = Node->explicit_capture_end(); 1804 C != CEnd; 1805 ++C) { 1806 if (NeedComma) 1807 OS << ", "; 1808 NeedComma = true; 1809 1810 switch (C->getCaptureKind()) { 1811 case LCK_This: 1812 OS << "this"; 1813 break; 1814 1815 case LCK_ByRef: 1816 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C)) 1817 OS << '&'; 1818 OS << C->getCapturedVar()->getName(); 1819 break; 1820 1821 case LCK_ByCopy: 1822 OS << C->getCapturedVar()->getName(); 1823 break; 1824 case LCK_VLAType: 1825 llvm_unreachable("VLA type in explicit captures."); 1826 } 1827 1828 if (Node->isInitCapture(C)) 1829 PrintExpr(C->getCapturedVar()->getInit()); 1830 } 1831 OS << ']'; 1832 1833 if (Node->hasExplicitParameters()) { 1834 OS << " ("; 1835 CXXMethodDecl *Method = Node->getCallOperator(); 1836 NeedComma = false; 1837 for (auto P : Method->params()) { 1838 if (NeedComma) { 1839 OS << ", "; 1840 } else { 1841 NeedComma = true; 1842 } 1843 std::string ParamStr = P->getNameAsString(); 1844 P->getOriginalType().print(OS, Policy, ParamStr); 1845 } 1846 if (Method->isVariadic()) { 1847 if (NeedComma) 1848 OS << ", "; 1849 OS << "..."; 1850 } 1851 OS << ')'; 1852 1853 if (Node->isMutable()) 1854 OS << " mutable"; 1855 1856 const FunctionProtoType *Proto 1857 = Method->getType()->getAs<FunctionProtoType>(); 1858 Proto->printExceptionSpecification(OS, Policy); 1859 1860 // FIXME: Attributes 1861 1862 // Print the trailing return type if it was specified in the source. 1863 if (Node->hasExplicitResultType()) { 1864 OS << " -> "; 1865 Proto->getReturnType().print(OS, Policy); 1866 } 1867 } 1868 1869 // Print the body. 1870 CompoundStmt *Body = Node->getBody(); 1871 OS << ' '; 1872 PrintStmt(Body); 1873} 1874 1875void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1876 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1877 TSInfo->getType().print(OS, Policy); 1878 else 1879 Node->getType().print(OS, Policy); 1880 OS << "()"; 1881} 1882 1883void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1884 if (E->isGlobalNew()) 1885 OS << "::"; 1886 OS << "new "; 1887 unsigned NumPlace = E->getNumPlacementArgs(); 1888 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1889 OS << "("; 1890 PrintExpr(E->getPlacementArg(0)); 1891 for (unsigned i = 1; i < NumPlace; ++i) { 1892 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1893 break; 1894 OS << ", "; 1895 PrintExpr(E->getPlacementArg(i)); 1896 } 1897 OS << ") "; 1898 } 1899 if (E->isParenTypeId()) 1900 OS << "("; 1901 std::string TypeS; 1902 if (Expr *Size = E->getArraySize()) { 1903 llvm::raw_string_ostream s(TypeS); 1904 s << '['; 1905 Size->printPretty(s, Helper, Policy); 1906 s << ']'; 1907 } 1908 E->getAllocatedType().print(OS, Policy, TypeS); 1909 if (E->isParenTypeId()) 1910 OS << ")"; 1911 1912 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1913 if (InitStyle) { 1914 if (InitStyle == CXXNewExpr::CallInit) 1915 OS << "("; 1916 PrintExpr(E->getInitializer()); 1917 if (InitStyle == CXXNewExpr::CallInit) 1918 OS << ")"; 1919 } 1920} 1921 1922void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1923 if (E->isGlobalDelete()) 1924 OS << "::"; 1925 OS << "delete "; 1926 if (E->isArrayForm()) 1927 OS << "[] "; 1928 PrintExpr(E->getArgument()); 1929} 1930 1931void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1932 PrintExpr(E->getBase()); 1933 if (E->isArrow()) 1934 OS << "->"; 1935 else 1936 OS << '.'; 1937 if (E->getQualifier()) 1938 E->getQualifier()->print(OS, Policy); 1939 OS << "~"; 1940 1941 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1942 OS << II->getName(); 1943 else 1944 E->getDestroyedType().print(OS, Policy); 1945} 1946 1947void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1948 if (E->isListInitialization() && !E->isStdInitListInitialization()) 1949 OS << "{"; 1950 1951 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1952 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1953 // Don't print any defaulted arguments 1954 break; 1955 } 1956 1957 if (i) OS << ", "; 1958 PrintExpr(E->getArg(i)); 1959 } 1960 1961 if (E->isListInitialization() && !E->isStdInitListInitialization()) 1962 OS << "}"; 1963} 1964 1965void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 1966 PrintExpr(E->getSubExpr()); 1967} 1968 1969void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1970 // Just forward to the subexpression. 1971 PrintExpr(E->getSubExpr()); 1972} 1973 1974void 1975StmtPrinter::VisitCXXUnresolvedConstructExpr( 1976 CXXUnresolvedConstructExpr *Node) { 1977 Node->getTypeAsWritten().print(OS, Policy); 1978 OS << "("; 1979 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1980 ArgEnd = Node->arg_end(); 1981 Arg != ArgEnd; ++Arg) { 1982 if (Arg != Node->arg_begin()) 1983 OS << ", "; 1984 PrintExpr(*Arg); 1985 } 1986 OS << ")"; 1987} 1988 1989void StmtPrinter::VisitCXXDependentScopeMemberExpr( 1990 CXXDependentScopeMemberExpr *Node) { 1991 if (!Node->isImplicitAccess()) { 1992 PrintExpr(Node->getBase()); 1993 OS << (Node->isArrow() ? "->" : "."); 1994 } 1995 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1996 Qualifier->print(OS, Policy); 1997 if (Node->hasTemplateKeyword()) 1998 OS << "template "; 1999 OS << Node->getMemberNameInfo(); 2000 if (Node->hasExplicitTemplateArgs()) 2001 TemplateSpecializationType::PrintTemplateArgumentList( 2002 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 2003} 2004 2005void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 2006 if (!Node->isImplicitAccess()) { 2007 PrintExpr(Node->getBase()); 2008 OS << (Node->isArrow() ? "->" : "."); 2009 } 2010 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2011 Qualifier->print(OS, Policy); 2012 if (Node->hasTemplateKeyword()) 2013 OS << "template "; 2014 OS << Node->getMemberNameInfo(); 2015 if (Node->hasExplicitTemplateArgs()) 2016 TemplateSpecializationType::PrintTemplateArgumentList( 2017 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 2018} 2019 2020static const char *getTypeTraitName(TypeTrait TT) { 2021 switch (TT) { 2022#define TYPE_TRAIT_1(Spelling, Name, Key) \ 2023case clang::UTT_##Name: return #Spelling; 2024#define TYPE_TRAIT_2(Spelling, Name, Key) \ 2025case clang::BTT_##Name: return #Spelling; 2026#define TYPE_TRAIT_N(Spelling, Name, Key) \ 2027 case clang::TT_##Name: return #Spelling; 2028#include "clang/Basic/TokenKinds.def" 2029 } 2030 llvm_unreachable("Type trait not covered by switch"); 2031} 2032 2033static const char *getTypeTraitName(ArrayTypeTrait ATT) { 2034 switch (ATT) { 2035 case ATT_ArrayRank: return "__array_rank"; 2036 case ATT_ArrayExtent: return "__array_extent"; 2037 } 2038 llvm_unreachable("Array type trait not covered by switch"); 2039} 2040 2041static const char *getExpressionTraitName(ExpressionTrait ET) { 2042 switch (ET) { 2043 case ET_IsLValueExpr: return "__is_lvalue_expr"; 2044 case ET_IsRValueExpr: return "__is_rvalue_expr"; 2045 } 2046 llvm_unreachable("Expression type trait not covered by switch"); 2047} 2048 2049void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 2050 OS << getTypeTraitName(E->getTrait()) << "("; 2051 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 2052 if (I > 0) 2053 OS << ", "; 2054 E->getArg(I)->getType().print(OS, Policy); 2055 } 2056 OS << ")"; 2057} 2058 2059void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2060 OS << getTypeTraitName(E->getTrait()) << '('; 2061 E->getQueriedType().print(OS, Policy); 2062 OS << ')'; 2063} 2064 2065void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2066 OS << getExpressionTraitName(E->getTrait()) << '('; 2067 PrintExpr(E->getQueriedExpression()); 2068 OS << ')'; 2069} 2070 2071void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 2072 OS << "noexcept("; 2073 PrintExpr(E->getOperand()); 2074 OS << ")"; 2075} 2076 2077void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 2078 PrintExpr(E->getPattern()); 2079 OS << "..."; 2080} 2081 2082void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2083 OS << "sizeof...(" << *E->getPack() << ")"; 2084} 2085 2086void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 2087 SubstNonTypeTemplateParmPackExpr *Node) { 2088 OS << *Node->getParameterPack(); 2089} 2090 2091void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 2092 SubstNonTypeTemplateParmExpr *Node) { 2093 Visit(Node->getReplacement()); 2094} 2095 2096void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 2097 OS << *E->getParameterPack(); 2098} 2099 2100void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 2101 PrintExpr(Node->GetTemporaryExpr()); 2102} 2103 2104void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 2105 OS << "("; 2106 if (E->getLHS()) { 2107 PrintExpr(E->getLHS()); 2108 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2109 } 2110 OS << "..."; 2111 if (E->getRHS()) { 2112 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2113 PrintExpr(E->getRHS()); 2114 } 2115 OS << ")"; 2116} 2117 2118// Obj-C 2119 2120void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 2121 OS << "@"; 2122 VisitStringLiteral(Node->getString()); 2123} 2124 2125void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 2126 OS << "@"; 2127 Visit(E->getSubExpr()); 2128} 2129 2130void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 2131 OS << "@[ "; 2132 StmtRange ch = E->children(); 2133 if (ch.first != ch.second) { 2134 while (1) { 2135 Visit(*ch.first); 2136 ++ch.first; 2137 if (ch.first == ch.second) break; 2138 OS << ", "; 2139 } 2140 } 2141 OS << " ]"; 2142} 2143 2144void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 2145 OS << "@{ "; 2146 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 2147 if (I > 0) 2148 OS << ", "; 2149 2150 ObjCDictionaryElement Element = E->getKeyValueElement(I); 2151 Visit(Element.Key); 2152 OS << " : "; 2153 Visit(Element.Value); 2154 if (Element.isPackExpansion()) 2155 OS << "..."; 2156 } 2157 OS << " }"; 2158} 2159 2160void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 2161 OS << "@encode("; 2162 Node->getEncodedType().print(OS, Policy); 2163 OS << ')'; 2164} 2165 2166void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 2167 OS << "@selector("; 2168 Node->getSelector().print(OS); 2169 OS << ')'; 2170} 2171 2172void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 2173 OS << "@protocol(" << *Node->getProtocol() << ')'; 2174} 2175 2176void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 2177 OS << "["; 2178 switch (Mess->getReceiverKind()) { 2179 case ObjCMessageExpr::Instance: 2180 PrintExpr(Mess->getInstanceReceiver()); 2181 break; 2182 2183 case ObjCMessageExpr::Class: 2184 Mess->getClassReceiver().print(OS, Policy); 2185 break; 2186 2187 case ObjCMessageExpr::SuperInstance: 2188 case ObjCMessageExpr::SuperClass: 2189 OS << "Super"; 2190 break; 2191 } 2192 2193 OS << ' '; 2194 Selector selector = Mess->getSelector(); 2195 if (selector.isUnarySelector()) { 2196 OS << selector.getNameForSlot(0); 2197 } else { 2198 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 2199 if (i < selector.getNumArgs()) { 2200 if (i > 0) OS << ' '; 2201 if (selector.getIdentifierInfoForSlot(i)) 2202 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 2203 else 2204 OS << ":"; 2205 } 2206 else OS << ", "; // Handle variadic methods. 2207 2208 PrintExpr(Mess->getArg(i)); 2209 } 2210 } 2211 OS << "]"; 2212} 2213 2214void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 2215 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 2216} 2217 2218void 2219StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2220 PrintExpr(E->getSubExpr()); 2221} 2222 2223void 2224StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2225 OS << '(' << E->getBridgeKindName(); 2226 E->getType().print(OS, Policy); 2227 OS << ')'; 2228 PrintExpr(E->getSubExpr()); 2229} 2230 2231void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2232 BlockDecl *BD = Node->getBlockDecl(); 2233 OS << "^"; 2234 2235 const FunctionType *AFT = Node->getFunctionType(); 2236 2237 if (isa<FunctionNoProtoType>(AFT)) { 2238 OS << "()"; 2239 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2240 OS << '('; 2241 for (BlockDecl::param_iterator AI = BD->param_begin(), 2242 E = BD->param_end(); AI != E; ++AI) { 2243 if (AI != BD->param_begin()) OS << ", "; 2244 std::string ParamStr = (*AI)->getNameAsString(); 2245 (*AI)->getType().print(OS, Policy, ParamStr); 2246 } 2247 2248 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2249 if (FT->isVariadic()) { 2250 if (!BD->param_empty()) OS << ", "; 2251 OS << "..."; 2252 } 2253 OS << ')'; 2254 } 2255 OS << "{ }"; 2256} 2257 2258void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2259 PrintExpr(Node->getSourceExpr()); 2260} 2261 2262void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 2263 // TODO: Print something reasonable for a TypoExpr, if necessary. 2264 assert(false && "Cannot print TypoExpr nodes"); 2265} 2266 2267void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2268 OS << "__builtin_astype("; 2269 PrintExpr(Node->getSrcExpr()); 2270 OS << ", "; 2271 Node->getType().print(OS, Policy); 2272 OS << ")"; 2273} 2274 2275//===----------------------------------------------------------------------===// 2276// Stmt method implementations 2277//===----------------------------------------------------------------------===// 2278 2279void Stmt::dumpPretty(const ASTContext &Context) const { 2280 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2281} 2282 2283void Stmt::printPretty(raw_ostream &OS, 2284 PrinterHelper *Helper, 2285 const PrintingPolicy &Policy, 2286 unsigned Indentation) const { 2287 StmtPrinter P(OS, Helper, Policy, Indentation); 2288 P.Visit(const_cast<Stmt*>(this)); 2289} 2290 2291//===----------------------------------------------------------------------===// 2292// PrinterHelper 2293//===----------------------------------------------------------------------===// 2294 2295// Implement virtual destructor. 2296PrinterHelper::~PrinterHelper() {} 2297