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