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