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