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