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 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind()); 656 if (Node->getChunkSize()) { 657 OS << ", "; 658 Node->getChunkSize()->printPretty(OS, nullptr, Policy); 659 } 660 OS << ")"; 661 } 662 663 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) { 664 OS << "ordered"; 665 if (auto *Num = Node->getNumForLoops()) { 666 OS << "("; 667 Num->printPretty(OS, nullptr, Policy, 0); 668 OS << ")"; 669 } 670 } 671 672 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) { 673 OS << "nowait"; 674 } 675 676 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) { 677 OS << "untied"; 678 } 679 680 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) { 681 OS << "nogroup"; 682 } 683 684 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) { 685 OS << "mergeable"; 686 } 687 688 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; } 689 690 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; } 691 692 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) { 693 OS << "update"; 694 } 695 696 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) { 697 OS << "capture"; 698 } 699 700 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) { 701 OS << "seq_cst"; 702 } 703 704 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) { 705 OS << "threads"; 706 } 707 708 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; } 709 710 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) { 711 OS << "device("; 712 Node->getDevice()->printPretty(OS, nullptr, Policy, 0); 713 OS << ")"; 714 } 715 716 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) { 717 OS << "num_teams("; 718 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0); 719 OS << ")"; 720 } 721 722 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) { 723 OS << "thread_limit("; 724 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0); 725 OS << ")"; 726 } 727 728 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) { 729 OS << "priority("; 730 Node->getPriority()->printPretty(OS, nullptr, Policy, 0); 731 OS << ")"; 732 } 733 734 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) { 735 OS << "grainsize("; 736 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0); 737 OS << ")"; 738 } 739 740 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) { 741 OS << "num_tasks("; 742 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0); 743 OS << ")"; 744 } 745 746 template<typename T> 747 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) { 748 for (typename T::varlist_iterator I = Node->varlist_begin(), 749 E = Node->varlist_end(); 750 I != E; ++I) { 751 assert(*I && "Expected non-null Stmt"); 752 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) { 753 OS << (I == Node->varlist_begin() ? StartSym : ','); 754 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS); 755 } else { 756 OS << (I == Node->varlist_begin() ? StartSym : ','); 757 (*I)->printPretty(OS, nullptr, Policy, 0); 758 } 759 } 760 } 761 762 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) { 763 if (!Node->varlist_empty()) { 764 OS << "private"; 765 VisitOMPClauseList(Node, '('); 766 OS << ")"; 767 } 768 } 769 770 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) { 771 if (!Node->varlist_empty()) { 772 OS << "firstprivate"; 773 VisitOMPClauseList(Node, '('); 774 OS << ")"; 775 } 776 } 777 778 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) { 779 if (!Node->varlist_empty()) { 780 OS << "lastprivate"; 781 VisitOMPClauseList(Node, '('); 782 OS << ")"; 783 } 784 } 785 786 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) { 787 if (!Node->varlist_empty()) { 788 OS << "shared"; 789 VisitOMPClauseList(Node, '('); 790 OS << ")"; 791 } 792 } 793 794 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) { 795 if (!Node->varlist_empty()) { 796 OS << "reduction("; 797 NestedNameSpecifier *QualifierLoc = 798 Node->getQualifierLoc().getNestedNameSpecifier(); 799 OverloadedOperatorKind OOK = 800 Node->getNameInfo().getName().getCXXOverloadedOperator(); 801 if (QualifierLoc == nullptr && OOK != OO_None) { 802 // Print reduction identifier in C format 803 OS << getOperatorSpelling(OOK); 804 } else { 805 // Use C++ format 806 if (QualifierLoc != nullptr) 807 QualifierLoc->print(OS, Policy); 808 OS << Node->getNameInfo(); 809 } 810 OS << ":"; 811 VisitOMPClauseList(Node, ' '); 812 OS << ")"; 813 } 814 } 815 816 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) { 817 if (!Node->varlist_empty()) { 818 OS << "linear"; 819 if (Node->getModifierLoc().isValid()) { 820 OS << '(' 821 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier()); 822 } 823 VisitOMPClauseList(Node, '('); 824 if (Node->getModifierLoc().isValid()) 825 OS << ')'; 826 if (Node->getStep() != nullptr) { 827 OS << ": "; 828 Node->getStep()->printPretty(OS, nullptr, Policy, 0); 829 } 830 OS << ")"; 831 } 832 } 833 834 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) { 835 if (!Node->varlist_empty()) { 836 OS << "aligned"; 837 VisitOMPClauseList(Node, '('); 838 if (Node->getAlignment() != nullptr) { 839 OS << ": "; 840 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0); 841 } 842 OS << ")"; 843 } 844 } 845 846 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) { 847 if (!Node->varlist_empty()) { 848 OS << "copyin"; 849 VisitOMPClauseList(Node, '('); 850 OS << ")"; 851 } 852 } 853 854 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) { 855 if (!Node->varlist_empty()) { 856 OS << "copyprivate"; 857 VisitOMPClauseList(Node, '('); 858 OS << ")"; 859 } 860 } 861 862 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) { 863 if (!Node->varlist_empty()) { 864 VisitOMPClauseList(Node, '('); 865 OS << ")"; 866 } 867 } 868 869 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) { 870 if (!Node->varlist_empty()) { 871 OS << "depend("; 872 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), 873 Node->getDependencyKind()) 874 << " :"; 875 VisitOMPClauseList(Node, ' '); 876 OS << ")"; 877 } 878 } 879 880 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) { 881 if (!Node->varlist_empty()) { 882 OS << "map("; 883 if (Node->getMapType() != OMPC_MAP_unknown) { 884 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) { 885 OS << getOpenMPSimpleClauseTypeName(OMPC_map, 886 Node->getMapTypeModifier()); 887 OS << ','; 888 } 889 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType()); 890 OS << ':'; 891 } 892 VisitOMPClauseList(Node, ' '); 893 OS << ")"; 894 } 895 } 896 } 897 898 //===----------------------------------------------------------------------===// 899 // OpenMP directives printing methods 900 //===----------------------------------------------------------------------===// 901 902 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) { 903 OMPClausePrinter Printer(OS, Policy); 904 ArrayRef<OMPClause *> Clauses = S->clauses(); 905 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 906 I != E; ++I) 907 if (*I && !(*I)->isImplicit()) { 908 Printer.Visit(*I); 909 OS << ' '; 910 } 911 OS << "\n"; 912 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) { 913 assert(isa<CapturedStmt>(S->getAssociatedStmt()) && 914 "Expected captured statement!"); 915 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt(); 916 PrintStmt(CS); 917 } 918 } 919 920 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) { 921 Indent() << "#pragma omp parallel "; 922 PrintOMPExecutableDirective(Node); 923 } 924 925 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) { 926 Indent() << "#pragma omp simd "; 927 PrintOMPExecutableDirective(Node); 928 } 929 930 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) { 931 Indent() << "#pragma omp for "; 932 PrintOMPExecutableDirective(Node); 933 } 934 935 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) { 936 Indent() << "#pragma omp for simd "; 937 PrintOMPExecutableDirective(Node); 938 } 939 940 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) { 941 Indent() << "#pragma omp sections "; 942 PrintOMPExecutableDirective(Node); 943 } 944 945 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) { 946 Indent() << "#pragma omp section"; 947 PrintOMPExecutableDirective(Node); 948 } 949 950 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) { 951 Indent() << "#pragma omp single "; 952 PrintOMPExecutableDirective(Node); 953 } 954 955 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) { 956 Indent() << "#pragma omp master"; 957 PrintOMPExecutableDirective(Node); 958 } 959 960 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) { 961 Indent() << "#pragma omp critical"; 962 if (Node->getDirectiveName().getName()) { 963 OS << " ("; 964 Node->getDirectiveName().printName(OS); 965 OS << ")"; 966 } 967 PrintOMPExecutableDirective(Node); 968 } 969 970 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) { 971 Indent() << "#pragma omp parallel for "; 972 PrintOMPExecutableDirective(Node); 973 } 974 975 void StmtPrinter::VisitOMPParallelForSimdDirective( 976 OMPParallelForSimdDirective *Node) { 977 Indent() << "#pragma omp parallel for simd "; 978 PrintOMPExecutableDirective(Node); 979 } 980 981 void StmtPrinter::VisitOMPParallelSectionsDirective( 982 OMPParallelSectionsDirective *Node) { 983 Indent() << "#pragma omp parallel sections "; 984 PrintOMPExecutableDirective(Node); 985 } 986 987 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) { 988 Indent() << "#pragma omp task "; 989 PrintOMPExecutableDirective(Node); 990 } 991 992 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) { 993 Indent() << "#pragma omp taskyield"; 994 PrintOMPExecutableDirective(Node); 995 } 996 997 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) { 998 Indent() << "#pragma omp barrier"; 999 PrintOMPExecutableDirective(Node); 1000 } 1001 1002 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) { 1003 Indent() << "#pragma omp taskwait"; 1004 PrintOMPExecutableDirective(Node); 1005 } 1006 1007 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) { 1008 Indent() << "#pragma omp taskgroup"; 1009 PrintOMPExecutableDirective(Node); 1010 } 1011 1012 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) { 1013 Indent() << "#pragma omp flush "; 1014 PrintOMPExecutableDirective(Node); 1015 } 1016 1017 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) { 1018 Indent() << "#pragma omp ordered "; 1019 PrintOMPExecutableDirective(Node); 1020 } 1021 1022 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) { 1023 Indent() << "#pragma omp atomic "; 1024 PrintOMPExecutableDirective(Node); 1025 } 1026 1027 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) { 1028 Indent() << "#pragma omp target "; 1029 PrintOMPExecutableDirective(Node); 1030 } 1031 1032 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) { 1033 Indent() << "#pragma omp target data "; 1034 PrintOMPExecutableDirective(Node); 1035 } 1036 1037 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) { 1038 Indent() << "#pragma omp teams "; 1039 PrintOMPExecutableDirective(Node); 1040 } 1041 1042 void StmtPrinter::VisitOMPCancellationPointDirective( 1043 OMPCancellationPointDirective *Node) { 1044 Indent() << "#pragma omp cancellation point " 1045 << getOpenMPDirectiveName(Node->getCancelRegion()); 1046 PrintOMPExecutableDirective(Node); 1047 } 1048 1049 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) { 1050 Indent() << "#pragma omp cancel " 1051 << getOpenMPDirectiveName(Node->getCancelRegion()) << " "; 1052 PrintOMPExecutableDirective(Node); 1053 } 1054 1055 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) { 1056 Indent() << "#pragma omp taskloop "; 1057 PrintOMPExecutableDirective(Node); 1058 } 1059 1060 void StmtPrinter::VisitOMPTaskLoopSimdDirective( 1061 OMPTaskLoopSimdDirective *Node) { 1062 Indent() << "#pragma omp taskloop simd "; 1063 PrintOMPExecutableDirective(Node); 1064 } 1065 1066 //===----------------------------------------------------------------------===// 1067 // Expr printing methods. 1068 //===----------------------------------------------------------------------===// 1069 1070 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 1071 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1072 Qualifier->print(OS, Policy); 1073 if (Node->hasTemplateKeyword()) 1074 OS << "template "; 1075 OS << Node->getNameInfo(); 1076 if (Node->hasExplicitTemplateArgs()) 1077 TemplateSpecializationType::PrintTemplateArgumentList( 1078 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1079 } 1080 1081 void StmtPrinter::VisitDependentScopeDeclRefExpr( 1082 DependentScopeDeclRefExpr *Node) { 1083 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1084 Qualifier->print(OS, Policy); 1085 if (Node->hasTemplateKeyword()) 1086 OS << "template "; 1087 OS << Node->getNameInfo(); 1088 if (Node->hasExplicitTemplateArgs()) 1089 TemplateSpecializationType::PrintTemplateArgumentList( 1090 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1091 } 1092 1093 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 1094 if (Node->getQualifier()) 1095 Node->getQualifier()->print(OS, Policy); 1096 if (Node->hasTemplateKeyword()) 1097 OS << "template "; 1098 OS << Node->getNameInfo(); 1099 if (Node->hasExplicitTemplateArgs()) 1100 TemplateSpecializationType::PrintTemplateArgumentList( 1101 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1102 } 1103 1104 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 1105 if (Node->getBase()) { 1106 PrintExpr(Node->getBase()); 1107 OS << (Node->isArrow() ? "->" : "."); 1108 } 1109 OS << *Node->getDecl(); 1110 } 1111 1112 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 1113 if (Node->isSuperReceiver()) 1114 OS << "super."; 1115 else if (Node->isObjectReceiver() && Node->getBase()) { 1116 PrintExpr(Node->getBase()); 1117 OS << "."; 1118 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 1119 OS << Node->getClassReceiver()->getName() << "."; 1120 } 1121 1122 if (Node->isImplicitProperty()) 1123 Node->getImplicitPropertyGetter()->getSelector().print(OS); 1124 else 1125 OS << Node->getExplicitProperty()->getName(); 1126 } 1127 1128 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 1129 1130 PrintExpr(Node->getBaseExpr()); 1131 OS << "["; 1132 PrintExpr(Node->getKeyExpr()); 1133 OS << "]"; 1134 } 1135 1136 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 1137 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType()); 1138 } 1139 1140 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 1141 unsigned value = Node->getValue(); 1142 1143 switch (Node->getKind()) { 1144 case CharacterLiteral::Ascii: break; // no prefix. 1145 case CharacterLiteral::Wide: OS << 'L'; break; 1146 case CharacterLiteral::UTF16: OS << 'u'; break; 1147 case CharacterLiteral::UTF32: OS << 'U'; break; 1148 } 1149 1150 switch (value) { 1151 case '\\': 1152 OS << "'\\\\'"; 1153 break; 1154 case '\'': 1155 OS << "'\\''"; 1156 break; 1157 case '\a': 1158 // TODO: K&R: the meaning of '\\a' is different in traditional C 1159 OS << "'\\a'"; 1160 break; 1161 case '\b': 1162 OS << "'\\b'"; 1163 break; 1164 // Nonstandard escape sequence. 1165 /*case '\e': 1166 OS << "'\\e'"; 1167 break;*/ 1168 case '\f': 1169 OS << "'\\f'"; 1170 break; 1171 case '\n': 1172 OS << "'\\n'"; 1173 break; 1174 case '\r': 1175 OS << "'\\r'"; 1176 break; 1177 case '\t': 1178 OS << "'\\t'"; 1179 break; 1180 case '\v': 1181 OS << "'\\v'"; 1182 break; 1183 default: 1184 if (value < 256 && isPrintable((unsigned char)value)) 1185 OS << "'" << (char)value << "'"; 1186 else if (value < 256) 1187 OS << "'\\x" << llvm::format("%02x", value) << "'"; 1188 else if (value <= 0xFFFF) 1189 OS << "'\\u" << llvm::format("%04x", value) << "'"; 1190 else 1191 OS << "'\\U" << llvm::format("%08x", value) << "'"; 1192 } 1193 } 1194 1195 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 1196 bool isSigned = Node->getType()->isSignedIntegerType(); 1197 OS << Node->getValue().toString(10, isSigned); 1198 1199 // Emit suffixes. Integer literals are always a builtin integer type. 1200 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1201 default: llvm_unreachable("Unexpected type for integer literal!"); 1202 case BuiltinType::Char_S: 1203 case BuiltinType::Char_U: OS << "i8"; break; 1204 case BuiltinType::UChar: OS << "Ui8"; break; 1205 case BuiltinType::Short: OS << "i16"; break; 1206 case BuiltinType::UShort: OS << "Ui16"; break; 1207 case BuiltinType::Int: break; // no suffix. 1208 case BuiltinType::UInt: OS << 'U'; break; 1209 case BuiltinType::Long: OS << 'L'; break; 1210 case BuiltinType::ULong: OS << "UL"; break; 1211 case BuiltinType::LongLong: OS << "LL"; break; 1212 case BuiltinType::ULongLong: OS << "ULL"; break; 1213 } 1214 } 1215 1216 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 1217 bool PrintSuffix) { 1218 SmallString<16> Str; 1219 Node->getValue().toString(Str); 1220 OS << Str; 1221 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 1222 OS << '.'; // Trailing dot in order to separate from ints. 1223 1224 if (!PrintSuffix) 1225 return; 1226 1227 // Emit suffixes. Float literals are always a builtin float type. 1228 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 1229 default: llvm_unreachable("Unexpected type for float literal!"); 1230 case BuiltinType::Half: break; // FIXME: suffix? 1231 case BuiltinType::Double: break; // no suffix. 1232 case BuiltinType::Float: OS << 'F'; break; 1233 case BuiltinType::LongDouble: OS << 'L'; break; 1234 } 1235 } 1236 1237 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 1238 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 1239 } 1240 1241 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 1242 PrintExpr(Node->getSubExpr()); 1243 OS << "i"; 1244 } 1245 1246 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 1247 Str->outputString(OS); 1248 } 1249 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 1250 OS << "("; 1251 PrintExpr(Node->getSubExpr()); 1252 OS << ")"; 1253 } 1254 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 1255 if (!Node->isPostfix()) { 1256 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1257 1258 // Print a space if this is an "identifier operator" like __real, or if 1259 // it might be concatenated incorrectly like '+'. 1260 switch (Node->getOpcode()) { 1261 default: break; 1262 case UO_Real: 1263 case UO_Imag: 1264 case UO_Extension: 1265 OS << ' '; 1266 break; 1267 case UO_Plus: 1268 case UO_Minus: 1269 if (isa<UnaryOperator>(Node->getSubExpr())) 1270 OS << ' '; 1271 break; 1272 } 1273 } 1274 PrintExpr(Node->getSubExpr()); 1275 1276 if (Node->isPostfix()) 1277 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 1278 } 1279 1280 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 1281 OS << "__builtin_offsetof("; 1282 Node->getTypeSourceInfo()->getType().print(OS, Policy); 1283 OS << ", "; 1284 bool PrintedSomething = false; 1285 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 1286 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 1287 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 1288 // Array node 1289 OS << "["; 1290 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 1291 OS << "]"; 1292 PrintedSomething = true; 1293 continue; 1294 } 1295 1296 // Skip implicit base indirections. 1297 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 1298 continue; 1299 1300 // Field or identifier node. 1301 IdentifierInfo *Id = ON.getFieldName(); 1302 if (!Id) 1303 continue; 1304 1305 if (PrintedSomething) 1306 OS << "."; 1307 else 1308 PrintedSomething = true; 1309 OS << Id->getName(); 1310 } 1311 OS << ")"; 1312 } 1313 1314 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 1315 switch(Node->getKind()) { 1316 case UETT_SizeOf: 1317 OS << "sizeof"; 1318 break; 1319 case UETT_AlignOf: 1320 if (Policy.LangOpts.CPlusPlus) 1321 OS << "alignof"; 1322 else if (Policy.LangOpts.C11) 1323 OS << "_Alignof"; 1324 else 1325 OS << "__alignof"; 1326 break; 1327 case UETT_VecStep: 1328 OS << "vec_step"; 1329 break; 1330 case UETT_OpenMPRequiredSimdAlign: 1331 OS << "__builtin_omp_required_simd_align"; 1332 break; 1333 } 1334 if (Node->isArgumentType()) { 1335 OS << '('; 1336 Node->getArgumentType().print(OS, Policy); 1337 OS << ')'; 1338 } else { 1339 OS << " "; 1340 PrintExpr(Node->getArgumentExpr()); 1341 } 1342 } 1343 1344 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 1345 OS << "_Generic("; 1346 PrintExpr(Node->getControllingExpr()); 1347 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 1348 OS << ", "; 1349 QualType T = Node->getAssocType(i); 1350 if (T.isNull()) 1351 OS << "default"; 1352 else 1353 T.print(OS, Policy); 1354 OS << ": "; 1355 PrintExpr(Node->getAssocExpr(i)); 1356 } 1357 OS << ")"; 1358 } 1359 1360 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 1361 PrintExpr(Node->getLHS()); 1362 OS << "["; 1363 PrintExpr(Node->getRHS()); 1364 OS << "]"; 1365 } 1366 1367 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) { 1368 PrintExpr(Node->getBase()); 1369 OS << "["; 1370 if (Node->getLowerBound()) 1371 PrintExpr(Node->getLowerBound()); 1372 if (Node->getColonLoc().isValid()) { 1373 OS << ":"; 1374 if (Node->getLength()) 1375 PrintExpr(Node->getLength()); 1376 } 1377 OS << "]"; 1378 } 1379 1380 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 1381 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 1382 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 1383 // Don't print any defaulted arguments 1384 break; 1385 } 1386 1387 if (i) OS << ", "; 1388 PrintExpr(Call->getArg(i)); 1389 } 1390 } 1391 1392 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 1393 PrintExpr(Call->getCallee()); 1394 OS << "("; 1395 PrintCallArgs(Call); 1396 OS << ")"; 1397 } 1398 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 1399 // FIXME: Suppress printing implicit bases (like "this") 1400 PrintExpr(Node->getBase()); 1401 1402 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 1403 FieldDecl *ParentDecl = ParentMember 1404 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr; 1405 1406 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 1407 OS << (Node->isArrow() ? "->" : "."); 1408 1409 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 1410 if (FD->isAnonymousStructOrUnion()) 1411 return; 1412 1413 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1414 Qualifier->print(OS, Policy); 1415 if (Node->hasTemplateKeyword()) 1416 OS << "template "; 1417 OS << Node->getMemberNameInfo(); 1418 if (Node->hasExplicitTemplateArgs()) 1419 TemplateSpecializationType::PrintTemplateArgumentList( 1420 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1421 } 1422 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 1423 PrintExpr(Node->getBase()); 1424 OS << (Node->isArrow() ? "->isa" : ".isa"); 1425 } 1426 1427 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 1428 PrintExpr(Node->getBase()); 1429 OS << "."; 1430 OS << Node->getAccessor().getName(); 1431 } 1432 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 1433 OS << '('; 1434 Node->getTypeAsWritten().print(OS, Policy); 1435 OS << ')'; 1436 PrintExpr(Node->getSubExpr()); 1437 } 1438 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 1439 OS << '('; 1440 Node->getType().print(OS, Policy); 1441 OS << ')'; 1442 PrintExpr(Node->getInitializer()); 1443 } 1444 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 1445 // No need to print anything, simply forward to the subexpression. 1446 PrintExpr(Node->getSubExpr()); 1447 } 1448 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 1449 PrintExpr(Node->getLHS()); 1450 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1451 PrintExpr(Node->getRHS()); 1452 } 1453 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 1454 PrintExpr(Node->getLHS()); 1455 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 1456 PrintExpr(Node->getRHS()); 1457 } 1458 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 1459 PrintExpr(Node->getCond()); 1460 OS << " ? "; 1461 PrintExpr(Node->getLHS()); 1462 OS << " : "; 1463 PrintExpr(Node->getRHS()); 1464 } 1465 1466 // GNU extensions. 1467 1468 void 1469 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 1470 PrintExpr(Node->getCommon()); 1471 OS << " ?: "; 1472 PrintExpr(Node->getFalseExpr()); 1473 } 1474 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 1475 OS << "&&" << Node->getLabel()->getName(); 1476 } 1477 1478 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 1479 OS << "("; 1480 PrintRawCompoundStmt(E->getSubStmt()); 1481 OS << ")"; 1482 } 1483 1484 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 1485 OS << "__builtin_choose_expr("; 1486 PrintExpr(Node->getCond()); 1487 OS << ", "; 1488 PrintExpr(Node->getLHS()); 1489 OS << ", "; 1490 PrintExpr(Node->getRHS()); 1491 OS << ")"; 1492 } 1493 1494 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1495 OS << "__null"; 1496 } 1497 1498 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1499 OS << "__builtin_shufflevector("; 1500 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1501 if (i) OS << ", "; 1502 PrintExpr(Node->getExpr(i)); 1503 } 1504 OS << ")"; 1505 } 1506 1507 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 1508 OS << "__builtin_convertvector("; 1509 PrintExpr(Node->getSrcExpr()); 1510 OS << ", "; 1511 Node->getType().print(OS, Policy); 1512 OS << ")"; 1513 } 1514 1515 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1516 if (Node->getSyntacticForm()) { 1517 Visit(Node->getSyntacticForm()); 1518 return; 1519 } 1520 1521 OS << "{"; 1522 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1523 if (i) OS << ", "; 1524 if (Node->getInit(i)) 1525 PrintExpr(Node->getInit(i)); 1526 else 1527 OS << "{}"; 1528 } 1529 OS << "}"; 1530 } 1531 1532 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1533 OS << "("; 1534 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1535 if (i) OS << ", "; 1536 PrintExpr(Node->getExpr(i)); 1537 } 1538 OS << ")"; 1539 } 1540 1541 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1542 bool NeedsEquals = true; 1543 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1544 DEnd = Node->designators_end(); 1545 D != DEnd; ++D) { 1546 if (D->isFieldDesignator()) { 1547 if (D->getDotLoc().isInvalid()) { 1548 if (IdentifierInfo *II = D->getFieldName()) { 1549 OS << II->getName() << ":"; 1550 NeedsEquals = false; 1551 } 1552 } else { 1553 OS << "." << D->getFieldName()->getName(); 1554 } 1555 } else { 1556 OS << "["; 1557 if (D->isArrayDesignator()) { 1558 PrintExpr(Node->getArrayIndex(*D)); 1559 } else { 1560 PrintExpr(Node->getArrayRangeStart(*D)); 1561 OS << " ... "; 1562 PrintExpr(Node->getArrayRangeEnd(*D)); 1563 } 1564 OS << "]"; 1565 } 1566 } 1567 1568 if (NeedsEquals) 1569 OS << " = "; 1570 else 1571 OS << " "; 1572 PrintExpr(Node->getInit()); 1573 } 1574 1575 void StmtPrinter::VisitDesignatedInitUpdateExpr( 1576 DesignatedInitUpdateExpr *Node) { 1577 OS << "{"; 1578 OS << "/*base*/"; 1579 PrintExpr(Node->getBase()); 1580 OS << ", "; 1581 1582 OS << "/*updater*/"; 1583 PrintExpr(Node->getUpdater()); 1584 OS << "}"; 1585 } 1586 1587 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) { 1588 OS << "/*no init*/"; 1589 } 1590 1591 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1592 if (Policy.LangOpts.CPlusPlus) { 1593 OS << "/*implicit*/"; 1594 Node->getType().print(OS, Policy); 1595 OS << "()"; 1596 } else { 1597 OS << "/*implicit*/("; 1598 Node->getType().print(OS, Policy); 1599 OS << ')'; 1600 if (Node->getType()->isRecordType()) 1601 OS << "{}"; 1602 else 1603 OS << 0; 1604 } 1605 } 1606 1607 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1608 OS << "__builtin_va_arg("; 1609 PrintExpr(Node->getSubExpr()); 1610 OS << ", "; 1611 Node->getType().print(OS, Policy); 1612 OS << ")"; 1613 } 1614 1615 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1616 PrintExpr(Node->getSyntacticForm()); 1617 } 1618 1619 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1620 const char *Name = nullptr; 1621 switch (Node->getOp()) { 1622 #define BUILTIN(ID, TYPE, ATTRS) 1623 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1624 case AtomicExpr::AO ## ID: \ 1625 Name = #ID "("; \ 1626 break; 1627 #include "clang/Basic/Builtins.def" 1628 } 1629 OS << Name; 1630 1631 // AtomicExpr stores its subexpressions in a permuted order. 1632 PrintExpr(Node->getPtr()); 1633 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1634 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1635 OS << ", "; 1636 PrintExpr(Node->getVal1()); 1637 } 1638 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1639 Node->isCmpXChg()) { 1640 OS << ", "; 1641 PrintExpr(Node->getVal2()); 1642 } 1643 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1644 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1645 OS << ", "; 1646 PrintExpr(Node->getWeak()); 1647 } 1648 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 1649 OS << ", "; 1650 PrintExpr(Node->getOrder()); 1651 } 1652 if (Node->isCmpXChg()) { 1653 OS << ", "; 1654 PrintExpr(Node->getOrderFail()); 1655 } 1656 OS << ")"; 1657 } 1658 1659 // C++ 1660 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1661 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1662 "", 1663 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1664 Spelling, 1665 #include "clang/Basic/OperatorKinds.def" 1666 }; 1667 1668 OverloadedOperatorKind Kind = Node->getOperator(); 1669 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1670 if (Node->getNumArgs() == 1) { 1671 OS << OpStrings[Kind] << ' '; 1672 PrintExpr(Node->getArg(0)); 1673 } else { 1674 PrintExpr(Node->getArg(0)); 1675 OS << ' ' << OpStrings[Kind]; 1676 } 1677 } else if (Kind == OO_Arrow) { 1678 PrintExpr(Node->getArg(0)); 1679 } else if (Kind == OO_Call) { 1680 PrintExpr(Node->getArg(0)); 1681 OS << '('; 1682 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1683 if (ArgIdx > 1) 1684 OS << ", "; 1685 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1686 PrintExpr(Node->getArg(ArgIdx)); 1687 } 1688 OS << ')'; 1689 } else if (Kind == OO_Subscript) { 1690 PrintExpr(Node->getArg(0)); 1691 OS << '['; 1692 PrintExpr(Node->getArg(1)); 1693 OS << ']'; 1694 } else if (Node->getNumArgs() == 1) { 1695 OS << OpStrings[Kind] << ' '; 1696 PrintExpr(Node->getArg(0)); 1697 } else if (Node->getNumArgs() == 2) { 1698 PrintExpr(Node->getArg(0)); 1699 OS << ' ' << OpStrings[Kind] << ' '; 1700 PrintExpr(Node->getArg(1)); 1701 } else { 1702 llvm_unreachable("unknown overloaded operator"); 1703 } 1704 } 1705 1706 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1707 // If we have a conversion operator call only print the argument. 1708 CXXMethodDecl *MD = Node->getMethodDecl(); 1709 if (MD && isa<CXXConversionDecl>(MD)) { 1710 PrintExpr(Node->getImplicitObjectArgument()); 1711 return; 1712 } 1713 VisitCallExpr(cast<CallExpr>(Node)); 1714 } 1715 1716 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1717 PrintExpr(Node->getCallee()); 1718 OS << "<<<"; 1719 PrintCallArgs(Node->getConfig()); 1720 OS << ">>>("; 1721 PrintCallArgs(Node); 1722 OS << ")"; 1723 } 1724 1725 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1726 OS << Node->getCastName() << '<'; 1727 Node->getTypeAsWritten().print(OS, Policy); 1728 OS << ">("; 1729 PrintExpr(Node->getSubExpr()); 1730 OS << ")"; 1731 } 1732 1733 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1734 VisitCXXNamedCastExpr(Node); 1735 } 1736 1737 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1738 VisitCXXNamedCastExpr(Node); 1739 } 1740 1741 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1742 VisitCXXNamedCastExpr(Node); 1743 } 1744 1745 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1746 VisitCXXNamedCastExpr(Node); 1747 } 1748 1749 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1750 OS << "typeid("; 1751 if (Node->isTypeOperand()) { 1752 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1753 } else { 1754 PrintExpr(Node->getExprOperand()); 1755 } 1756 OS << ")"; 1757 } 1758 1759 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1760 OS << "__uuidof("; 1761 if (Node->isTypeOperand()) { 1762 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 1763 } else { 1764 PrintExpr(Node->getExprOperand()); 1765 } 1766 OS << ")"; 1767 } 1768 1769 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 1770 PrintExpr(Node->getBaseExpr()); 1771 if (Node->isArrow()) 1772 OS << "->"; 1773 else 1774 OS << "."; 1775 if (NestedNameSpecifier *Qualifier = 1776 Node->getQualifierLoc().getNestedNameSpecifier()) 1777 Qualifier->print(OS, Policy); 1778 OS << Node->getPropertyDecl()->getDeclName(); 1779 } 1780 1781 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) { 1782 PrintExpr(Node->getBase()); 1783 OS << "["; 1784 PrintExpr(Node->getIdx()); 1785 OS << "]"; 1786 } 1787 1788 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1789 switch (Node->getLiteralOperatorKind()) { 1790 case UserDefinedLiteral::LOK_Raw: 1791 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1792 break; 1793 case UserDefinedLiteral::LOK_Template: { 1794 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1795 const TemplateArgumentList *Args = 1796 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1797 assert(Args); 1798 1799 if (Args->size() != 1) { 1800 OS << "operator\"\"" << Node->getUDSuffix()->getName(); 1801 TemplateSpecializationType::PrintTemplateArgumentList( 1802 OS, Args->data(), Args->size(), Policy); 1803 OS << "()"; 1804 return; 1805 } 1806 1807 const TemplateArgument &Pack = Args->get(0); 1808 for (const auto &P : Pack.pack_elements()) { 1809 char C = (char)P.getAsIntegral().getZExtValue(); 1810 OS << C; 1811 } 1812 break; 1813 } 1814 case UserDefinedLiteral::LOK_Integer: { 1815 // Print integer literal without suffix. 1816 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1817 OS << Int->getValue().toString(10, /*isSigned*/false); 1818 break; 1819 } 1820 case UserDefinedLiteral::LOK_Floating: { 1821 // Print floating literal without suffix. 1822 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1823 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1824 break; 1825 } 1826 case UserDefinedLiteral::LOK_String: 1827 case UserDefinedLiteral::LOK_Character: 1828 PrintExpr(Node->getCookedLiteral()); 1829 break; 1830 } 1831 OS << Node->getUDSuffix()->getName(); 1832 } 1833 1834 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1835 OS << (Node->getValue() ? "true" : "false"); 1836 } 1837 1838 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1839 OS << "nullptr"; 1840 } 1841 1842 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1843 OS << "this"; 1844 } 1845 1846 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1847 if (!Node->getSubExpr()) 1848 OS << "throw"; 1849 else { 1850 OS << "throw "; 1851 PrintExpr(Node->getSubExpr()); 1852 } 1853 } 1854 1855 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1856 // Nothing to print: we picked up the default argument. 1857 } 1858 1859 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 1860 // Nothing to print: we picked up the default initializer. 1861 } 1862 1863 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1864 Node->getType().print(OS, Policy); 1865 // If there are no parens, this is list-initialization, and the braces are 1866 // part of the syntax of the inner construct. 1867 if (Node->getLParenLoc().isValid()) 1868 OS << "("; 1869 PrintExpr(Node->getSubExpr()); 1870 if (Node->getLParenLoc().isValid()) 1871 OS << ")"; 1872 } 1873 1874 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1875 PrintExpr(Node->getSubExpr()); 1876 } 1877 1878 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1879 Node->getType().print(OS, Policy); 1880 if (Node->isStdInitListInitialization()) 1881 /* Nothing to do; braces are part of creating the std::initializer_list. */; 1882 else if (Node->isListInitialization()) 1883 OS << "{"; 1884 else 1885 OS << "("; 1886 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1887 ArgEnd = Node->arg_end(); 1888 Arg != ArgEnd; ++Arg) { 1889 if ((*Arg)->isDefaultArgument()) 1890 break; 1891 if (Arg != Node->arg_begin()) 1892 OS << ", "; 1893 PrintExpr(*Arg); 1894 } 1895 if (Node->isStdInitListInitialization()) 1896 /* See above. */; 1897 else if (Node->isListInitialization()) 1898 OS << "}"; 1899 else 1900 OS << ")"; 1901 } 1902 1903 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1904 OS << '['; 1905 bool NeedComma = false; 1906 switch (Node->getCaptureDefault()) { 1907 case LCD_None: 1908 break; 1909 1910 case LCD_ByCopy: 1911 OS << '='; 1912 NeedComma = true; 1913 break; 1914 1915 case LCD_ByRef: 1916 OS << '&'; 1917 NeedComma = true; 1918 break; 1919 } 1920 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1921 CEnd = Node->explicit_capture_end(); 1922 C != CEnd; 1923 ++C) { 1924 if (NeedComma) 1925 OS << ", "; 1926 NeedComma = true; 1927 1928 switch (C->getCaptureKind()) { 1929 case LCK_This: 1930 OS << "this"; 1931 break; 1932 1933 case LCK_ByRef: 1934 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C)) 1935 OS << '&'; 1936 OS << C->getCapturedVar()->getName(); 1937 break; 1938 1939 case LCK_ByCopy: 1940 OS << C->getCapturedVar()->getName(); 1941 break; 1942 case LCK_VLAType: 1943 llvm_unreachable("VLA type in explicit captures."); 1944 } 1945 1946 if (Node->isInitCapture(C)) 1947 PrintExpr(C->getCapturedVar()->getInit()); 1948 } 1949 OS << ']'; 1950 1951 if (Node->hasExplicitParameters()) { 1952 OS << " ("; 1953 CXXMethodDecl *Method = Node->getCallOperator(); 1954 NeedComma = false; 1955 for (auto P : Method->params()) { 1956 if (NeedComma) { 1957 OS << ", "; 1958 } else { 1959 NeedComma = true; 1960 } 1961 std::string ParamStr = P->getNameAsString(); 1962 P->getOriginalType().print(OS, Policy, ParamStr); 1963 } 1964 if (Method->isVariadic()) { 1965 if (NeedComma) 1966 OS << ", "; 1967 OS << "..."; 1968 } 1969 OS << ')'; 1970 1971 if (Node->isMutable()) 1972 OS << " mutable"; 1973 1974 const FunctionProtoType *Proto 1975 = Method->getType()->getAs<FunctionProtoType>(); 1976 Proto->printExceptionSpecification(OS, Policy); 1977 1978 // FIXME: Attributes 1979 1980 // Print the trailing return type if it was specified in the source. 1981 if (Node->hasExplicitResultType()) { 1982 OS << " -> "; 1983 Proto->getReturnType().print(OS, Policy); 1984 } 1985 } 1986 1987 // Print the body. 1988 CompoundStmt *Body = Node->getBody(); 1989 OS << ' '; 1990 PrintStmt(Body); 1991 } 1992 1993 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1994 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1995 TSInfo->getType().print(OS, Policy); 1996 else 1997 Node->getType().print(OS, Policy); 1998 OS << "()"; 1999 } 2000 2001 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 2002 if (E->isGlobalNew()) 2003 OS << "::"; 2004 OS << "new "; 2005 unsigned NumPlace = E->getNumPlacementArgs(); 2006 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 2007 OS << "("; 2008 PrintExpr(E->getPlacementArg(0)); 2009 for (unsigned i = 1; i < NumPlace; ++i) { 2010 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 2011 break; 2012 OS << ", "; 2013 PrintExpr(E->getPlacementArg(i)); 2014 } 2015 OS << ") "; 2016 } 2017 if (E->isParenTypeId()) 2018 OS << "("; 2019 std::string TypeS; 2020 if (Expr *Size = E->getArraySize()) { 2021 llvm::raw_string_ostream s(TypeS); 2022 s << '['; 2023 Size->printPretty(s, Helper, Policy); 2024 s << ']'; 2025 } 2026 E->getAllocatedType().print(OS, Policy, TypeS); 2027 if (E->isParenTypeId()) 2028 OS << ")"; 2029 2030 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 2031 if (InitStyle) { 2032 if (InitStyle == CXXNewExpr::CallInit) 2033 OS << "("; 2034 PrintExpr(E->getInitializer()); 2035 if (InitStyle == CXXNewExpr::CallInit) 2036 OS << ")"; 2037 } 2038 } 2039 2040 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 2041 if (E->isGlobalDelete()) 2042 OS << "::"; 2043 OS << "delete "; 2044 if (E->isArrayForm()) 2045 OS << "[] "; 2046 PrintExpr(E->getArgument()); 2047 } 2048 2049 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 2050 PrintExpr(E->getBase()); 2051 if (E->isArrow()) 2052 OS << "->"; 2053 else 2054 OS << '.'; 2055 if (E->getQualifier()) 2056 E->getQualifier()->print(OS, Policy); 2057 OS << "~"; 2058 2059 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 2060 OS << II->getName(); 2061 else 2062 E->getDestroyedType().print(OS, Policy); 2063 } 2064 2065 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 2066 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2067 OS << "{"; 2068 2069 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 2070 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 2071 // Don't print any defaulted arguments 2072 break; 2073 } 2074 2075 if (i) OS << ", "; 2076 PrintExpr(E->getArg(i)); 2077 } 2078 2079 if (E->isListInitialization() && !E->isStdInitListInitialization()) 2080 OS << "}"; 2081 } 2082 2083 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 2084 PrintExpr(E->getSubExpr()); 2085 } 2086 2087 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 2088 // Just forward to the subexpression. 2089 PrintExpr(E->getSubExpr()); 2090 } 2091 2092 void 2093 StmtPrinter::VisitCXXUnresolvedConstructExpr( 2094 CXXUnresolvedConstructExpr *Node) { 2095 Node->getTypeAsWritten().print(OS, Policy); 2096 OS << "("; 2097 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 2098 ArgEnd = Node->arg_end(); 2099 Arg != ArgEnd; ++Arg) { 2100 if (Arg != Node->arg_begin()) 2101 OS << ", "; 2102 PrintExpr(*Arg); 2103 } 2104 OS << ")"; 2105 } 2106 2107 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 2108 CXXDependentScopeMemberExpr *Node) { 2109 if (!Node->isImplicitAccess()) { 2110 PrintExpr(Node->getBase()); 2111 OS << (Node->isArrow() ? "->" : "."); 2112 } 2113 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2114 Qualifier->print(OS, Policy); 2115 if (Node->hasTemplateKeyword()) 2116 OS << "template "; 2117 OS << Node->getMemberNameInfo(); 2118 if (Node->hasExplicitTemplateArgs()) 2119 TemplateSpecializationType::PrintTemplateArgumentList( 2120 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 2121 } 2122 2123 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 2124 if (!Node->isImplicitAccess()) { 2125 PrintExpr(Node->getBase()); 2126 OS << (Node->isArrow() ? "->" : "."); 2127 } 2128 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 2129 Qualifier->print(OS, Policy); 2130 if (Node->hasTemplateKeyword()) 2131 OS << "template "; 2132 OS << Node->getMemberNameInfo(); 2133 if (Node->hasExplicitTemplateArgs()) 2134 TemplateSpecializationType::PrintTemplateArgumentList( 2135 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 2136 } 2137 2138 static const char *getTypeTraitName(TypeTrait TT) { 2139 switch (TT) { 2140 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 2141 case clang::UTT_##Name: return #Spelling; 2142 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 2143 case clang::BTT_##Name: return #Spelling; 2144 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 2145 case clang::TT_##Name: return #Spelling; 2146 #include "clang/Basic/TokenKinds.def" 2147 } 2148 llvm_unreachable("Type trait not covered by switch"); 2149 } 2150 2151 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 2152 switch (ATT) { 2153 case ATT_ArrayRank: return "__array_rank"; 2154 case ATT_ArrayExtent: return "__array_extent"; 2155 } 2156 llvm_unreachable("Array type trait not covered by switch"); 2157 } 2158 2159 static const char *getExpressionTraitName(ExpressionTrait ET) { 2160 switch (ET) { 2161 case ET_IsLValueExpr: return "__is_lvalue_expr"; 2162 case ET_IsRValueExpr: return "__is_rvalue_expr"; 2163 } 2164 llvm_unreachable("Expression type trait not covered by switch"); 2165 } 2166 2167 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 2168 OS << getTypeTraitName(E->getTrait()) << "("; 2169 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 2170 if (I > 0) 2171 OS << ", "; 2172 E->getArg(I)->getType().print(OS, Policy); 2173 } 2174 OS << ")"; 2175 } 2176 2177 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2178 OS << getTypeTraitName(E->getTrait()) << '('; 2179 E->getQueriedType().print(OS, Policy); 2180 OS << ')'; 2181 } 2182 2183 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2184 OS << getExpressionTraitName(E->getTrait()) << '('; 2185 PrintExpr(E->getQueriedExpression()); 2186 OS << ')'; 2187 } 2188 2189 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 2190 OS << "noexcept("; 2191 PrintExpr(E->getOperand()); 2192 OS << ")"; 2193 } 2194 2195 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 2196 PrintExpr(E->getPattern()); 2197 OS << "..."; 2198 } 2199 2200 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2201 OS << "sizeof...(" << *E->getPack() << ")"; 2202 } 2203 2204 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 2205 SubstNonTypeTemplateParmPackExpr *Node) { 2206 OS << *Node->getParameterPack(); 2207 } 2208 2209 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 2210 SubstNonTypeTemplateParmExpr *Node) { 2211 Visit(Node->getReplacement()); 2212 } 2213 2214 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 2215 OS << *E->getParameterPack(); 2216 } 2217 2218 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 2219 PrintExpr(Node->GetTemporaryExpr()); 2220 } 2221 2222 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 2223 OS << "("; 2224 if (E->getLHS()) { 2225 PrintExpr(E->getLHS()); 2226 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2227 } 2228 OS << "..."; 2229 if (E->getRHS()) { 2230 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 2231 PrintExpr(E->getRHS()); 2232 } 2233 OS << ")"; 2234 } 2235 2236 // C++ Coroutines TS 2237 2238 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) { 2239 Visit(S->getBody()); 2240 } 2241 2242 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) { 2243 OS << "co_return"; 2244 if (S->getOperand()) { 2245 OS << " "; 2246 Visit(S->getOperand()); 2247 } 2248 OS << ";"; 2249 } 2250 2251 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) { 2252 OS << "co_await "; 2253 PrintExpr(S->getOperand()); 2254 } 2255 2256 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) { 2257 OS << "co_yield "; 2258 PrintExpr(S->getOperand()); 2259 } 2260 2261 // Obj-C 2262 2263 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 2264 OS << "@"; 2265 VisitStringLiteral(Node->getString()); 2266 } 2267 2268 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 2269 OS << "@"; 2270 Visit(E->getSubExpr()); 2271 } 2272 2273 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 2274 OS << "@[ "; 2275 ObjCArrayLiteral::child_range Ch = E->children(); 2276 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) { 2277 if (I != Ch.begin()) 2278 OS << ", "; 2279 Visit(*I); 2280 } 2281 OS << " ]"; 2282 } 2283 2284 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 2285 OS << "@{ "; 2286 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 2287 if (I > 0) 2288 OS << ", "; 2289 2290 ObjCDictionaryElement Element = E->getKeyValueElement(I); 2291 Visit(Element.Key); 2292 OS << " : "; 2293 Visit(Element.Value); 2294 if (Element.isPackExpansion()) 2295 OS << "..."; 2296 } 2297 OS << " }"; 2298 } 2299 2300 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 2301 OS << "@encode("; 2302 Node->getEncodedType().print(OS, Policy); 2303 OS << ')'; 2304 } 2305 2306 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 2307 OS << "@selector("; 2308 Node->getSelector().print(OS); 2309 OS << ')'; 2310 } 2311 2312 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 2313 OS << "@protocol(" << *Node->getProtocol() << ')'; 2314 } 2315 2316 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 2317 OS << "["; 2318 switch (Mess->getReceiverKind()) { 2319 case ObjCMessageExpr::Instance: 2320 PrintExpr(Mess->getInstanceReceiver()); 2321 break; 2322 2323 case ObjCMessageExpr::Class: 2324 Mess->getClassReceiver().print(OS, Policy); 2325 break; 2326 2327 case ObjCMessageExpr::SuperInstance: 2328 case ObjCMessageExpr::SuperClass: 2329 OS << "Super"; 2330 break; 2331 } 2332 2333 OS << ' '; 2334 Selector selector = Mess->getSelector(); 2335 if (selector.isUnarySelector()) { 2336 OS << selector.getNameForSlot(0); 2337 } else { 2338 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 2339 if (i < selector.getNumArgs()) { 2340 if (i > 0) OS << ' '; 2341 if (selector.getIdentifierInfoForSlot(i)) 2342 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 2343 else 2344 OS << ":"; 2345 } 2346 else OS << ", "; // Handle variadic methods. 2347 2348 PrintExpr(Mess->getArg(i)); 2349 } 2350 } 2351 OS << "]"; 2352 } 2353 2354 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 2355 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 2356 } 2357 2358 void 2359 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 2360 PrintExpr(E->getSubExpr()); 2361 } 2362 2363 void 2364 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 2365 OS << '(' << E->getBridgeKindName(); 2366 E->getType().print(OS, Policy); 2367 OS << ')'; 2368 PrintExpr(E->getSubExpr()); 2369 } 2370 2371 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 2372 BlockDecl *BD = Node->getBlockDecl(); 2373 OS << "^"; 2374 2375 const FunctionType *AFT = Node->getFunctionType(); 2376 2377 if (isa<FunctionNoProtoType>(AFT)) { 2378 OS << "()"; 2379 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 2380 OS << '('; 2381 for (BlockDecl::param_iterator AI = BD->param_begin(), 2382 E = BD->param_end(); AI != E; ++AI) { 2383 if (AI != BD->param_begin()) OS << ", "; 2384 std::string ParamStr = (*AI)->getNameAsString(); 2385 (*AI)->getType().print(OS, Policy, ParamStr); 2386 } 2387 2388 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 2389 if (FT->isVariadic()) { 2390 if (!BD->param_empty()) OS << ", "; 2391 OS << "..."; 2392 } 2393 OS << ')'; 2394 } 2395 OS << "{ }"; 2396 } 2397 2398 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 2399 PrintExpr(Node->getSourceExpr()); 2400 } 2401 2402 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 2403 // TODO: Print something reasonable for a TypoExpr, if necessary. 2404 assert(false && "Cannot print TypoExpr nodes"); 2405 } 2406 2407 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 2408 OS << "__builtin_astype("; 2409 PrintExpr(Node->getSrcExpr()); 2410 OS << ", "; 2411 Node->getType().print(OS, Policy); 2412 OS << ")"; 2413 } 2414 2415 //===----------------------------------------------------------------------===// 2416 // Stmt method implementations 2417 //===----------------------------------------------------------------------===// 2418 2419 void Stmt::dumpPretty(const ASTContext &Context) const { 2420 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 2421 } 2422 2423 void Stmt::printPretty(raw_ostream &OS, 2424 PrinterHelper *Helper, 2425 const PrintingPolicy &Policy, 2426 unsigned Indentation) const { 2427 StmtPrinter P(OS, Helper, Policy, Indentation); 2428 P.Visit(const_cast<Stmt*>(this)); 2429 } 2430 2431 //===----------------------------------------------------------------------===// 2432 // PrinterHelper 2433 //===----------------------------------------------------------------------===// 2434 2435 // Implement virtual destructor. 2436 PrinterHelper::~PrinterHelper() {} 2437