clang API Documentation
00001 //===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which 00011 // pretty print the AST back out to C code. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "clang/AST/ASTContext.h" 00016 #include "clang/AST/Attr.h" 00017 #include "clang/AST/DeclCXX.h" 00018 #include "clang/AST/DeclObjC.h" 00019 #include "clang/AST/DeclTemplate.h" 00020 #include "clang/AST/Expr.h" 00021 #include "clang/AST/ExprCXX.h" 00022 #include "clang/AST/PrettyPrinter.h" 00023 #include "clang/AST/StmtVisitor.h" 00024 #include "clang/Basic/CharInfo.h" 00025 #include "llvm/ADT/SmallString.h" 00026 #include "llvm/Support/Format.h" 00027 using namespace clang; 00028 00029 //===----------------------------------------------------------------------===// 00030 // StmtPrinter Visitor 00031 //===----------------------------------------------------------------------===// 00032 00033 namespace { 00034 class StmtPrinter : public StmtVisitor<StmtPrinter> { 00035 raw_ostream &OS; 00036 unsigned IndentLevel; 00037 clang::PrinterHelper* Helper; 00038 PrintingPolicy Policy; 00039 00040 public: 00041 StmtPrinter(raw_ostream &os, PrinterHelper* helper, 00042 const PrintingPolicy &Policy, 00043 unsigned Indentation = 0) 00044 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {} 00045 00046 void PrintStmt(Stmt *S) { 00047 PrintStmt(S, Policy.Indentation); 00048 } 00049 00050 void PrintStmt(Stmt *S, int SubIndent) { 00051 IndentLevel += SubIndent; 00052 if (S && isa<Expr>(S)) { 00053 // If this is an expr used in a stmt context, indent and newline it. 00054 Indent(); 00055 Visit(S); 00056 OS << ";\n"; 00057 } else if (S) { 00058 Visit(S); 00059 } else { 00060 Indent() << "<<<NULL STATEMENT>>>\n"; 00061 } 00062 IndentLevel -= SubIndent; 00063 } 00064 00065 void PrintRawCompoundStmt(CompoundStmt *S); 00066 void PrintRawDecl(Decl *D); 00067 void PrintRawDeclStmt(const DeclStmt *S); 00068 void PrintRawIfStmt(IfStmt *If); 00069 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); 00070 void PrintCallArgs(CallExpr *E); 00071 void PrintRawSEHExceptHandler(SEHExceptStmt *S); 00072 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S); 00073 void PrintOMPExecutableDirective(OMPExecutableDirective *S); 00074 00075 void PrintExpr(Expr *E) { 00076 if (E) 00077 Visit(E); 00078 else 00079 OS << "<null expr>"; 00080 } 00081 00082 raw_ostream &Indent(int Delta = 0) { 00083 for (int i = 0, e = IndentLevel+Delta; i < e; ++i) 00084 OS << " "; 00085 return OS; 00086 } 00087 00088 void Visit(Stmt* S) { 00089 if (Helper && Helper->handledStmt(S,OS)) 00090 return; 00091 else StmtVisitor<StmtPrinter>::Visit(S); 00092 } 00093 00094 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED { 00095 Indent() << "<<unknown stmt type>>\n"; 00096 } 00097 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED { 00098 OS << "<<unknown expr type>>"; 00099 } 00100 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); 00101 00102 #define ABSTRACT_STMT(CLASS) 00103 #define STMT(CLASS, PARENT) \ 00104 void Visit##CLASS(CLASS *Node); 00105 #include "clang/AST/StmtNodes.inc" 00106 }; 00107 } 00108 00109 //===----------------------------------------------------------------------===// 00110 // Stmt printing methods. 00111 //===----------------------------------------------------------------------===// 00112 00113 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and 00114 /// with no newline after the }. 00115 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { 00116 OS << "{\n"; 00117 for (auto *I : Node->body()) 00118 PrintStmt(I); 00119 00120 Indent() << "}"; 00121 } 00122 00123 void StmtPrinter::PrintRawDecl(Decl *D) { 00124 D->print(OS, Policy, IndentLevel); 00125 } 00126 00127 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) { 00128 SmallVector<Decl*, 2> Decls(S->decls()); 00129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); 00130 } 00131 00132 void StmtPrinter::VisitNullStmt(NullStmt *Node) { 00133 Indent() << ";\n"; 00134 } 00135 00136 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { 00137 Indent(); 00138 PrintRawDeclStmt(Node); 00139 OS << ";\n"; 00140 } 00141 00142 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { 00143 Indent(); 00144 PrintRawCompoundStmt(Node); 00145 OS << "\n"; 00146 } 00147 00148 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { 00149 Indent(-1) << "case "; 00150 PrintExpr(Node->getLHS()); 00151 if (Node->getRHS()) { 00152 OS << " ... "; 00153 PrintExpr(Node->getRHS()); 00154 } 00155 OS << ":\n"; 00156 00157 PrintStmt(Node->getSubStmt(), 0); 00158 } 00159 00160 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { 00161 Indent(-1) << "default:\n"; 00162 PrintStmt(Node->getSubStmt(), 0); 00163 } 00164 00165 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { 00166 Indent(-1) << Node->getName() << ":\n"; 00167 PrintStmt(Node->getSubStmt(), 0); 00168 } 00169 00170 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) { 00171 for (const auto *Attr : Node->getAttrs()) { 00172 Attr->printPretty(OS, Policy); 00173 } 00174 00175 PrintStmt(Node->getSubStmt(), 0); 00176 } 00177 00178 void StmtPrinter::PrintRawIfStmt(IfStmt *If) { 00179 OS << "if ("; 00180 if (const DeclStmt *DS = If->getConditionVariableDeclStmt()) 00181 PrintRawDeclStmt(DS); 00182 else 00183 PrintExpr(If->getCond()); 00184 OS << ')'; 00185 00186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { 00187 OS << ' '; 00188 PrintRawCompoundStmt(CS); 00189 OS << (If->getElse() ? ' ' : '\n'); 00190 } else { 00191 OS << '\n'; 00192 PrintStmt(If->getThen()); 00193 if (If->getElse()) Indent(); 00194 } 00195 00196 if (Stmt *Else = If->getElse()) { 00197 OS << "else"; 00198 00199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { 00200 OS << ' '; 00201 PrintRawCompoundStmt(CS); 00202 OS << '\n'; 00203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { 00204 OS << ' '; 00205 PrintRawIfStmt(ElseIf); 00206 } else { 00207 OS << '\n'; 00208 PrintStmt(If->getElse()); 00209 } 00210 } 00211 } 00212 00213 void StmtPrinter::VisitIfStmt(IfStmt *If) { 00214 Indent(); 00215 PrintRawIfStmt(If); 00216 } 00217 00218 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { 00219 Indent() << "switch ("; 00220 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 00221 PrintRawDeclStmt(DS); 00222 else 00223 PrintExpr(Node->getCond()); 00224 OS << ")"; 00225 00226 // Pretty print compoundstmt bodies (very common). 00227 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 00228 OS << " "; 00229 PrintRawCompoundStmt(CS); 00230 OS << "\n"; 00231 } else { 00232 OS << "\n"; 00233 PrintStmt(Node->getBody()); 00234 } 00235 } 00236 00237 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { 00238 Indent() << "while ("; 00239 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 00240 PrintRawDeclStmt(DS); 00241 else 00242 PrintExpr(Node->getCond()); 00243 OS << ")\n"; 00244 PrintStmt(Node->getBody()); 00245 } 00246 00247 void StmtPrinter::VisitDoStmt(DoStmt *Node) { 00248 Indent() << "do "; 00249 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 00250 PrintRawCompoundStmt(CS); 00251 OS << " "; 00252 } else { 00253 OS << "\n"; 00254 PrintStmt(Node->getBody()); 00255 Indent(); 00256 } 00257 00258 OS << "while ("; 00259 PrintExpr(Node->getCond()); 00260 OS << ");\n"; 00261 } 00262 00263 void StmtPrinter::VisitForStmt(ForStmt *Node) { 00264 Indent() << "for ("; 00265 if (Node->getInit()) { 00266 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) 00267 PrintRawDeclStmt(DS); 00268 else 00269 PrintExpr(cast<Expr>(Node->getInit())); 00270 } 00271 OS << ";"; 00272 if (Node->getCond()) { 00273 OS << " "; 00274 PrintExpr(Node->getCond()); 00275 } 00276 OS << ";"; 00277 if (Node->getInc()) { 00278 OS << " "; 00279 PrintExpr(Node->getInc()); 00280 } 00281 OS << ") "; 00282 00283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 00284 PrintRawCompoundStmt(CS); 00285 OS << "\n"; 00286 } else { 00287 OS << "\n"; 00288 PrintStmt(Node->getBody()); 00289 } 00290 } 00291 00292 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { 00293 Indent() << "for ("; 00294 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) 00295 PrintRawDeclStmt(DS); 00296 else 00297 PrintExpr(cast<Expr>(Node->getElement())); 00298 OS << " in "; 00299 PrintExpr(Node->getCollection()); 00300 OS << ") "; 00301 00302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 00303 PrintRawCompoundStmt(CS); 00304 OS << "\n"; 00305 } else { 00306 OS << "\n"; 00307 PrintStmt(Node->getBody()); 00308 } 00309 } 00310 00311 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) { 00312 Indent() << "for ("; 00313 PrintingPolicy SubPolicy(Policy); 00314 SubPolicy.SuppressInitializers = true; 00315 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel); 00316 OS << " : "; 00317 PrintExpr(Node->getRangeInit()); 00318 OS << ") {\n"; 00319 PrintStmt(Node->getBody()); 00320 Indent() << "}"; 00321 if (Policy.IncludeNewlines) OS << "\n"; 00322 } 00323 00324 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) { 00325 Indent(); 00326 if (Node->isIfExists()) 00327 OS << "__if_exists ("; 00328 else 00329 OS << "__if_not_exists ("; 00330 00331 if (NestedNameSpecifier *Qualifier 00332 = Node->getQualifierLoc().getNestedNameSpecifier()) 00333 Qualifier->print(OS, Policy); 00334 00335 OS << Node->getNameInfo() << ") "; 00336 00337 PrintRawCompoundStmt(Node->getSubStmt()); 00338 } 00339 00340 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { 00341 Indent() << "goto " << Node->getLabel()->getName() << ";"; 00342 if (Policy.IncludeNewlines) OS << "\n"; 00343 } 00344 00345 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 00346 Indent() << "goto *"; 00347 PrintExpr(Node->getTarget()); 00348 OS << ";"; 00349 if (Policy.IncludeNewlines) OS << "\n"; 00350 } 00351 00352 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 00353 Indent() << "continue;"; 00354 if (Policy.IncludeNewlines) OS << "\n"; 00355 } 00356 00357 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 00358 Indent() << "break;"; 00359 if (Policy.IncludeNewlines) OS << "\n"; 00360 } 00361 00362 00363 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 00364 Indent() << "return"; 00365 if (Node->getRetValue()) { 00366 OS << " "; 00367 PrintExpr(Node->getRetValue()); 00368 } 00369 OS << ";"; 00370 if (Policy.IncludeNewlines) OS << "\n"; 00371 } 00372 00373 00374 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { 00375 Indent() << "asm "; 00376 00377 if (Node->isVolatile()) 00378 OS << "volatile "; 00379 00380 OS << "("; 00381 VisitStringLiteral(Node->getAsmString()); 00382 00383 // Outputs 00384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 00385 Node->getNumClobbers() != 0) 00386 OS << " : "; 00387 00388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 00389 if (i != 0) 00390 OS << ", "; 00391 00392 if (!Node->getOutputName(i).empty()) { 00393 OS << '['; 00394 OS << Node->getOutputName(i); 00395 OS << "] "; 00396 } 00397 00398 VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 00399 OS << " "; 00400 Visit(Node->getOutputExpr(i)); 00401 } 00402 00403 // Inputs 00404 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 00405 OS << " : "; 00406 00407 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 00408 if (i != 0) 00409 OS << ", "; 00410 00411 if (!Node->getInputName(i).empty()) { 00412 OS << '['; 00413 OS << Node->getInputName(i); 00414 OS << "] "; 00415 } 00416 00417 VisitStringLiteral(Node->getInputConstraintLiteral(i)); 00418 OS << " "; 00419 Visit(Node->getInputExpr(i)); 00420 } 00421 00422 // Clobbers 00423 if (Node->getNumClobbers() != 0) 00424 OS << " : "; 00425 00426 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 00427 if (i != 0) 00428 OS << ", "; 00429 00430 VisitStringLiteral(Node->getClobberStringLiteral(i)); 00431 } 00432 00433 OS << ");"; 00434 if (Policy.IncludeNewlines) OS << "\n"; 00435 } 00436 00437 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) { 00438 // FIXME: Implement MS style inline asm statement printer. 00439 Indent() << "__asm "; 00440 if (Node->hasBraces()) 00441 OS << "{\n"; 00442 OS << Node->getAsmString() << "\n"; 00443 if (Node->hasBraces()) 00444 Indent() << "}\n"; 00445 } 00446 00447 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) { 00448 PrintStmt(Node->getCapturedDecl()->getBody()); 00449 } 00450 00451 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 00452 Indent() << "@try"; 00453 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 00454 PrintRawCompoundStmt(TS); 00455 OS << "\n"; 00456 } 00457 00458 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 00459 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 00460 Indent() << "@catch("; 00461 if (catchStmt->getCatchParamDecl()) { 00462 if (Decl *DS = catchStmt->getCatchParamDecl()) 00463 PrintRawDecl(DS); 00464 } 00465 OS << ")"; 00466 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 00467 PrintRawCompoundStmt(CS); 00468 OS << "\n"; 00469 } 00470 } 00471 00472 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 00473 Node->getFinallyStmt())) { 00474 Indent() << "@finally"; 00475 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 00476 OS << "\n"; 00477 } 00478 } 00479 00480 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 00481 } 00482 00483 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 00484 Indent() << "@catch (...) { /* todo */ } \n"; 00485 } 00486 00487 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 00488 Indent() << "@throw"; 00489 if (Node->getThrowExpr()) { 00490 OS << " "; 00491 PrintExpr(Node->getThrowExpr()); 00492 } 00493 OS << ";\n"; 00494 } 00495 00496 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 00497 Indent() << "@synchronized ("; 00498 PrintExpr(Node->getSynchExpr()); 00499 OS << ")"; 00500 PrintRawCompoundStmt(Node->getSynchBody()); 00501 OS << "\n"; 00502 } 00503 00504 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 00505 Indent() << "@autoreleasepool"; 00506 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt())); 00507 OS << "\n"; 00508 } 00509 00510 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 00511 OS << "catch ("; 00512 if (Decl *ExDecl = Node->getExceptionDecl()) 00513 PrintRawDecl(ExDecl); 00514 else 00515 OS << "..."; 00516 OS << ") "; 00517 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 00518 } 00519 00520 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 00521 Indent(); 00522 PrintRawCXXCatchStmt(Node); 00523 OS << "\n"; 00524 } 00525 00526 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 00527 Indent() << "try "; 00528 PrintRawCompoundStmt(Node->getTryBlock()); 00529 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 00530 OS << " "; 00531 PrintRawCXXCatchStmt(Node->getHandler(i)); 00532 } 00533 OS << "\n"; 00534 } 00535 00536 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 00537 Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 00538 PrintRawCompoundStmt(Node->getTryBlock()); 00539 SEHExceptStmt *E = Node->getExceptHandler(); 00540 SEHFinallyStmt *F = Node->getFinallyHandler(); 00541 if(E) 00542 PrintRawSEHExceptHandler(E); 00543 else { 00544 assert(F && "Must have a finally block..."); 00545 PrintRawSEHFinallyStmt(F); 00546 } 00547 OS << "\n"; 00548 } 00549 00550 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 00551 OS << "__finally "; 00552 PrintRawCompoundStmt(Node->getBlock()); 00553 OS << "\n"; 00554 } 00555 00556 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 00557 OS << "__except ("; 00558 VisitExpr(Node->getFilterExpr()); 00559 OS << ")\n"; 00560 PrintRawCompoundStmt(Node->getBlock()); 00561 OS << "\n"; 00562 } 00563 00564 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 00565 Indent(); 00566 PrintRawSEHExceptHandler(Node); 00567 OS << "\n"; 00568 } 00569 00570 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 00571 Indent(); 00572 PrintRawSEHFinallyStmt(Node); 00573 OS << "\n"; 00574 } 00575 00576 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) { 00577 Indent() << "__leave;"; 00578 if (Policy.IncludeNewlines) OS << "\n"; 00579 } 00580 00581 //===----------------------------------------------------------------------===// 00582 // OpenMP clauses printing methods 00583 //===----------------------------------------------------------------------===// 00584 00585 namespace { 00586 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> { 00587 raw_ostream &OS; 00588 const PrintingPolicy &Policy; 00589 /// \brief Process clauses with list of variables. 00590 template <typename T> 00591 void VisitOMPClauseList(T *Node, char StartSym); 00592 public: 00593 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy) 00594 : OS(OS), Policy(Policy) { } 00595 #define OPENMP_CLAUSE(Name, Class) \ 00596 void Visit##Class(Class *S); 00597 #include "clang/Basic/OpenMPKinds.def" 00598 }; 00599 00600 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) { 00601 OS << "if("; 00602 Node->getCondition()->printPretty(OS, nullptr, Policy, 0); 00603 OS << ")"; 00604 } 00605 00606 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) { 00607 OS << "final("; 00608 Node->getCondition()->printPretty(OS, nullptr, Policy, 0); 00609 OS << ")"; 00610 } 00611 00612 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) { 00613 OS << "num_threads("; 00614 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0); 00615 OS << ")"; 00616 } 00617 00618 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) { 00619 OS << "safelen("; 00620 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0); 00621 OS << ")"; 00622 } 00623 00624 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) { 00625 OS << "collapse("; 00626 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0); 00627 OS << ")"; 00628 } 00629 00630 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) { 00631 OS << "default(" 00632 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind()) 00633 << ")"; 00634 } 00635 00636 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) { 00637 OS << "proc_bind(" 00638 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind()) 00639 << ")"; 00640 } 00641 00642 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) { 00643 OS << "schedule(" 00644 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind()); 00645 if (Node->getChunkSize()) { 00646 OS << ", "; 00647 Node->getChunkSize()->printPretty(OS, nullptr, Policy); 00648 } 00649 OS << ")"; 00650 } 00651 00652 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) { 00653 OS << "ordered"; 00654 } 00655 00656 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) { 00657 OS << "nowait"; 00658 } 00659 00660 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) { 00661 OS << "untied"; 00662 } 00663 00664 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) { 00665 OS << "mergeable"; 00666 } 00667 00668 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; } 00669 00670 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; } 00671 00672 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) { 00673 OS << "update"; 00674 } 00675 00676 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) { 00677 OS << "capture"; 00678 } 00679 00680 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) { 00681 OS << "seq_cst"; 00682 } 00683 00684 template<typename T> 00685 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) { 00686 for (typename T::varlist_iterator I = Node->varlist_begin(), 00687 E = Node->varlist_end(); 00688 I != E; ++I) { 00689 assert(*I && "Expected non-null Stmt"); 00690 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) { 00691 OS << (I == Node->varlist_begin() ? StartSym : ','); 00692 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS); 00693 } else { 00694 OS << (I == Node->varlist_begin() ? StartSym : ','); 00695 (*I)->printPretty(OS, nullptr, Policy, 0); 00696 } 00697 } 00698 } 00699 00700 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) { 00701 if (!Node->varlist_empty()) { 00702 OS << "private"; 00703 VisitOMPClauseList(Node, '('); 00704 OS << ")"; 00705 } 00706 } 00707 00708 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) { 00709 if (!Node->varlist_empty()) { 00710 OS << "firstprivate"; 00711 VisitOMPClauseList(Node, '('); 00712 OS << ")"; 00713 } 00714 } 00715 00716 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) { 00717 if (!Node->varlist_empty()) { 00718 OS << "lastprivate"; 00719 VisitOMPClauseList(Node, '('); 00720 OS << ")"; 00721 } 00722 } 00723 00724 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) { 00725 if (!Node->varlist_empty()) { 00726 OS << "shared"; 00727 VisitOMPClauseList(Node, '('); 00728 OS << ")"; 00729 } 00730 } 00731 00732 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) { 00733 if (!Node->varlist_empty()) { 00734 OS << "reduction("; 00735 NestedNameSpecifier *QualifierLoc = 00736 Node->getQualifierLoc().getNestedNameSpecifier(); 00737 OverloadedOperatorKind OOK = 00738 Node->getNameInfo().getName().getCXXOverloadedOperator(); 00739 if (QualifierLoc == nullptr && OOK != OO_None) { 00740 // Print reduction identifier in C format 00741 OS << getOperatorSpelling(OOK); 00742 } else { 00743 // Use C++ format 00744 if (QualifierLoc != nullptr) 00745 QualifierLoc->print(OS, Policy); 00746 OS << Node->getNameInfo(); 00747 } 00748 OS << ":"; 00749 VisitOMPClauseList(Node, ' '); 00750 OS << ")"; 00751 } 00752 } 00753 00754 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) { 00755 if (!Node->varlist_empty()) { 00756 OS << "linear"; 00757 VisitOMPClauseList(Node, '('); 00758 if (Node->getStep() != nullptr) { 00759 OS << ": "; 00760 Node->getStep()->printPretty(OS, nullptr, Policy, 0); 00761 } 00762 OS << ")"; 00763 } 00764 } 00765 00766 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) { 00767 if (!Node->varlist_empty()) { 00768 OS << "aligned"; 00769 VisitOMPClauseList(Node, '('); 00770 if (Node->getAlignment() != nullptr) { 00771 OS << ": "; 00772 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0); 00773 } 00774 OS << ")"; 00775 } 00776 } 00777 00778 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) { 00779 if (!Node->varlist_empty()) { 00780 OS << "copyin"; 00781 VisitOMPClauseList(Node, '('); 00782 OS << ")"; 00783 } 00784 } 00785 00786 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) { 00787 if (!Node->varlist_empty()) { 00788 OS << "copyprivate"; 00789 VisitOMPClauseList(Node, '('); 00790 OS << ")"; 00791 } 00792 } 00793 00794 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) { 00795 if (!Node->varlist_empty()) { 00796 VisitOMPClauseList(Node, '('); 00797 OS << ")"; 00798 } 00799 } 00800 } 00801 00802 //===----------------------------------------------------------------------===// 00803 // OpenMP directives printing methods 00804 //===----------------------------------------------------------------------===// 00805 00806 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) { 00807 OMPClausePrinter Printer(OS, Policy); 00808 ArrayRef<OMPClause *> Clauses = S->clauses(); 00809 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 00810 I != E; ++I) 00811 if (*I && !(*I)->isImplicit()) { 00812 Printer.Visit(*I); 00813 OS << ' '; 00814 } 00815 OS << "\n"; 00816 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) { 00817 assert(isa<CapturedStmt>(S->getAssociatedStmt()) && 00818 "Expected captured statement!"); 00819 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt(); 00820 PrintStmt(CS); 00821 } 00822 } 00823 00824 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) { 00825 Indent() << "#pragma omp parallel "; 00826 PrintOMPExecutableDirective(Node); 00827 } 00828 00829 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) { 00830 Indent() << "#pragma omp simd "; 00831 PrintOMPExecutableDirective(Node); 00832 } 00833 00834 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) { 00835 Indent() << "#pragma omp for "; 00836 PrintOMPExecutableDirective(Node); 00837 } 00838 00839 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) { 00840 Indent() << "#pragma omp for simd "; 00841 PrintOMPExecutableDirective(Node); 00842 } 00843 00844 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) { 00845 Indent() << "#pragma omp sections "; 00846 PrintOMPExecutableDirective(Node); 00847 } 00848 00849 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) { 00850 Indent() << "#pragma omp section"; 00851 PrintOMPExecutableDirective(Node); 00852 } 00853 00854 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) { 00855 Indent() << "#pragma omp single "; 00856 PrintOMPExecutableDirective(Node); 00857 } 00858 00859 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) { 00860 Indent() << "#pragma omp master"; 00861 PrintOMPExecutableDirective(Node); 00862 } 00863 00864 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) { 00865 Indent() << "#pragma omp critical"; 00866 if (Node->getDirectiveName().getName()) { 00867 OS << " ("; 00868 Node->getDirectiveName().printName(OS); 00869 OS << ")"; 00870 } 00871 PrintOMPExecutableDirective(Node); 00872 } 00873 00874 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) { 00875 Indent() << "#pragma omp parallel for "; 00876 PrintOMPExecutableDirective(Node); 00877 } 00878 00879 void StmtPrinter::VisitOMPParallelForSimdDirective( 00880 OMPParallelForSimdDirective *Node) { 00881 Indent() << "#pragma omp parallel for simd "; 00882 PrintOMPExecutableDirective(Node); 00883 } 00884 00885 void StmtPrinter::VisitOMPParallelSectionsDirective( 00886 OMPParallelSectionsDirective *Node) { 00887 Indent() << "#pragma omp parallel sections "; 00888 PrintOMPExecutableDirective(Node); 00889 } 00890 00891 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) { 00892 Indent() << "#pragma omp task "; 00893 PrintOMPExecutableDirective(Node); 00894 } 00895 00896 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) { 00897 Indent() << "#pragma omp taskyield"; 00898 PrintOMPExecutableDirective(Node); 00899 } 00900 00901 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) { 00902 Indent() << "#pragma omp barrier"; 00903 PrintOMPExecutableDirective(Node); 00904 } 00905 00906 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) { 00907 Indent() << "#pragma omp taskwait"; 00908 PrintOMPExecutableDirective(Node); 00909 } 00910 00911 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) { 00912 Indent() << "#pragma omp flush "; 00913 PrintOMPExecutableDirective(Node); 00914 } 00915 00916 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) { 00917 Indent() << "#pragma omp ordered"; 00918 PrintOMPExecutableDirective(Node); 00919 } 00920 00921 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) { 00922 Indent() << "#pragma omp atomic "; 00923 PrintOMPExecutableDirective(Node); 00924 } 00925 00926 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) { 00927 Indent() << "#pragma omp target "; 00928 PrintOMPExecutableDirective(Node); 00929 } 00930 00931 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) { 00932 Indent() << "#pragma omp teams "; 00933 PrintOMPExecutableDirective(Node); 00934 } 00935 00936 //===----------------------------------------------------------------------===// 00937 // Expr printing methods. 00938 //===----------------------------------------------------------------------===// 00939 00940 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 00941 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 00942 Qualifier->print(OS, Policy); 00943 if (Node->hasTemplateKeyword()) 00944 OS << "template "; 00945 OS << Node->getNameInfo(); 00946 if (Node->hasExplicitTemplateArgs()) 00947 TemplateSpecializationType::PrintTemplateArgumentList( 00948 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 00949 } 00950 00951 void StmtPrinter::VisitDependentScopeDeclRefExpr( 00952 DependentScopeDeclRefExpr *Node) { 00953 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 00954 Qualifier->print(OS, Policy); 00955 if (Node->hasTemplateKeyword()) 00956 OS << "template "; 00957 OS << Node->getNameInfo(); 00958 if (Node->hasExplicitTemplateArgs()) 00959 TemplateSpecializationType::PrintTemplateArgumentList( 00960 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 00961 } 00962 00963 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 00964 if (Node->getQualifier()) 00965 Node->getQualifier()->print(OS, Policy); 00966 if (Node->hasTemplateKeyword()) 00967 OS << "template "; 00968 OS << Node->getNameInfo(); 00969 if (Node->hasExplicitTemplateArgs()) 00970 TemplateSpecializationType::PrintTemplateArgumentList( 00971 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 00972 } 00973 00974 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 00975 if (Node->getBase()) { 00976 PrintExpr(Node->getBase()); 00977 OS << (Node->isArrow() ? "->" : "."); 00978 } 00979 OS << *Node->getDecl(); 00980 } 00981 00982 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 00983 if (Node->isSuperReceiver()) 00984 OS << "super."; 00985 else if (Node->isObjectReceiver() && Node->getBase()) { 00986 PrintExpr(Node->getBase()); 00987 OS << "."; 00988 } else if (Node->isClassReceiver() && Node->getClassReceiver()) { 00989 OS << Node->getClassReceiver()->getName() << "."; 00990 } 00991 00992 if (Node->isImplicitProperty()) 00993 Node->getImplicitPropertyGetter()->getSelector().print(OS); 00994 else 00995 OS << Node->getExplicitProperty()->getName(); 00996 } 00997 00998 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 00999 01000 PrintExpr(Node->getBaseExpr()); 01001 OS << "["; 01002 PrintExpr(Node->getKeyExpr()); 01003 OS << "]"; 01004 } 01005 01006 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 01007 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType()); 01008 } 01009 01010 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 01011 unsigned value = Node->getValue(); 01012 01013 switch (Node->getKind()) { 01014 case CharacterLiteral::Ascii: break; // no prefix. 01015 case CharacterLiteral::Wide: OS << 'L'; break; 01016 case CharacterLiteral::UTF16: OS << 'u'; break; 01017 case CharacterLiteral::UTF32: OS << 'U'; break; 01018 } 01019 01020 switch (value) { 01021 case '\\': 01022 OS << "'\\\\'"; 01023 break; 01024 case '\'': 01025 OS << "'\\''"; 01026 break; 01027 case '\a': 01028 // TODO: K&R: the meaning of '\\a' is different in traditional C 01029 OS << "'\\a'"; 01030 break; 01031 case '\b': 01032 OS << "'\\b'"; 01033 break; 01034 // Nonstandard escape sequence. 01035 /*case '\e': 01036 OS << "'\\e'"; 01037 break;*/ 01038 case '\f': 01039 OS << "'\\f'"; 01040 break; 01041 case '\n': 01042 OS << "'\\n'"; 01043 break; 01044 case '\r': 01045 OS << "'\\r'"; 01046 break; 01047 case '\t': 01048 OS << "'\\t'"; 01049 break; 01050 case '\v': 01051 OS << "'\\v'"; 01052 break; 01053 default: 01054 if (value < 256 && isPrintable((unsigned char)value)) 01055 OS << "'" << (char)value << "'"; 01056 else if (value < 256) 01057 OS << "'\\x" << llvm::format("%02x", value) << "'"; 01058 else if (value <= 0xFFFF) 01059 OS << "'\\u" << llvm::format("%04x", value) << "'"; 01060 else 01061 OS << "'\\U" << llvm::format("%08x", value) << "'"; 01062 } 01063 } 01064 01065 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 01066 bool isSigned = Node->getType()->isSignedIntegerType(); 01067 OS << Node->getValue().toString(10, isSigned); 01068 01069 // Emit suffixes. Integer literals are always a builtin integer type. 01070 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 01071 default: llvm_unreachable("Unexpected type for integer literal!"); 01072 case BuiltinType::SChar: OS << "i8"; break; 01073 case BuiltinType::UChar: OS << "Ui8"; break; 01074 case BuiltinType::Short: OS << "i16"; break; 01075 case BuiltinType::UShort: OS << "Ui16"; break; 01076 case BuiltinType::Int: break; // no suffix. 01077 case BuiltinType::UInt: OS << 'U'; break; 01078 case BuiltinType::Long: OS << 'L'; break; 01079 case BuiltinType::ULong: OS << "UL"; break; 01080 case BuiltinType::LongLong: OS << "LL"; break; 01081 case BuiltinType::ULongLong: OS << "ULL"; break; 01082 case BuiltinType::Int128: OS << "i128"; break; 01083 case BuiltinType::UInt128: OS << "Ui128"; break; 01084 } 01085 } 01086 01087 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 01088 bool PrintSuffix) { 01089 SmallString<16> Str; 01090 Node->getValue().toString(Str); 01091 OS << Str; 01092 if (Str.find_first_not_of("-0123456789") == StringRef::npos) 01093 OS << '.'; // Trailing dot in order to separate from ints. 01094 01095 if (!PrintSuffix) 01096 return; 01097 01098 // Emit suffixes. Float literals are always a builtin float type. 01099 switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 01100 default: llvm_unreachable("Unexpected type for float literal!"); 01101 case BuiltinType::Half: break; // FIXME: suffix? 01102 case BuiltinType::Double: break; // no suffix. 01103 case BuiltinType::Float: OS << 'F'; break; 01104 case BuiltinType::LongDouble: OS << 'L'; break; 01105 } 01106 } 01107 01108 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 01109 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 01110 } 01111 01112 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 01113 PrintExpr(Node->getSubExpr()); 01114 OS << "i"; 01115 } 01116 01117 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 01118 Str->outputString(OS); 01119 } 01120 void StmtPrinter::VisitParenExpr(ParenExpr *Node) { 01121 OS << "("; 01122 PrintExpr(Node->getSubExpr()); 01123 OS << ")"; 01124 } 01125 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 01126 if (!Node->isPostfix()) { 01127 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 01128 01129 // Print a space if this is an "identifier operator" like __real, or if 01130 // it might be concatenated incorrectly like '+'. 01131 switch (Node->getOpcode()) { 01132 default: break; 01133 case UO_Real: 01134 case UO_Imag: 01135 case UO_Extension: 01136 OS << ' '; 01137 break; 01138 case UO_Plus: 01139 case UO_Minus: 01140 if (isa<UnaryOperator>(Node->getSubExpr())) 01141 OS << ' '; 01142 break; 01143 } 01144 } 01145 PrintExpr(Node->getSubExpr()); 01146 01147 if (Node->isPostfix()) 01148 OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 01149 } 01150 01151 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 01152 OS << "__builtin_offsetof("; 01153 Node->getTypeSourceInfo()->getType().print(OS, Policy); 01154 OS << ", "; 01155 bool PrintedSomething = false; 01156 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 01157 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 01158 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 01159 // Array node 01160 OS << "["; 01161 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 01162 OS << "]"; 01163 PrintedSomething = true; 01164 continue; 01165 } 01166 01167 // Skip implicit base indirections. 01168 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 01169 continue; 01170 01171 // Field or identifier node. 01172 IdentifierInfo *Id = ON.getFieldName(); 01173 if (!Id) 01174 continue; 01175 01176 if (PrintedSomething) 01177 OS << "."; 01178 else 01179 PrintedSomething = true; 01180 OS << Id->getName(); 01181 } 01182 OS << ")"; 01183 } 01184 01185 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 01186 switch(Node->getKind()) { 01187 case UETT_SizeOf: 01188 OS << "sizeof"; 01189 break; 01190 case UETT_AlignOf: 01191 if (Policy.LangOpts.CPlusPlus) 01192 OS << "alignof"; 01193 else if (Policy.LangOpts.C11) 01194 OS << "_Alignof"; 01195 else 01196 OS << "__alignof"; 01197 break; 01198 case UETT_VecStep: 01199 OS << "vec_step"; 01200 break; 01201 } 01202 if (Node->isArgumentType()) { 01203 OS << '('; 01204 Node->getArgumentType().print(OS, Policy); 01205 OS << ')'; 01206 } else { 01207 OS << " "; 01208 PrintExpr(Node->getArgumentExpr()); 01209 } 01210 } 01211 01212 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 01213 OS << "_Generic("; 01214 PrintExpr(Node->getControllingExpr()); 01215 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 01216 OS << ", "; 01217 QualType T = Node->getAssocType(i); 01218 if (T.isNull()) 01219 OS << "default"; 01220 else 01221 T.print(OS, Policy); 01222 OS << ": "; 01223 PrintExpr(Node->getAssocExpr(i)); 01224 } 01225 OS << ")"; 01226 } 01227 01228 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 01229 PrintExpr(Node->getLHS()); 01230 OS << "["; 01231 PrintExpr(Node->getRHS()); 01232 OS << "]"; 01233 } 01234 01235 void StmtPrinter::PrintCallArgs(CallExpr *Call) { 01236 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 01237 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 01238 // Don't print any defaulted arguments 01239 break; 01240 } 01241 01242 if (i) OS << ", "; 01243 PrintExpr(Call->getArg(i)); 01244 } 01245 } 01246 01247 void StmtPrinter::VisitCallExpr(CallExpr *Call) { 01248 PrintExpr(Call->getCallee()); 01249 OS << "("; 01250 PrintCallArgs(Call); 01251 OS << ")"; 01252 } 01253 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 01254 // FIXME: Suppress printing implicit bases (like "this") 01255 PrintExpr(Node->getBase()); 01256 01257 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 01258 FieldDecl *ParentDecl = ParentMember 01259 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr; 01260 01261 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 01262 OS << (Node->isArrow() ? "->" : "."); 01263 01264 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 01265 if (FD->isAnonymousStructOrUnion()) 01266 return; 01267 01268 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 01269 Qualifier->print(OS, Policy); 01270 if (Node->hasTemplateKeyword()) 01271 OS << "template "; 01272 OS << Node->getMemberNameInfo(); 01273 if (Node->hasExplicitTemplateArgs()) 01274 TemplateSpecializationType::PrintTemplateArgumentList( 01275 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 01276 } 01277 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 01278 PrintExpr(Node->getBase()); 01279 OS << (Node->isArrow() ? "->isa" : ".isa"); 01280 } 01281 01282 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 01283 PrintExpr(Node->getBase()); 01284 OS << "."; 01285 OS << Node->getAccessor().getName(); 01286 } 01287 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 01288 OS << '('; 01289 Node->getTypeAsWritten().print(OS, Policy); 01290 OS << ')'; 01291 PrintExpr(Node->getSubExpr()); 01292 } 01293 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 01294 OS << '('; 01295 Node->getType().print(OS, Policy); 01296 OS << ')'; 01297 PrintExpr(Node->getInitializer()); 01298 } 01299 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 01300 // No need to print anything, simply forward to the subexpression. 01301 PrintExpr(Node->getSubExpr()); 01302 } 01303 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 01304 PrintExpr(Node->getLHS()); 01305 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 01306 PrintExpr(Node->getRHS()); 01307 } 01308 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 01309 PrintExpr(Node->getLHS()); 01310 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 01311 PrintExpr(Node->getRHS()); 01312 } 01313 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 01314 PrintExpr(Node->getCond()); 01315 OS << " ? "; 01316 PrintExpr(Node->getLHS()); 01317 OS << " : "; 01318 PrintExpr(Node->getRHS()); 01319 } 01320 01321 // GNU extensions. 01322 01323 void 01324 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 01325 PrintExpr(Node->getCommon()); 01326 OS << " ?: "; 01327 PrintExpr(Node->getFalseExpr()); 01328 } 01329 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 01330 OS << "&&" << Node->getLabel()->getName(); 01331 } 01332 01333 void StmtPrinter::VisitStmtExpr(StmtExpr *E) { 01334 OS << "("; 01335 PrintRawCompoundStmt(E->getSubStmt()); 01336 OS << ")"; 01337 } 01338 01339 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 01340 OS << "__builtin_choose_expr("; 01341 PrintExpr(Node->getCond()); 01342 OS << ", "; 01343 PrintExpr(Node->getLHS()); 01344 OS << ", "; 01345 PrintExpr(Node->getRHS()); 01346 OS << ")"; 01347 } 01348 01349 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 01350 OS << "__null"; 01351 } 01352 01353 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 01354 OS << "__builtin_shufflevector("; 01355 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 01356 if (i) OS << ", "; 01357 PrintExpr(Node->getExpr(i)); 01358 } 01359 OS << ")"; 01360 } 01361 01362 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) { 01363 OS << "__builtin_convertvector("; 01364 PrintExpr(Node->getSrcExpr()); 01365 OS << ", "; 01366 Node->getType().print(OS, Policy); 01367 OS << ")"; 01368 } 01369 01370 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 01371 if (Node->getSyntacticForm()) { 01372 Visit(Node->getSyntacticForm()); 01373 return; 01374 } 01375 01376 OS << "{ "; 01377 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 01378 if (i) OS << ", "; 01379 if (Node->getInit(i)) 01380 PrintExpr(Node->getInit(i)); 01381 else 01382 OS << "0"; 01383 } 01384 OS << " }"; 01385 } 01386 01387 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 01388 OS << "( "; 01389 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 01390 if (i) OS << ", "; 01391 PrintExpr(Node->getExpr(i)); 01392 } 01393 OS << " )"; 01394 } 01395 01396 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 01397 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 01398 DEnd = Node->designators_end(); 01399 D != DEnd; ++D) { 01400 if (D->isFieldDesignator()) { 01401 if (D->getDotLoc().isInvalid()) { 01402 if (IdentifierInfo *II = D->getFieldName()) 01403 OS << II->getName() << ":"; 01404 } else { 01405 OS << "." << D->getFieldName()->getName(); 01406 } 01407 } else { 01408 OS << "["; 01409 if (D->isArrayDesignator()) { 01410 PrintExpr(Node->getArrayIndex(*D)); 01411 } else { 01412 PrintExpr(Node->getArrayRangeStart(*D)); 01413 OS << " ... "; 01414 PrintExpr(Node->getArrayRangeEnd(*D)); 01415 } 01416 OS << "]"; 01417 } 01418 } 01419 01420 OS << " = "; 01421 PrintExpr(Node->getInit()); 01422 } 01423 01424 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 01425 if (Policy.LangOpts.CPlusPlus) { 01426 OS << "/*implicit*/"; 01427 Node->getType().print(OS, Policy); 01428 OS << "()"; 01429 } else { 01430 OS << "/*implicit*/("; 01431 Node->getType().print(OS, Policy); 01432 OS << ')'; 01433 if (Node->getType()->isRecordType()) 01434 OS << "{}"; 01435 else 01436 OS << 0; 01437 } 01438 } 01439 01440 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 01441 OS << "__builtin_va_arg("; 01442 PrintExpr(Node->getSubExpr()); 01443 OS << ", "; 01444 Node->getType().print(OS, Policy); 01445 OS << ")"; 01446 } 01447 01448 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 01449 PrintExpr(Node->getSyntacticForm()); 01450 } 01451 01452 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 01453 const char *Name = nullptr; 01454 switch (Node->getOp()) { 01455 #define BUILTIN(ID, TYPE, ATTRS) 01456 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 01457 case AtomicExpr::AO ## ID: \ 01458 Name = #ID "("; \ 01459 break; 01460 #include "clang/Basic/Builtins.def" 01461 } 01462 OS << Name; 01463 01464 // AtomicExpr stores its subexpressions in a permuted order. 01465 PrintExpr(Node->getPtr()); 01466 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 01467 Node->getOp() != AtomicExpr::AO__atomic_load_n) { 01468 OS << ", "; 01469 PrintExpr(Node->getVal1()); 01470 } 01471 if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 01472 Node->isCmpXChg()) { 01473 OS << ", "; 01474 PrintExpr(Node->getVal2()); 01475 } 01476 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 01477 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 01478 OS << ", "; 01479 PrintExpr(Node->getWeak()); 01480 } 01481 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) { 01482 OS << ", "; 01483 PrintExpr(Node->getOrder()); 01484 } 01485 if (Node->isCmpXChg()) { 01486 OS << ", "; 01487 PrintExpr(Node->getOrderFail()); 01488 } 01489 OS << ")"; 01490 } 01491 01492 // C++ 01493 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 01494 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 01495 "", 01496 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 01497 Spelling, 01498 #include "clang/Basic/OperatorKinds.def" 01499 }; 01500 01501 OverloadedOperatorKind Kind = Node->getOperator(); 01502 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 01503 if (Node->getNumArgs() == 1) { 01504 OS << OpStrings[Kind] << ' '; 01505 PrintExpr(Node->getArg(0)); 01506 } else { 01507 PrintExpr(Node->getArg(0)); 01508 OS << ' ' << OpStrings[Kind]; 01509 } 01510 } else if (Kind == OO_Arrow) { 01511 PrintExpr(Node->getArg(0)); 01512 } else if (Kind == OO_Call) { 01513 PrintExpr(Node->getArg(0)); 01514 OS << '('; 01515 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 01516 if (ArgIdx > 1) 01517 OS << ", "; 01518 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 01519 PrintExpr(Node->getArg(ArgIdx)); 01520 } 01521 OS << ')'; 01522 } else if (Kind == OO_Subscript) { 01523 PrintExpr(Node->getArg(0)); 01524 OS << '['; 01525 PrintExpr(Node->getArg(1)); 01526 OS << ']'; 01527 } else if (Node->getNumArgs() == 1) { 01528 OS << OpStrings[Kind] << ' '; 01529 PrintExpr(Node->getArg(0)); 01530 } else if (Node->getNumArgs() == 2) { 01531 PrintExpr(Node->getArg(0)); 01532 OS << ' ' << OpStrings[Kind] << ' '; 01533 PrintExpr(Node->getArg(1)); 01534 } else { 01535 llvm_unreachable("unknown overloaded operator"); 01536 } 01537 } 01538 01539 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 01540 // If we have a conversion operator call only print the argument. 01541 CXXMethodDecl *MD = Node->getMethodDecl(); 01542 if (MD && isa<CXXConversionDecl>(MD)) { 01543 PrintExpr(Node->getImplicitObjectArgument()); 01544 return; 01545 } 01546 VisitCallExpr(cast<CallExpr>(Node)); 01547 } 01548 01549 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 01550 PrintExpr(Node->getCallee()); 01551 OS << "<<<"; 01552 PrintCallArgs(Node->getConfig()); 01553 OS << ">>>("; 01554 PrintCallArgs(Node); 01555 OS << ")"; 01556 } 01557 01558 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 01559 OS << Node->getCastName() << '<'; 01560 Node->getTypeAsWritten().print(OS, Policy); 01561 OS << ">("; 01562 PrintExpr(Node->getSubExpr()); 01563 OS << ")"; 01564 } 01565 01566 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 01567 VisitCXXNamedCastExpr(Node); 01568 } 01569 01570 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 01571 VisitCXXNamedCastExpr(Node); 01572 } 01573 01574 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 01575 VisitCXXNamedCastExpr(Node); 01576 } 01577 01578 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 01579 VisitCXXNamedCastExpr(Node); 01580 } 01581 01582 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 01583 OS << "typeid("; 01584 if (Node->isTypeOperand()) { 01585 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 01586 } else { 01587 PrintExpr(Node->getExprOperand()); 01588 } 01589 OS << ")"; 01590 } 01591 01592 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 01593 OS << "__uuidof("; 01594 if (Node->isTypeOperand()) { 01595 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); 01596 } else { 01597 PrintExpr(Node->getExprOperand()); 01598 } 01599 OS << ")"; 01600 } 01601 01602 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) { 01603 PrintExpr(Node->getBaseExpr()); 01604 if (Node->isArrow()) 01605 OS << "->"; 01606 else 01607 OS << "."; 01608 if (NestedNameSpecifier *Qualifier = 01609 Node->getQualifierLoc().getNestedNameSpecifier()) 01610 Qualifier->print(OS, Policy); 01611 OS << Node->getPropertyDecl()->getDeclName(); 01612 } 01613 01614 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 01615 switch (Node->getLiteralOperatorKind()) { 01616 case UserDefinedLiteral::LOK_Raw: 01617 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 01618 break; 01619 case UserDefinedLiteral::LOK_Template: { 01620 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 01621 const TemplateArgumentList *Args = 01622 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 01623 assert(Args); 01624 const TemplateArgument &Pack = Args->get(0); 01625 for (const auto &P : Pack.pack_elements()) { 01626 char C = (char)P.getAsIntegral().getZExtValue(); 01627 OS << C; 01628 } 01629 break; 01630 } 01631 case UserDefinedLiteral::LOK_Integer: { 01632 // Print integer literal without suffix. 01633 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 01634 OS << Int->getValue().toString(10, /*isSigned*/false); 01635 break; 01636 } 01637 case UserDefinedLiteral::LOK_Floating: { 01638 // Print floating literal without suffix. 01639 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 01640 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 01641 break; 01642 } 01643 case UserDefinedLiteral::LOK_String: 01644 case UserDefinedLiteral::LOK_Character: 01645 PrintExpr(Node->getCookedLiteral()); 01646 break; 01647 } 01648 OS << Node->getUDSuffix()->getName(); 01649 } 01650 01651 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 01652 OS << (Node->getValue() ? "true" : "false"); 01653 } 01654 01655 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 01656 OS << "nullptr"; 01657 } 01658 01659 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 01660 OS << "this"; 01661 } 01662 01663 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 01664 if (!Node->getSubExpr()) 01665 OS << "throw"; 01666 else { 01667 OS << "throw "; 01668 PrintExpr(Node->getSubExpr()); 01669 } 01670 } 01671 01672 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 01673 // Nothing to print: we picked up the default argument. 01674 } 01675 01676 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) { 01677 // Nothing to print: we picked up the default initializer. 01678 } 01679 01680 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 01681 Node->getType().print(OS, Policy); 01682 OS << "("; 01683 PrintExpr(Node->getSubExpr()); 01684 OS << ")"; 01685 } 01686 01687 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 01688 PrintExpr(Node->getSubExpr()); 01689 } 01690 01691 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 01692 Node->getType().print(OS, Policy); 01693 OS << "("; 01694 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 01695 ArgEnd = Node->arg_end(); 01696 Arg != ArgEnd; ++Arg) { 01697 if (Arg->isDefaultArgument()) 01698 break; 01699 if (Arg != Node->arg_begin()) 01700 OS << ", "; 01701 PrintExpr(*Arg); 01702 } 01703 OS << ")"; 01704 } 01705 01706 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 01707 OS << '['; 01708 bool NeedComma = false; 01709 switch (Node->getCaptureDefault()) { 01710 case LCD_None: 01711 break; 01712 01713 case LCD_ByCopy: 01714 OS << '='; 01715 NeedComma = true; 01716 break; 01717 01718 case LCD_ByRef: 01719 OS << '&'; 01720 NeedComma = true; 01721 break; 01722 } 01723 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 01724 CEnd = Node->explicit_capture_end(); 01725 C != CEnd; 01726 ++C) { 01727 if (NeedComma) 01728 OS << ", "; 01729 NeedComma = true; 01730 01731 switch (C->getCaptureKind()) { 01732 case LCK_This: 01733 OS << "this"; 01734 break; 01735 01736 case LCK_ByRef: 01737 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture()) 01738 OS << '&'; 01739 OS << C->getCapturedVar()->getName(); 01740 break; 01741 01742 case LCK_ByCopy: 01743 OS << C->getCapturedVar()->getName(); 01744 break; 01745 case LCK_VLAType: 01746 llvm_unreachable("VLA type in explicit captures."); 01747 } 01748 01749 if (C->isInitCapture()) 01750 PrintExpr(C->getCapturedVar()->getInit()); 01751 } 01752 OS << ']'; 01753 01754 if (Node->hasExplicitParameters()) { 01755 OS << " ("; 01756 CXXMethodDecl *Method = Node->getCallOperator(); 01757 NeedComma = false; 01758 for (auto P : Method->params()) { 01759 if (NeedComma) { 01760 OS << ", "; 01761 } else { 01762 NeedComma = true; 01763 } 01764 std::string ParamStr = P->getNameAsString(); 01765 P->getOriginalType().print(OS, Policy, ParamStr); 01766 } 01767 if (Method->isVariadic()) { 01768 if (NeedComma) 01769 OS << ", "; 01770 OS << "..."; 01771 } 01772 OS << ')'; 01773 01774 if (Node->isMutable()) 01775 OS << " mutable"; 01776 01777 const FunctionProtoType *Proto 01778 = Method->getType()->getAs<FunctionProtoType>(); 01779 Proto->printExceptionSpecification(OS, Policy); 01780 01781 // FIXME: Attributes 01782 01783 // Print the trailing return type if it was specified in the source. 01784 if (Node->hasExplicitResultType()) { 01785 OS << " -> "; 01786 Proto->getReturnType().print(OS, Policy); 01787 } 01788 } 01789 01790 // Print the body. 01791 CompoundStmt *Body = Node->getBody(); 01792 OS << ' '; 01793 PrintStmt(Body); 01794 } 01795 01796 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 01797 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 01798 TSInfo->getType().print(OS, Policy); 01799 else 01800 Node->getType().print(OS, Policy); 01801 OS << "()"; 01802 } 01803 01804 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 01805 if (E->isGlobalNew()) 01806 OS << "::"; 01807 OS << "new "; 01808 unsigned NumPlace = E->getNumPlacementArgs(); 01809 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 01810 OS << "("; 01811 PrintExpr(E->getPlacementArg(0)); 01812 for (unsigned i = 1; i < NumPlace; ++i) { 01813 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 01814 break; 01815 OS << ", "; 01816 PrintExpr(E->getPlacementArg(i)); 01817 } 01818 OS << ") "; 01819 } 01820 if (E->isParenTypeId()) 01821 OS << "("; 01822 std::string TypeS; 01823 if (Expr *Size = E->getArraySize()) { 01824 llvm::raw_string_ostream s(TypeS); 01825 s << '['; 01826 Size->printPretty(s, Helper, Policy); 01827 s << ']'; 01828 } 01829 E->getAllocatedType().print(OS, Policy, TypeS); 01830 if (E->isParenTypeId()) 01831 OS << ")"; 01832 01833 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 01834 if (InitStyle) { 01835 if (InitStyle == CXXNewExpr::CallInit) 01836 OS << "("; 01837 PrintExpr(E->getInitializer()); 01838 if (InitStyle == CXXNewExpr::CallInit) 01839 OS << ")"; 01840 } 01841 } 01842 01843 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 01844 if (E->isGlobalDelete()) 01845 OS << "::"; 01846 OS << "delete "; 01847 if (E->isArrayForm()) 01848 OS << "[] "; 01849 PrintExpr(E->getArgument()); 01850 } 01851 01852 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 01853 PrintExpr(E->getBase()); 01854 if (E->isArrow()) 01855 OS << "->"; 01856 else 01857 OS << '.'; 01858 if (E->getQualifier()) 01859 E->getQualifier()->print(OS, Policy); 01860 OS << "~"; 01861 01862 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 01863 OS << II->getName(); 01864 else 01865 E->getDestroyedType().print(OS, Policy); 01866 } 01867 01868 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 01869 if (E->isListInitialization()) 01870 OS << "{ "; 01871 01872 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 01873 if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 01874 // Don't print any defaulted arguments 01875 break; 01876 } 01877 01878 if (i) OS << ", "; 01879 PrintExpr(E->getArg(i)); 01880 } 01881 01882 if (E->isListInitialization()) 01883 OS << " }"; 01884 } 01885 01886 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 01887 PrintExpr(E->getSubExpr()); 01888 } 01889 01890 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 01891 // Just forward to the subexpression. 01892 PrintExpr(E->getSubExpr()); 01893 } 01894 01895 void 01896 StmtPrinter::VisitCXXUnresolvedConstructExpr( 01897 CXXUnresolvedConstructExpr *Node) { 01898 Node->getTypeAsWritten().print(OS, Policy); 01899 OS << "("; 01900 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 01901 ArgEnd = Node->arg_end(); 01902 Arg != ArgEnd; ++Arg) { 01903 if (Arg != Node->arg_begin()) 01904 OS << ", "; 01905 PrintExpr(*Arg); 01906 } 01907 OS << ")"; 01908 } 01909 01910 void StmtPrinter::VisitCXXDependentScopeMemberExpr( 01911 CXXDependentScopeMemberExpr *Node) { 01912 if (!Node->isImplicitAccess()) { 01913 PrintExpr(Node->getBase()); 01914 OS << (Node->isArrow() ? "->" : "."); 01915 } 01916 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 01917 Qualifier->print(OS, Policy); 01918 if (Node->hasTemplateKeyword()) 01919 OS << "template "; 01920 OS << Node->getMemberNameInfo(); 01921 if (Node->hasExplicitTemplateArgs()) 01922 TemplateSpecializationType::PrintTemplateArgumentList( 01923 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 01924 } 01925 01926 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 01927 if (!Node->isImplicitAccess()) { 01928 PrintExpr(Node->getBase()); 01929 OS << (Node->isArrow() ? "->" : "."); 01930 } 01931 if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 01932 Qualifier->print(OS, Policy); 01933 if (Node->hasTemplateKeyword()) 01934 OS << "template "; 01935 OS << Node->getMemberNameInfo(); 01936 if (Node->hasExplicitTemplateArgs()) 01937 TemplateSpecializationType::PrintTemplateArgumentList( 01938 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 01939 } 01940 01941 static const char *getTypeTraitName(TypeTrait TT) { 01942 switch (TT) { 01943 #define TYPE_TRAIT_1(Spelling, Name, Key) \ 01944 case clang::UTT_##Name: return #Spelling; 01945 #define TYPE_TRAIT_2(Spelling, Name, Key) \ 01946 case clang::BTT_##Name: return #Spelling; 01947 #define TYPE_TRAIT_N(Spelling, Name, Key) \ 01948 case clang::TT_##Name: return #Spelling; 01949 #include "clang/Basic/TokenKinds.def" 01950 } 01951 llvm_unreachable("Type trait not covered by switch"); 01952 } 01953 01954 static const char *getTypeTraitName(ArrayTypeTrait ATT) { 01955 switch (ATT) { 01956 case ATT_ArrayRank: return "__array_rank"; 01957 case ATT_ArrayExtent: return "__array_extent"; 01958 } 01959 llvm_unreachable("Array type trait not covered by switch"); 01960 } 01961 01962 static const char *getExpressionTraitName(ExpressionTrait ET) { 01963 switch (ET) { 01964 case ET_IsLValueExpr: return "__is_lvalue_expr"; 01965 case ET_IsRValueExpr: return "__is_rvalue_expr"; 01966 } 01967 llvm_unreachable("Expression type trait not covered by switch"); 01968 } 01969 01970 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 01971 OS << getTypeTraitName(E->getTrait()) << "("; 01972 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 01973 if (I > 0) 01974 OS << ", "; 01975 E->getArg(I)->getType().print(OS, Policy); 01976 } 01977 OS << ")"; 01978 } 01979 01980 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 01981 OS << getTypeTraitName(E->getTrait()) << '('; 01982 E->getQueriedType().print(OS, Policy); 01983 OS << ')'; 01984 } 01985 01986 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 01987 OS << getExpressionTraitName(E->getTrait()) << '('; 01988 PrintExpr(E->getQueriedExpression()); 01989 OS << ')'; 01990 } 01991 01992 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 01993 OS << "noexcept("; 01994 PrintExpr(E->getOperand()); 01995 OS << ")"; 01996 } 01997 01998 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 01999 PrintExpr(E->getPattern()); 02000 OS << "..."; 02001 } 02002 02003 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 02004 OS << "sizeof...(" << *E->getPack() << ")"; 02005 } 02006 02007 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 02008 SubstNonTypeTemplateParmPackExpr *Node) { 02009 OS << *Node->getParameterPack(); 02010 } 02011 02012 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 02013 SubstNonTypeTemplateParmExpr *Node) { 02014 Visit(Node->getReplacement()); 02015 } 02016 02017 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 02018 OS << *E->getParameterPack(); 02019 } 02020 02021 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 02022 PrintExpr(Node->GetTemporaryExpr()); 02023 } 02024 02025 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) { 02026 OS << "("; 02027 if (E->getLHS()) { 02028 PrintExpr(E->getLHS()); 02029 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 02030 } 02031 OS << "..."; 02032 if (E->getRHS()) { 02033 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " "; 02034 PrintExpr(E->getRHS()); 02035 } 02036 OS << ")"; 02037 } 02038 02039 // Obj-C 02040 02041 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 02042 OS << "@"; 02043 VisitStringLiteral(Node->getString()); 02044 } 02045 02046 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 02047 OS << "@"; 02048 Visit(E->getSubExpr()); 02049 } 02050 02051 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 02052 OS << "@[ "; 02053 StmtRange ch = E->children(); 02054 if (ch.first != ch.second) { 02055 while (1) { 02056 Visit(*ch.first); 02057 ++ch.first; 02058 if (ch.first == ch.second) break; 02059 OS << ", "; 02060 } 02061 } 02062 OS << " ]"; 02063 } 02064 02065 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 02066 OS << "@{ "; 02067 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 02068 if (I > 0) 02069 OS << ", "; 02070 02071 ObjCDictionaryElement Element = E->getKeyValueElement(I); 02072 Visit(Element.Key); 02073 OS << " : "; 02074 Visit(Element.Value); 02075 if (Element.isPackExpansion()) 02076 OS << "..."; 02077 } 02078 OS << " }"; 02079 } 02080 02081 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 02082 OS << "@encode("; 02083 Node->getEncodedType().print(OS, Policy); 02084 OS << ')'; 02085 } 02086 02087 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 02088 OS << "@selector("; 02089 Node->getSelector().print(OS); 02090 OS << ')'; 02091 } 02092 02093 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 02094 OS << "@protocol(" << *Node->getProtocol() << ')'; 02095 } 02096 02097 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 02098 OS << "["; 02099 switch (Mess->getReceiverKind()) { 02100 case ObjCMessageExpr::Instance: 02101 PrintExpr(Mess->getInstanceReceiver()); 02102 break; 02103 02104 case ObjCMessageExpr::Class: 02105 Mess->getClassReceiver().print(OS, Policy); 02106 break; 02107 02108 case ObjCMessageExpr::SuperInstance: 02109 case ObjCMessageExpr::SuperClass: 02110 OS << "Super"; 02111 break; 02112 } 02113 02114 OS << ' '; 02115 Selector selector = Mess->getSelector(); 02116 if (selector.isUnarySelector()) { 02117 OS << selector.getNameForSlot(0); 02118 } else { 02119 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 02120 if (i < selector.getNumArgs()) { 02121 if (i > 0) OS << ' '; 02122 if (selector.getIdentifierInfoForSlot(i)) 02123 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 02124 else 02125 OS << ":"; 02126 } 02127 else OS << ", "; // Handle variadic methods. 02128 02129 PrintExpr(Mess->getArg(i)); 02130 } 02131 } 02132 OS << "]"; 02133 } 02134 02135 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 02136 OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 02137 } 02138 02139 void 02140 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 02141 PrintExpr(E->getSubExpr()); 02142 } 02143 02144 void 02145 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 02146 OS << '(' << E->getBridgeKindName(); 02147 E->getType().print(OS, Policy); 02148 OS << ')'; 02149 PrintExpr(E->getSubExpr()); 02150 } 02151 02152 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 02153 BlockDecl *BD = Node->getBlockDecl(); 02154 OS << "^"; 02155 02156 const FunctionType *AFT = Node->getFunctionType(); 02157 02158 if (isa<FunctionNoProtoType>(AFT)) { 02159 OS << "()"; 02160 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 02161 OS << '('; 02162 for (BlockDecl::param_iterator AI = BD->param_begin(), 02163 E = BD->param_end(); AI != E; ++AI) { 02164 if (AI != BD->param_begin()) OS << ", "; 02165 std::string ParamStr = (*AI)->getNameAsString(); 02166 (*AI)->getType().print(OS, Policy, ParamStr); 02167 } 02168 02169 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 02170 if (FT->isVariadic()) { 02171 if (!BD->param_empty()) OS << ", "; 02172 OS << "..."; 02173 } 02174 OS << ')'; 02175 } 02176 OS << "{ }"; 02177 } 02178 02179 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 02180 PrintExpr(Node->getSourceExpr()); 02181 } 02182 02183 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) { 02184 // TODO: Print something reasonable for a TypoExpr, if necessary. 02185 assert(false && "Cannot print TypoExpr nodes"); 02186 } 02187 02188 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 02189 OS << "__builtin_astype("; 02190 PrintExpr(Node->getSrcExpr()); 02191 OS << ", "; 02192 Node->getType().print(OS, Policy); 02193 OS << ")"; 02194 } 02195 02196 //===----------------------------------------------------------------------===// 02197 // Stmt method implementations 02198 //===----------------------------------------------------------------------===// 02199 02200 void Stmt::dumpPretty(const ASTContext &Context) const { 02201 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); 02202 } 02203 02204 void Stmt::printPretty(raw_ostream &OS, 02205 PrinterHelper *Helper, 02206 const PrintingPolicy &Policy, 02207 unsigned Indentation) const { 02208 StmtPrinter P(OS, Helper, Policy, Indentation); 02209 P.Visit(const_cast<Stmt*>(this)); 02210 } 02211 02212 //===----------------------------------------------------------------------===// 02213 // PrinterHelper 02214 //===----------------------------------------------------------------------===// 02215 02216 // Implement virtual destructor. 02217 PrinterHelper::~PrinterHelper() {}