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