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