StmtPrinter.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  1. //===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
  10. // pretty print the AST back out to C code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Attr.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclBase.h"
  17. #include "clang/AST/DeclCXX.h"
  18. #include "clang/AST/DeclObjC.h"
  19. #include "clang/AST/DeclOpenMP.h"
  20. #include "clang/AST/DeclTemplate.h"
  21. #include "clang/AST/Expr.h"
  22. #include "clang/AST/ExprCXX.h"
  23. #include "clang/AST/ExprObjC.h"
  24. #include "clang/AST/ExprOpenMP.h"
  25. #include "clang/AST/NestedNameSpecifier.h"
  26. #include "clang/AST/OpenMPClause.h"
  27. #include "clang/AST/PrettyPrinter.h"
  28. #include "clang/AST/Stmt.h"
  29. #include "clang/AST/StmtCXX.h"
  30. #include "clang/AST/StmtObjC.h"
  31. #include "clang/AST/StmtOpenMP.h"
  32. #include "clang/AST/StmtVisitor.h"
  33. #include "clang/AST/TemplateBase.h"
  34. #include "clang/AST/Type.h"
  35. #include "clang/Basic/CharInfo.h"
  36. #include "clang/Basic/ExpressionTraits.h"
  37. #include "clang/Basic/IdentifierTable.h"
  38. #include "clang/Basic/JsonSupport.h"
  39. #include "clang/Basic/LLVM.h"
  40. #include "clang/Basic/Lambda.h"
  41. #include "clang/Basic/OpenMPKinds.h"
  42. #include "clang/Basic/OperatorKinds.h"
  43. #include "clang/Basic/SourceLocation.h"
  44. #include "clang/Basic/TypeTraits.h"
  45. #include "clang/Lex/Lexer.h"
  46. #include "llvm/ADT/ArrayRef.h"
  47. #include "llvm/ADT/SmallString.h"
  48. #include "llvm/ADT/SmallVector.h"
  49. #include "llvm/ADT/StringRef.h"
  50. #include "llvm/Support/Casting.h"
  51. #include "llvm/Support/Compiler.h"
  52. #include "llvm/Support/ErrorHandling.h"
  53. #include "llvm/Support/Format.h"
  54. #include "llvm/Support/raw_ostream.h"
  55. #include <cassert>
  56. #include <string>
  57. using namespace clang;
  58. //===----------------------------------------------------------------------===//
  59. // StmtPrinter Visitor
  60. //===----------------------------------------------------------------------===//
  61. namespace {
  62. class StmtPrinter : public StmtVisitor<StmtPrinter> {
  63. raw_ostream &OS;
  64. unsigned IndentLevel;
  65. PrinterHelper* Helper;
  66. PrintingPolicy Policy;
  67. std::string NL;
  68. const ASTContext *Context;
  69. public:
  70. StmtPrinter(raw_ostream &os, PrinterHelper *helper,
  71. const PrintingPolicy &Policy, unsigned Indentation = 0,
  72. StringRef NL = "\n",
  73. const ASTContext *Context = nullptr)
  74. : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
  75. NL(NL), Context(Context) {}
  76. void PrintStmt(Stmt *S) {
  77. PrintStmt(S, Policy.Indentation);
  78. }
  79. void PrintStmt(Stmt *S, int SubIndent) {
  80. IndentLevel += SubIndent;
  81. if (S && isa<Expr>(S)) {
  82. // If this is an expr used in a stmt context, indent and newline it.
  83. Indent();
  84. Visit(S);
  85. OS << ";" << NL;
  86. } else if (S) {
  87. Visit(S);
  88. } else {
  89. Indent() << "<<<NULL STATEMENT>>>" << NL;
  90. }
  91. IndentLevel -= SubIndent;
  92. }
  93. void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
  94. // FIXME: Cope better with odd prefix widths.
  95. IndentLevel += (PrefixWidth + 1) / 2;
  96. if (auto *DS = dyn_cast<DeclStmt>(S))
  97. PrintRawDeclStmt(DS);
  98. else
  99. PrintExpr(cast<Expr>(S));
  100. OS << "; ";
  101. IndentLevel -= (PrefixWidth + 1) / 2;
  102. }
  103. void PrintControlledStmt(Stmt *S) {
  104. if (auto *CS = dyn_cast<CompoundStmt>(S)) {
  105. OS << " ";
  106. PrintRawCompoundStmt(CS);
  107. OS << NL;
  108. } else {
  109. OS << NL;
  110. PrintStmt(S);
  111. }
  112. }
  113. void PrintRawCompoundStmt(CompoundStmt *S);
  114. void PrintRawDecl(Decl *D);
  115. void PrintRawDeclStmt(const DeclStmt *S);
  116. void PrintRawIfStmt(IfStmt *If);
  117. void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
  118. void PrintCallArgs(CallExpr *E);
  119. void PrintRawSEHExceptHandler(SEHExceptStmt *S);
  120. void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
  121. void PrintOMPExecutableDirective(OMPExecutableDirective *S,
  122. bool ForceNoStmt = false);
  123. void PrintExpr(Expr *E) {
  124. if (E)
  125. Visit(E);
  126. else
  127. OS << "<null expr>";
  128. }
  129. raw_ostream &Indent(int Delta = 0) {
  130. for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
  131. OS << " ";
  132. return OS;
  133. }
  134. void Visit(Stmt* S) {
  135. if (Helper && Helper->handledStmt(S,OS))
  136. return;
  137. else StmtVisitor<StmtPrinter>::Visit(S);
  138. }
  139. void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
  140. Indent() << "<<unknown stmt type>>" << NL;
  141. }
  142. void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
  143. OS << "<<unknown expr type>>";
  144. }
  145. void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
  146. #define ABSTRACT_STMT(CLASS)
  147. #define STMT(CLASS, PARENT) \
  148. void Visit##CLASS(CLASS *Node);
  149. #include "clang/AST/StmtNodes.inc"
  150. };
  151. } // namespace
  152. //===----------------------------------------------------------------------===//
  153. // Stmt printing methods.
  154. //===----------------------------------------------------------------------===//
  155. /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
  156. /// with no newline after the }.
  157. void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
  158. OS << "{" << NL;
  159. for (auto *I : Node->body())
  160. PrintStmt(I);
  161. Indent() << "}";
  162. }
  163. void StmtPrinter::PrintRawDecl(Decl *D) {
  164. D->print(OS, Policy, IndentLevel);
  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. void StmtPrinter::VisitNullStmt(NullStmt *Node) {
  171. Indent() << ";" << NL;
  172. }
  173. void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
  174. Indent();
  175. PrintRawDeclStmt(Node);
  176. OS << ";" << NL;
  177. }
  178. void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
  179. Indent();
  180. PrintRawCompoundStmt(Node);
  181. OS << "" << NL;
  182. }
  183. void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
  184. Indent(-1) << "case ";
  185. PrintExpr(Node->getLHS());
  186. if (Node->getRHS()) {
  187. OS << " ... ";
  188. PrintExpr(Node->getRHS());
  189. }
  190. OS << ":" << NL;
  191. PrintStmt(Node->getSubStmt(), 0);
  192. }
  193. void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
  194. Indent(-1) << "default:" << NL;
  195. PrintStmt(Node->getSubStmt(), 0);
  196. }
  197. void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
  198. Indent(-1) << Node->getName() << ":" << NL;
  199. PrintStmt(Node->getSubStmt(), 0);
  200. }
  201. void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
  202. for (const auto *Attr : Node->getAttrs()) {
  203. Attr->printPretty(OS, Policy);
  204. }
  205. PrintStmt(Node->getSubStmt(), 0);
  206. }
  207. void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
  208. OS << "if (";
  209. if (If->getInit())
  210. PrintInitStmt(If->getInit(), 4);
  211. if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
  212. PrintRawDeclStmt(DS);
  213. else
  214. PrintExpr(If->getCond());
  215. OS << ')';
  216. if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
  217. OS << ' ';
  218. PrintRawCompoundStmt(CS);
  219. OS << (If->getElse() ? " " : NL);
  220. } else {
  221. OS << NL;
  222. PrintStmt(If->getThen());
  223. if (If->getElse()) Indent();
  224. }
  225. if (Stmt *Else = If->getElse()) {
  226. OS << "else";
  227. if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
  228. OS << ' ';
  229. PrintRawCompoundStmt(CS);
  230. OS << NL;
  231. } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
  232. OS << ' ';
  233. PrintRawIfStmt(ElseIf);
  234. } else {
  235. OS << NL;
  236. PrintStmt(If->getElse());
  237. }
  238. }
  239. }
  240. void StmtPrinter::VisitIfStmt(IfStmt *If) {
  241. Indent();
  242. PrintRawIfStmt(If);
  243. }
  244. void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
  245. Indent() << "switch (";
  246. if (Node->getInit())
  247. PrintInitStmt(Node->getInit(), 8);
  248. if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
  249. PrintRawDeclStmt(DS);
  250. else
  251. PrintExpr(Node->getCond());
  252. OS << ")";
  253. PrintControlledStmt(Node->getBody());
  254. }
  255. void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
  256. Indent() << "while (";
  257. if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
  258. PrintRawDeclStmt(DS);
  259. else
  260. PrintExpr(Node->getCond());
  261. OS << ")" << NL;
  262. PrintStmt(Node->getBody());
  263. }
  264. void StmtPrinter::VisitDoStmt(DoStmt *Node) {
  265. Indent() << "do ";
  266. if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
  267. PrintRawCompoundStmt(CS);
  268. OS << " ";
  269. } else {
  270. OS << NL;
  271. PrintStmt(Node->getBody());
  272. Indent();
  273. }
  274. OS << "while (";
  275. PrintExpr(Node->getCond());
  276. OS << ");" << NL;
  277. }
  278. void StmtPrinter::VisitForStmt(ForStmt *Node) {
  279. Indent() << "for (";
  280. if (Node->getInit())
  281. PrintInitStmt(Node->getInit(), 5);
  282. else
  283. OS << (Node->getCond() ? "; " : ";");
  284. if (Node->getCond())
  285. PrintExpr(Node->getCond());
  286. OS << ";";
  287. if (Node->getInc()) {
  288. OS << " ";
  289. PrintExpr(Node->getInc());
  290. }
  291. OS << ")";
  292. PrintControlledStmt(Node->getBody());
  293. }
  294. void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
  295. Indent() << "for (";
  296. if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
  297. PrintRawDeclStmt(DS);
  298. else
  299. PrintExpr(cast<Expr>(Node->getElement()));
  300. OS << " in ";
  301. PrintExpr(Node->getCollection());
  302. OS << ")";
  303. PrintControlledStmt(Node->getBody());
  304. }
  305. void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
  306. Indent() << "for (";
  307. if (Node->getInit())
  308. PrintInitStmt(Node->getInit(), 5);
  309. PrintingPolicy SubPolicy(Policy);
  310. SubPolicy.SuppressInitializers = true;
  311. Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
  312. OS << " : ";
  313. PrintExpr(Node->getRangeInit());
  314. OS << ")";
  315. PrintControlledStmt(Node->getBody());
  316. }
  317. void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
  318. Indent();
  319. if (Node->isIfExists())
  320. OS << "__if_exists (";
  321. else
  322. OS << "__if_not_exists (";
  323. if (NestedNameSpecifier *Qualifier
  324. = Node->getQualifierLoc().getNestedNameSpecifier())
  325. Qualifier->print(OS, Policy);
  326. OS << Node->getNameInfo() << ") ";
  327. PrintRawCompoundStmt(Node->getSubStmt());
  328. }
  329. void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
  330. Indent() << "goto " << Node->getLabel()->getName() << ";";
  331. if (Policy.IncludeNewlines) OS << NL;
  332. }
  333. void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
  334. Indent() << "goto *";
  335. PrintExpr(Node->getTarget());
  336. OS << ";";
  337. if (Policy.IncludeNewlines) OS << NL;
  338. }
  339. void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
  340. Indent() << "continue;";
  341. if (Policy.IncludeNewlines) OS << NL;
  342. }
  343. void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
  344. Indent() << "break;";
  345. if (Policy.IncludeNewlines) OS << NL;
  346. }
  347. void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
  348. Indent() << "return";
  349. if (Node->getRetValue()) {
  350. OS << " ";
  351. PrintExpr(Node->getRetValue());
  352. }
  353. OS << ";";
  354. if (Policy.IncludeNewlines) OS << NL;
  355. }
  356. void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
  357. Indent() << "asm ";
  358. if (Node->isVolatile())
  359. OS << "volatile ";
  360. if (Node->isAsmGoto())
  361. OS << "goto ";
  362. OS << "(";
  363. VisitStringLiteral(Node->getAsmString());
  364. // Outputs
  365. if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
  366. Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
  367. OS << " : ";
  368. for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
  369. if (i != 0)
  370. OS << ", ";
  371. if (!Node->getOutputName(i).empty()) {
  372. OS << '[';
  373. OS << Node->getOutputName(i);
  374. OS << "] ";
  375. }
  376. VisitStringLiteral(Node->getOutputConstraintLiteral(i));
  377. OS << " (";
  378. Visit(Node->getOutputExpr(i));
  379. OS << ")";
  380. }
  381. // Inputs
  382. if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
  383. Node->getNumLabels() != 0)
  384. OS << " : ";
  385. for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
  386. if (i != 0)
  387. OS << ", ";
  388. if (!Node->getInputName(i).empty()) {
  389. OS << '[';
  390. OS << Node->getInputName(i);
  391. OS << "] ";
  392. }
  393. VisitStringLiteral(Node->getInputConstraintLiteral(i));
  394. OS << " (";
  395. Visit(Node->getInputExpr(i));
  396. OS << ")";
  397. }
  398. // Clobbers
  399. if (Node->getNumClobbers() != 0 || Node->getNumLabels())
  400. OS << " : ";
  401. for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
  402. if (i != 0)
  403. OS << ", ";
  404. VisitStringLiteral(Node->getClobberStringLiteral(i));
  405. }
  406. // Labels
  407. if (Node->getNumLabels() != 0)
  408. OS << " : ";
  409. for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
  410. if (i != 0)
  411. OS << ", ";
  412. OS << Node->getLabelName(i);
  413. }
  414. OS << ");";
  415. if (Policy.IncludeNewlines) OS << NL;
  416. }
  417. void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
  418. // FIXME: Implement MS style inline asm statement printer.
  419. Indent() << "__asm ";
  420. if (Node->hasBraces())
  421. OS << "{" << NL;
  422. OS << Node->getAsmString() << NL;
  423. if (Node->hasBraces())
  424. Indent() << "}" << NL;
  425. }
  426. void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
  427. PrintStmt(Node->getCapturedDecl()->getBody());
  428. }
  429. void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
  430. Indent() << "@try";
  431. if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
  432. PrintRawCompoundStmt(TS);
  433. OS << NL;
  434. }
  435. for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
  436. ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
  437. Indent() << "@catch(";
  438. if (catchStmt->getCatchParamDecl()) {
  439. if (Decl *DS = catchStmt->getCatchParamDecl())
  440. PrintRawDecl(DS);
  441. }
  442. OS << ")";
  443. if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
  444. PrintRawCompoundStmt(CS);
  445. OS << NL;
  446. }
  447. }
  448. if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
  449. Indent() << "@finally";
  450. PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
  451. OS << NL;
  452. }
  453. }
  454. void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
  455. }
  456. void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
  457. Indent() << "@catch (...) { /* todo */ } " << NL;
  458. }
  459. void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
  460. Indent() << "@throw";
  461. if (Node->getThrowExpr()) {
  462. OS << " ";
  463. PrintExpr(Node->getThrowExpr());
  464. }
  465. OS << ";" << NL;
  466. }
  467. void StmtPrinter::VisitObjCAvailabilityCheckExpr(
  468. ObjCAvailabilityCheckExpr *Node) {
  469. OS << "@available(...)";
  470. }
  471. void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
  472. Indent() << "@synchronized (";
  473. PrintExpr(Node->getSynchExpr());
  474. OS << ")";
  475. PrintRawCompoundStmt(Node->getSynchBody());
  476. OS << NL;
  477. }
  478. void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
  479. Indent() << "@autoreleasepool";
  480. PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
  481. OS << NL;
  482. }
  483. void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
  484. OS << "catch (";
  485. if (Decl *ExDecl = Node->getExceptionDecl())
  486. PrintRawDecl(ExDecl);
  487. else
  488. OS << "...";
  489. OS << ") ";
  490. PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
  491. }
  492. void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
  493. Indent();
  494. PrintRawCXXCatchStmt(Node);
  495. OS << NL;
  496. }
  497. void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
  498. Indent() << "try ";
  499. PrintRawCompoundStmt(Node->getTryBlock());
  500. for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
  501. OS << " ";
  502. PrintRawCXXCatchStmt(Node->getHandler(i));
  503. }
  504. OS << NL;
  505. }
  506. void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
  507. Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
  508. PrintRawCompoundStmt(Node->getTryBlock());
  509. SEHExceptStmt *E = Node->getExceptHandler();
  510. SEHFinallyStmt *F = Node->getFinallyHandler();
  511. if(E)
  512. PrintRawSEHExceptHandler(E);
  513. else {
  514. assert(F && "Must have a finally block...");
  515. PrintRawSEHFinallyStmt(F);
  516. }
  517. OS << NL;
  518. }
  519. void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
  520. OS << "__finally ";
  521. PrintRawCompoundStmt(Node->getBlock());
  522. OS << NL;
  523. }
  524. void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
  525. OS << "__except (";
  526. VisitExpr(Node->getFilterExpr());
  527. OS << ")" << NL;
  528. PrintRawCompoundStmt(Node->getBlock());
  529. OS << NL;
  530. }
  531. void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
  532. Indent();
  533. PrintRawSEHExceptHandler(Node);
  534. OS << NL;
  535. }
  536. void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
  537. Indent();
  538. PrintRawSEHFinallyStmt(Node);
  539. OS << NL;
  540. }
  541. void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
  542. Indent() << "__leave;";
  543. if (Policy.IncludeNewlines) OS << NL;
  544. }
  545. //===----------------------------------------------------------------------===//
  546. // OpenMP directives printing methods
  547. //===----------------------------------------------------------------------===//
  548. void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
  549. bool ForceNoStmt) {
  550. OMPClausePrinter Printer(OS, Policy);
  551. ArrayRef<OMPClause *> Clauses = S->clauses();
  552. for (auto *Clause : Clauses)
  553. if (Clause && !Clause->isImplicit()) {
  554. OS << ' ';
  555. Printer.Visit(Clause);
  556. }
  557. OS << NL;
  558. if (!ForceNoStmt && S->hasAssociatedStmt())
  559. PrintStmt(S->getInnermostCapturedStmt()->getCapturedStmt());
  560. }
  561. void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
  562. Indent() << "#pragma omp parallel";
  563. PrintOMPExecutableDirective(Node);
  564. }
  565. void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
  566. Indent() << "#pragma omp simd";
  567. PrintOMPExecutableDirective(Node);
  568. }
  569. void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
  570. Indent() << "#pragma omp for";
  571. PrintOMPExecutableDirective(Node);
  572. }
  573. void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
  574. Indent() << "#pragma omp for simd";
  575. PrintOMPExecutableDirective(Node);
  576. }
  577. void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
  578. Indent() << "#pragma omp sections";
  579. PrintOMPExecutableDirective(Node);
  580. }
  581. void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
  582. Indent() << "#pragma omp section";
  583. PrintOMPExecutableDirective(Node);
  584. }
  585. void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
  586. Indent() << "#pragma omp single";
  587. PrintOMPExecutableDirective(Node);
  588. }
  589. void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
  590. Indent() << "#pragma omp master";
  591. PrintOMPExecutableDirective(Node);
  592. }
  593. void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
  594. Indent() << "#pragma omp critical";
  595. if (Node->getDirectiveName().getName()) {
  596. OS << " (";
  597. Node->getDirectiveName().printName(OS);
  598. OS << ")";
  599. }
  600. PrintOMPExecutableDirective(Node);
  601. }
  602. void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
  603. Indent() << "#pragma omp parallel for";
  604. PrintOMPExecutableDirective(Node);
  605. }
  606. void StmtPrinter::VisitOMPParallelForSimdDirective(
  607. OMPParallelForSimdDirective *Node) {
  608. Indent() << "#pragma omp parallel for simd";
  609. PrintOMPExecutableDirective(Node);
  610. }
  611. void StmtPrinter::VisitOMPParallelSectionsDirective(
  612. OMPParallelSectionsDirective *Node) {
  613. Indent() << "#pragma omp parallel sections";
  614. PrintOMPExecutableDirective(Node);
  615. }
  616. void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
  617. Indent() << "#pragma omp task";
  618. PrintOMPExecutableDirective(Node);
  619. }
  620. void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
  621. Indent() << "#pragma omp taskyield";
  622. PrintOMPExecutableDirective(Node);
  623. }
  624. void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
  625. Indent() << "#pragma omp barrier";
  626. PrintOMPExecutableDirective(Node);
  627. }
  628. void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
  629. Indent() << "#pragma omp taskwait";
  630. PrintOMPExecutableDirective(Node);
  631. }
  632. void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
  633. Indent() << "#pragma omp taskgroup";
  634. PrintOMPExecutableDirective(Node);
  635. }
  636. void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
  637. Indent() << "#pragma omp flush";
  638. PrintOMPExecutableDirective(Node);
  639. }
  640. void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
  641. Indent() << "#pragma omp ordered";
  642. PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
  643. }
  644. void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
  645. Indent() << "#pragma omp atomic";
  646. PrintOMPExecutableDirective(Node);
  647. }
  648. void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
  649. Indent() << "#pragma omp target";
  650. PrintOMPExecutableDirective(Node);
  651. }
  652. void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
  653. Indent() << "#pragma omp target data";
  654. PrintOMPExecutableDirective(Node);
  655. }
  656. void StmtPrinter::VisitOMPTargetEnterDataDirective(
  657. OMPTargetEnterDataDirective *Node) {
  658. Indent() << "#pragma omp target enter data";
  659. PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
  660. }
  661. void StmtPrinter::VisitOMPTargetExitDataDirective(
  662. OMPTargetExitDataDirective *Node) {
  663. Indent() << "#pragma omp target exit data";
  664. PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
  665. }
  666. void StmtPrinter::VisitOMPTargetParallelDirective(
  667. OMPTargetParallelDirective *Node) {
  668. Indent() << "#pragma omp target parallel";
  669. PrintOMPExecutableDirective(Node);
  670. }
  671. void StmtPrinter::VisitOMPTargetParallelForDirective(
  672. OMPTargetParallelForDirective *Node) {
  673. Indent() << "#pragma omp target parallel for";
  674. PrintOMPExecutableDirective(Node);
  675. }
  676. void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
  677. Indent() << "#pragma omp teams";
  678. PrintOMPExecutableDirective(Node);
  679. }
  680. void StmtPrinter::VisitOMPCancellationPointDirective(
  681. OMPCancellationPointDirective *Node) {
  682. Indent() << "#pragma omp cancellation point "
  683. << getOpenMPDirectiveName(Node->getCancelRegion());
  684. PrintOMPExecutableDirective(Node);
  685. }
  686. void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
  687. Indent() << "#pragma omp cancel "
  688. << getOpenMPDirectiveName(Node->getCancelRegion());
  689. PrintOMPExecutableDirective(Node);
  690. }
  691. void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
  692. Indent() << "#pragma omp taskloop";
  693. PrintOMPExecutableDirective(Node);
  694. }
  695. void StmtPrinter::VisitOMPTaskLoopSimdDirective(
  696. OMPTaskLoopSimdDirective *Node) {
  697. Indent() << "#pragma omp taskloop simd";
  698. PrintOMPExecutableDirective(Node);
  699. }
  700. void StmtPrinter::VisitOMPMasterTaskLoopDirective(
  701. OMPMasterTaskLoopDirective *Node) {
  702. Indent() << "#pragma omp master taskloop";
  703. PrintOMPExecutableDirective(Node);
  704. }
  705. void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
  706. OMPMasterTaskLoopSimdDirective *Node) {
  707. Indent() << "#pragma omp master taskloop simd";
  708. PrintOMPExecutableDirective(Node);
  709. }
  710. void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
  711. OMPParallelMasterTaskLoopDirective *Node) {
  712. Indent() << "#pragma omp parallel master taskloop";
  713. PrintOMPExecutableDirective(Node);
  714. }
  715. void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
  716. Indent() << "#pragma omp distribute";
  717. PrintOMPExecutableDirective(Node);
  718. }
  719. void StmtPrinter::VisitOMPTargetUpdateDirective(
  720. OMPTargetUpdateDirective *Node) {
  721. Indent() << "#pragma omp target update";
  722. PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
  723. }
  724. void StmtPrinter::VisitOMPDistributeParallelForDirective(
  725. OMPDistributeParallelForDirective *Node) {
  726. Indent() << "#pragma omp distribute parallel for";
  727. PrintOMPExecutableDirective(Node);
  728. }
  729. void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
  730. OMPDistributeParallelForSimdDirective *Node) {
  731. Indent() << "#pragma omp distribute parallel for simd";
  732. PrintOMPExecutableDirective(Node);
  733. }
  734. void StmtPrinter::VisitOMPDistributeSimdDirective(
  735. OMPDistributeSimdDirective *Node) {
  736. Indent() << "#pragma omp distribute simd";
  737. PrintOMPExecutableDirective(Node);
  738. }
  739. void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
  740. OMPTargetParallelForSimdDirective *Node) {
  741. Indent() << "#pragma omp target parallel for simd";
  742. PrintOMPExecutableDirective(Node);
  743. }
  744. void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
  745. Indent() << "#pragma omp target simd";
  746. PrintOMPExecutableDirective(Node);
  747. }
  748. void StmtPrinter::VisitOMPTeamsDistributeDirective(
  749. OMPTeamsDistributeDirective *Node) {
  750. Indent() << "#pragma omp teams distribute";
  751. PrintOMPExecutableDirective(Node);
  752. }
  753. void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
  754. OMPTeamsDistributeSimdDirective *Node) {
  755. Indent() << "#pragma omp teams distribute simd";
  756. PrintOMPExecutableDirective(Node);
  757. }
  758. void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
  759. OMPTeamsDistributeParallelForSimdDirective *Node) {
  760. Indent() << "#pragma omp teams distribute parallel for simd";
  761. PrintOMPExecutableDirective(Node);
  762. }
  763. void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
  764. OMPTeamsDistributeParallelForDirective *Node) {
  765. Indent() << "#pragma omp teams distribute parallel for";
  766. PrintOMPExecutableDirective(Node);
  767. }
  768. void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
  769. Indent() << "#pragma omp target teams";
  770. PrintOMPExecutableDirective(Node);
  771. }
  772. void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
  773. OMPTargetTeamsDistributeDirective *Node) {
  774. Indent() << "#pragma omp target teams distribute";
  775. PrintOMPExecutableDirective(Node);
  776. }
  777. void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
  778. OMPTargetTeamsDistributeParallelForDirective *Node) {
  779. Indent() << "#pragma omp target teams distribute parallel for";
  780. PrintOMPExecutableDirective(Node);
  781. }
  782. void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
  783. OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
  784. Indent() << "#pragma omp target teams distribute parallel for simd";
  785. PrintOMPExecutableDirective(Node);
  786. }
  787. void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
  788. OMPTargetTeamsDistributeSimdDirective *Node) {
  789. Indent() << "#pragma omp target teams distribute simd";
  790. PrintOMPExecutableDirective(Node);
  791. }
  792. //===----------------------------------------------------------------------===//
  793. // Expr printing methods.
  794. //===----------------------------------------------------------------------===//
  795. void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
  796. OS << Node->getBuiltinStr() << "()";
  797. }
  798. void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
  799. PrintExpr(Node->getSubExpr());
  800. }
  801. void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
  802. if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
  803. OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
  804. return;
  805. }
  806. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  807. Qualifier->print(OS, Policy);
  808. if (Node->hasTemplateKeyword())
  809. OS << "template ";
  810. OS << Node->getNameInfo();
  811. if (Node->hasExplicitTemplateArgs())
  812. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  813. }
  814. void StmtPrinter::VisitDependentScopeDeclRefExpr(
  815. DependentScopeDeclRefExpr *Node) {
  816. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  817. Qualifier->print(OS, Policy);
  818. if (Node->hasTemplateKeyword())
  819. OS << "template ";
  820. OS << Node->getNameInfo();
  821. if (Node->hasExplicitTemplateArgs())
  822. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  823. }
  824. void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
  825. if (Node->getQualifier())
  826. Node->getQualifier()->print(OS, Policy);
  827. if (Node->hasTemplateKeyword())
  828. OS << "template ";
  829. OS << Node->getNameInfo();
  830. if (Node->hasExplicitTemplateArgs())
  831. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  832. }
  833. static bool isImplicitSelf(const Expr *E) {
  834. if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
  835. if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
  836. if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
  837. DRE->getBeginLoc().isInvalid())
  838. return true;
  839. }
  840. }
  841. return false;
  842. }
  843. void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
  844. if (Node->getBase()) {
  845. if (!Policy.SuppressImplicitBase ||
  846. !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
  847. PrintExpr(Node->getBase());
  848. OS << (Node->isArrow() ? "->" : ".");
  849. }
  850. }
  851. OS << *Node->getDecl();
  852. }
  853. void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
  854. if (Node->isSuperReceiver())
  855. OS << "super.";
  856. else if (Node->isObjectReceiver() && Node->getBase()) {
  857. PrintExpr(Node->getBase());
  858. OS << ".";
  859. } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
  860. OS << Node->getClassReceiver()->getName() << ".";
  861. }
  862. if (Node->isImplicitProperty()) {
  863. if (const auto *Getter = Node->getImplicitPropertyGetter())
  864. Getter->getSelector().print(OS);
  865. else
  866. OS << SelectorTable::getPropertyNameFromSetterSelector(
  867. Node->getImplicitPropertySetter()->getSelector());
  868. } else
  869. OS << Node->getExplicitProperty()->getName();
  870. }
  871. void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
  872. PrintExpr(Node->getBaseExpr());
  873. OS << "[";
  874. PrintExpr(Node->getKeyExpr());
  875. OS << "]";
  876. }
  877. void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
  878. OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
  879. }
  880. void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
  881. unsigned value = Node->getValue();
  882. switch (Node->getKind()) {
  883. case CharacterLiteral::Ascii: break; // no prefix.
  884. case CharacterLiteral::Wide: OS << 'L'; break;
  885. case CharacterLiteral::UTF8: OS << "u8"; break;
  886. case CharacterLiteral::UTF16: OS << 'u'; break;
  887. case CharacterLiteral::UTF32: OS << 'U'; break;
  888. }
  889. switch (value) {
  890. case '\\':
  891. OS << "'\\\\'";
  892. break;
  893. case '\'':
  894. OS << "'\\''";
  895. break;
  896. case '\a':
  897. // TODO: K&R: the meaning of '\\a' is different in traditional C
  898. OS << "'\\a'";
  899. break;
  900. case '\b':
  901. OS << "'\\b'";
  902. break;
  903. // Nonstandard escape sequence.
  904. /*case '\e':
  905. OS << "'\\e'";
  906. break;*/
  907. case '\f':
  908. OS << "'\\f'";
  909. break;
  910. case '\n':
  911. OS << "'\\n'";
  912. break;
  913. case '\r':
  914. OS << "'\\r'";
  915. break;
  916. case '\t':
  917. OS << "'\\t'";
  918. break;
  919. case '\v':
  920. OS << "'\\v'";
  921. break;
  922. default:
  923. // A character literal might be sign-extended, which
  924. // would result in an invalid \U escape sequence.
  925. // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
  926. // are not correctly handled.
  927. if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
  928. value &= 0xFFu;
  929. if (value < 256 && isPrintable((unsigned char)value))
  930. OS << "'" << (char)value << "'";
  931. else if (value < 256)
  932. OS << "'\\x" << llvm::format("%02x", value) << "'";
  933. else if (value <= 0xFFFF)
  934. OS << "'\\u" << llvm::format("%04x", value) << "'";
  935. else
  936. OS << "'\\U" << llvm::format("%08x", value) << "'";
  937. }
  938. }
  939. /// Prints the given expression using the original source text. Returns true on
  940. /// success, false otherwise.
  941. static bool printExprAsWritten(raw_ostream &OS, Expr *E,
  942. const ASTContext *Context) {
  943. if (!Context)
  944. return false;
  945. bool Invalid = false;
  946. StringRef Source = Lexer::getSourceText(
  947. CharSourceRange::getTokenRange(E->getSourceRange()),
  948. Context->getSourceManager(), Context->getLangOpts(), &Invalid);
  949. if (!Invalid) {
  950. OS << Source;
  951. return true;
  952. }
  953. return false;
  954. }
  955. void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
  956. if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
  957. return;
  958. bool isSigned = Node->getType()->isSignedIntegerType();
  959. OS << Node->getValue().toString(10, isSigned);
  960. // Emit suffixes. Integer literals are always a builtin integer type.
  961. switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
  962. default: llvm_unreachable("Unexpected type for integer literal!");
  963. case BuiltinType::Char_S:
  964. case BuiltinType::Char_U: OS << "i8"; break;
  965. case BuiltinType::UChar: OS << "Ui8"; break;
  966. case BuiltinType::Short: OS << "i16"; break;
  967. case BuiltinType::UShort: OS << "Ui16"; break;
  968. case BuiltinType::Int: break; // no suffix.
  969. case BuiltinType::UInt: OS << 'U'; break;
  970. case BuiltinType::Long: OS << 'L'; break;
  971. case BuiltinType::ULong: OS << "UL"; break;
  972. case BuiltinType::LongLong: OS << "LL"; break;
  973. case BuiltinType::ULongLong: OS << "ULL"; break;
  974. }
  975. }
  976. void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
  977. if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
  978. return;
  979. OS << Node->getValueAsString(/*Radix=*/10);
  980. switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
  981. default: llvm_unreachable("Unexpected type for fixed point literal!");
  982. case BuiltinType::ShortFract: OS << "hr"; break;
  983. case BuiltinType::ShortAccum: OS << "hk"; break;
  984. case BuiltinType::UShortFract: OS << "uhr"; break;
  985. case BuiltinType::UShortAccum: OS << "uhk"; break;
  986. case BuiltinType::Fract: OS << "r"; break;
  987. case BuiltinType::Accum: OS << "k"; break;
  988. case BuiltinType::UFract: OS << "ur"; break;
  989. case BuiltinType::UAccum: OS << "uk"; break;
  990. case BuiltinType::LongFract: OS << "lr"; break;
  991. case BuiltinType::LongAccum: OS << "lk"; break;
  992. case BuiltinType::ULongFract: OS << "ulr"; break;
  993. case BuiltinType::ULongAccum: OS << "ulk"; break;
  994. }
  995. }
  996. static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
  997. bool PrintSuffix) {
  998. SmallString<16> Str;
  999. Node->getValue().toString(Str);
  1000. OS << Str;
  1001. if (Str.find_first_not_of("-0123456789") == StringRef::npos)
  1002. OS << '.'; // Trailing dot in order to separate from ints.
  1003. if (!PrintSuffix)
  1004. return;
  1005. // Emit suffixes. Float literals are always a builtin float type.
  1006. switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
  1007. default: llvm_unreachable("Unexpected type for float literal!");
  1008. case BuiltinType::Half: break; // FIXME: suffix?
  1009. case BuiltinType::Double: break; // no suffix.
  1010. case BuiltinType::Float16: OS << "F16"; break;
  1011. case BuiltinType::Float: OS << 'F'; break;
  1012. case BuiltinType::LongDouble: OS << 'L'; break;
  1013. case BuiltinType::Float128: OS << 'Q'; break;
  1014. }
  1015. }
  1016. void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
  1017. if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
  1018. return;
  1019. PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
  1020. }
  1021. void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
  1022. PrintExpr(Node->getSubExpr());
  1023. OS << "i";
  1024. }
  1025. void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
  1026. Str->outputString(OS);
  1027. }
  1028. void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
  1029. OS << "(";
  1030. PrintExpr(Node->getSubExpr());
  1031. OS << ")";
  1032. }
  1033. void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
  1034. if (!Node->isPostfix()) {
  1035. OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
  1036. // Print a space if this is an "identifier operator" like __real, or if
  1037. // it might be concatenated incorrectly like '+'.
  1038. switch (Node->getOpcode()) {
  1039. default: break;
  1040. case UO_Real:
  1041. case UO_Imag:
  1042. case UO_Extension:
  1043. OS << ' ';
  1044. break;
  1045. case UO_Plus:
  1046. case UO_Minus:
  1047. if (isa<UnaryOperator>(Node->getSubExpr()))
  1048. OS << ' ';
  1049. break;
  1050. }
  1051. }
  1052. PrintExpr(Node->getSubExpr());
  1053. if (Node->isPostfix())
  1054. OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
  1055. }
  1056. void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
  1057. OS << "__builtin_offsetof(";
  1058. Node->getTypeSourceInfo()->getType().print(OS, Policy);
  1059. OS << ", ";
  1060. bool PrintedSomething = false;
  1061. for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
  1062. OffsetOfNode ON = Node->getComponent(i);
  1063. if (ON.getKind() == OffsetOfNode::Array) {
  1064. // Array node
  1065. OS << "[";
  1066. PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
  1067. OS << "]";
  1068. PrintedSomething = true;
  1069. continue;
  1070. }
  1071. // Skip implicit base indirections.
  1072. if (ON.getKind() == OffsetOfNode::Base)
  1073. continue;
  1074. // Field or identifier node.
  1075. IdentifierInfo *Id = ON.getFieldName();
  1076. if (!Id)
  1077. continue;
  1078. if (PrintedSomething)
  1079. OS << ".";
  1080. else
  1081. PrintedSomething = true;
  1082. OS << Id->getName();
  1083. }
  1084. OS << ")";
  1085. }
  1086. void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
  1087. switch(Node->getKind()) {
  1088. case UETT_SizeOf:
  1089. OS << "sizeof";
  1090. break;
  1091. case UETT_AlignOf:
  1092. if (Policy.Alignof)
  1093. OS << "alignof";
  1094. else if (Policy.UnderscoreAlignof)
  1095. OS << "_Alignof";
  1096. else
  1097. OS << "__alignof";
  1098. break;
  1099. case UETT_PreferredAlignOf:
  1100. OS << "__alignof";
  1101. break;
  1102. case UETT_VecStep:
  1103. OS << "vec_step";
  1104. break;
  1105. case UETT_OpenMPRequiredSimdAlign:
  1106. OS << "__builtin_omp_required_simd_align";
  1107. break;
  1108. }
  1109. if (Node->isArgumentType()) {
  1110. OS << '(';
  1111. Node->getArgumentType().print(OS, Policy);
  1112. OS << ')';
  1113. } else {
  1114. OS << " ";
  1115. PrintExpr(Node->getArgumentExpr());
  1116. }
  1117. }
  1118. void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
  1119. OS << "_Generic(";
  1120. PrintExpr(Node->getControllingExpr());
  1121. for (const GenericSelectionExpr::Association &Assoc : Node->associations()) {
  1122. OS << ", ";
  1123. QualType T = Assoc.getType();
  1124. if (T.isNull())
  1125. OS << "default";
  1126. else
  1127. T.print(OS, Policy);
  1128. OS << ": ";
  1129. PrintExpr(Assoc.getAssociationExpr());
  1130. }
  1131. OS << ")";
  1132. }
  1133. void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
  1134. PrintExpr(Node->getLHS());
  1135. OS << "[";
  1136. PrintExpr(Node->getRHS());
  1137. OS << "]";
  1138. }
  1139. void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
  1140. PrintExpr(Node->getBase());
  1141. OS << "[";
  1142. if (Node->getLowerBound())
  1143. PrintExpr(Node->getLowerBound());
  1144. if (Node->getColonLoc().isValid()) {
  1145. OS << ":";
  1146. if (Node->getLength())
  1147. PrintExpr(Node->getLength());
  1148. }
  1149. OS << "]";
  1150. }
  1151. void StmtPrinter::PrintCallArgs(CallExpr *Call) {
  1152. for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
  1153. if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
  1154. // Don't print any defaulted arguments
  1155. break;
  1156. }
  1157. if (i) OS << ", ";
  1158. PrintExpr(Call->getArg(i));
  1159. }
  1160. }
  1161. void StmtPrinter::VisitCallExpr(CallExpr *Call) {
  1162. PrintExpr(Call->getCallee());
  1163. OS << "(";
  1164. PrintCallArgs(Call);
  1165. OS << ")";
  1166. }
  1167. static bool isImplicitThis(const Expr *E) {
  1168. if (const auto *TE = dyn_cast<CXXThisExpr>(E))
  1169. return TE->isImplicit();
  1170. return false;
  1171. }
  1172. void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
  1173. if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
  1174. PrintExpr(Node->getBase());
  1175. auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
  1176. FieldDecl *ParentDecl =
  1177. ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
  1178. : nullptr;
  1179. if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
  1180. OS << (Node->isArrow() ? "->" : ".");
  1181. }
  1182. if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
  1183. if (FD->isAnonymousStructOrUnion())
  1184. return;
  1185. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1186. Qualifier->print(OS, Policy);
  1187. if (Node->hasTemplateKeyword())
  1188. OS << "template ";
  1189. OS << Node->getMemberNameInfo();
  1190. if (Node->hasExplicitTemplateArgs())
  1191. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  1192. }
  1193. void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
  1194. PrintExpr(Node->getBase());
  1195. OS << (Node->isArrow() ? "->isa" : ".isa");
  1196. }
  1197. void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
  1198. PrintExpr(Node->getBase());
  1199. OS << ".";
  1200. OS << Node->getAccessor().getName();
  1201. }
  1202. void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
  1203. OS << '(';
  1204. Node->getTypeAsWritten().print(OS, Policy);
  1205. OS << ')';
  1206. PrintExpr(Node->getSubExpr());
  1207. }
  1208. void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
  1209. OS << '(';
  1210. Node->getType().print(OS, Policy);
  1211. OS << ')';
  1212. PrintExpr(Node->getInitializer());
  1213. }
  1214. void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
  1215. // No need to print anything, simply forward to the subexpression.
  1216. PrintExpr(Node->getSubExpr());
  1217. }
  1218. void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
  1219. PrintExpr(Node->getLHS());
  1220. OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
  1221. PrintExpr(Node->getRHS());
  1222. }
  1223. void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
  1224. PrintExpr(Node->getLHS());
  1225. OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
  1226. PrintExpr(Node->getRHS());
  1227. }
  1228. void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
  1229. PrintExpr(Node->getCond());
  1230. OS << " ? ";
  1231. PrintExpr(Node->getLHS());
  1232. OS << " : ";
  1233. PrintExpr(Node->getRHS());
  1234. }
  1235. // GNU extensions.
  1236. void
  1237. StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
  1238. PrintExpr(Node->getCommon());
  1239. OS << " ?: ";
  1240. PrintExpr(Node->getFalseExpr());
  1241. }
  1242. void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
  1243. OS << "&&" << Node->getLabel()->getName();
  1244. }
  1245. void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
  1246. OS << "(";
  1247. PrintRawCompoundStmt(E->getSubStmt());
  1248. OS << ")";
  1249. }
  1250. void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
  1251. OS << "__builtin_choose_expr(";
  1252. PrintExpr(Node->getCond());
  1253. OS << ", ";
  1254. PrintExpr(Node->getLHS());
  1255. OS << ", ";
  1256. PrintExpr(Node->getRHS());
  1257. OS << ")";
  1258. }
  1259. void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
  1260. OS << "__null";
  1261. }
  1262. void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
  1263. OS << "__builtin_shufflevector(";
  1264. for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
  1265. if (i) OS << ", ";
  1266. PrintExpr(Node->getExpr(i));
  1267. }
  1268. OS << ")";
  1269. }
  1270. void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
  1271. OS << "__builtin_convertvector(";
  1272. PrintExpr(Node->getSrcExpr());
  1273. OS << ", ";
  1274. Node->getType().print(OS, Policy);
  1275. OS << ")";
  1276. }
  1277. void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
  1278. if (Node->getSyntacticForm()) {
  1279. Visit(Node->getSyntacticForm());
  1280. return;
  1281. }
  1282. OS << "{";
  1283. for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
  1284. if (i) OS << ", ";
  1285. if (Node->getInit(i))
  1286. PrintExpr(Node->getInit(i));
  1287. else
  1288. OS << "{}";
  1289. }
  1290. OS << "}";
  1291. }
  1292. void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
  1293. // There's no way to express this expression in any of our supported
  1294. // languages, so just emit something terse and (hopefully) clear.
  1295. OS << "{";
  1296. PrintExpr(Node->getSubExpr());
  1297. OS << "}";
  1298. }
  1299. void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
  1300. OS << "*";
  1301. }
  1302. void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
  1303. OS << "(";
  1304. for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
  1305. if (i) OS << ", ";
  1306. PrintExpr(Node->getExpr(i));
  1307. }
  1308. OS << ")";
  1309. }
  1310. void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
  1311. bool NeedsEquals = true;
  1312. for (const DesignatedInitExpr::Designator &D : Node->designators()) {
  1313. if (D.isFieldDesignator()) {
  1314. if (D.getDotLoc().isInvalid()) {
  1315. if (IdentifierInfo *II = D.getFieldName()) {
  1316. OS << II->getName() << ":";
  1317. NeedsEquals = false;
  1318. }
  1319. } else {
  1320. OS << "." << D.getFieldName()->getName();
  1321. }
  1322. } else {
  1323. OS << "[";
  1324. if (D.isArrayDesignator()) {
  1325. PrintExpr(Node->getArrayIndex(D));
  1326. } else {
  1327. PrintExpr(Node->getArrayRangeStart(D));
  1328. OS << " ... ";
  1329. PrintExpr(Node->getArrayRangeEnd(D));
  1330. }
  1331. OS << "]";
  1332. }
  1333. }
  1334. if (NeedsEquals)
  1335. OS << " = ";
  1336. else
  1337. OS << " ";
  1338. PrintExpr(Node->getInit());
  1339. }
  1340. void StmtPrinter::VisitDesignatedInitUpdateExpr(
  1341. DesignatedInitUpdateExpr *Node) {
  1342. OS << "{";
  1343. OS << "/*base*/";
  1344. PrintExpr(Node->getBase());
  1345. OS << ", ";
  1346. OS << "/*updater*/";
  1347. PrintExpr(Node->getUpdater());
  1348. OS << "}";
  1349. }
  1350. void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
  1351. OS << "/*no init*/";
  1352. }
  1353. void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
  1354. if (Node->getType()->getAsCXXRecordDecl()) {
  1355. OS << "/*implicit*/";
  1356. Node->getType().print(OS, Policy);
  1357. OS << "()";
  1358. } else {
  1359. OS << "/*implicit*/(";
  1360. Node->getType().print(OS, Policy);
  1361. OS << ')';
  1362. if (Node->getType()->isRecordType())
  1363. OS << "{}";
  1364. else
  1365. OS << 0;
  1366. }
  1367. }
  1368. void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
  1369. OS << "__builtin_va_arg(";
  1370. PrintExpr(Node->getSubExpr());
  1371. OS << ", ";
  1372. Node->getType().print(OS, Policy);
  1373. OS << ")";
  1374. }
  1375. void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
  1376. PrintExpr(Node->getSyntacticForm());
  1377. }
  1378. void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
  1379. const char *Name = nullptr;
  1380. switch (Node->getOp()) {
  1381. #define BUILTIN(ID, TYPE, ATTRS)
  1382. #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
  1383. case AtomicExpr::AO ## ID: \
  1384. Name = #ID "("; \
  1385. break;
  1386. #include "clang/Basic/Builtins.def"
  1387. }
  1388. OS << Name;
  1389. // AtomicExpr stores its subexpressions in a permuted order.
  1390. PrintExpr(Node->getPtr());
  1391. if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
  1392. Node->getOp() != AtomicExpr::AO__atomic_load_n &&
  1393. Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
  1394. OS << ", ";
  1395. PrintExpr(Node->getVal1());
  1396. }
  1397. if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
  1398. Node->isCmpXChg()) {
  1399. OS << ", ";
  1400. PrintExpr(Node->getVal2());
  1401. }
  1402. if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
  1403. Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
  1404. OS << ", ";
  1405. PrintExpr(Node->getWeak());
  1406. }
  1407. if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
  1408. Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
  1409. OS << ", ";
  1410. PrintExpr(Node->getOrder());
  1411. }
  1412. if (Node->isCmpXChg()) {
  1413. OS << ", ";
  1414. PrintExpr(Node->getOrderFail());
  1415. }
  1416. OS << ")";
  1417. }
  1418. // C++
  1419. void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
  1420. OverloadedOperatorKind Kind = Node->getOperator();
  1421. if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
  1422. if (Node->getNumArgs() == 1) {
  1423. OS << getOperatorSpelling(Kind) << ' ';
  1424. PrintExpr(Node->getArg(0));
  1425. } else {
  1426. PrintExpr(Node->getArg(0));
  1427. OS << ' ' << getOperatorSpelling(Kind);
  1428. }
  1429. } else if (Kind == OO_Arrow) {
  1430. PrintExpr(Node->getArg(0));
  1431. } else if (Kind == OO_Call) {
  1432. PrintExpr(Node->getArg(0));
  1433. OS << '(';
  1434. for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
  1435. if (ArgIdx > 1)
  1436. OS << ", ";
  1437. if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
  1438. PrintExpr(Node->getArg(ArgIdx));
  1439. }
  1440. OS << ')';
  1441. } else if (Kind == OO_Subscript) {
  1442. PrintExpr(Node->getArg(0));
  1443. OS << '[';
  1444. PrintExpr(Node->getArg(1));
  1445. OS << ']';
  1446. } else if (Node->getNumArgs() == 1) {
  1447. OS << getOperatorSpelling(Kind) << ' ';
  1448. PrintExpr(Node->getArg(0));
  1449. } else if (Node->getNumArgs() == 2) {
  1450. PrintExpr(Node->getArg(0));
  1451. OS << ' ' << getOperatorSpelling(Kind) << ' ';
  1452. PrintExpr(Node->getArg(1));
  1453. } else {
  1454. llvm_unreachable("unknown overloaded operator");
  1455. }
  1456. }
  1457. void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
  1458. // If we have a conversion operator call only print the argument.
  1459. CXXMethodDecl *MD = Node->getMethodDecl();
  1460. if (MD && isa<CXXConversionDecl>(MD)) {
  1461. PrintExpr(Node->getImplicitObjectArgument());
  1462. return;
  1463. }
  1464. VisitCallExpr(cast<CallExpr>(Node));
  1465. }
  1466. void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
  1467. PrintExpr(Node->getCallee());
  1468. OS << "<<<";
  1469. PrintCallArgs(Node->getConfig());
  1470. OS << ">>>(";
  1471. PrintCallArgs(Node);
  1472. OS << ")";
  1473. }
  1474. void StmtPrinter::VisitCXXRewrittenBinaryOperator(
  1475. CXXRewrittenBinaryOperator *Node) {
  1476. CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
  1477. Node->getDecomposedForm();
  1478. PrintExpr(const_cast<Expr*>(Decomposed.LHS));
  1479. OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
  1480. PrintExpr(const_cast<Expr*>(Decomposed.RHS));
  1481. }
  1482. void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
  1483. OS << Node->getCastName() << '<';
  1484. Node->getTypeAsWritten().print(OS, Policy);
  1485. OS << ">(";
  1486. PrintExpr(Node->getSubExpr());
  1487. OS << ")";
  1488. }
  1489. void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
  1490. VisitCXXNamedCastExpr(Node);
  1491. }
  1492. void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
  1493. VisitCXXNamedCastExpr(Node);
  1494. }
  1495. void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
  1496. VisitCXXNamedCastExpr(Node);
  1497. }
  1498. void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
  1499. VisitCXXNamedCastExpr(Node);
  1500. }
  1501. void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
  1502. OS << "__builtin_bit_cast(";
  1503. Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
  1504. OS << ", ";
  1505. PrintExpr(Node->getSubExpr());
  1506. OS << ")";
  1507. }
  1508. void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
  1509. OS << "typeid(";
  1510. if (Node->isTypeOperand()) {
  1511. Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
  1512. } else {
  1513. PrintExpr(Node->getExprOperand());
  1514. }
  1515. OS << ")";
  1516. }
  1517. void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
  1518. OS << "__uuidof(";
  1519. if (Node->isTypeOperand()) {
  1520. Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
  1521. } else {
  1522. PrintExpr(Node->getExprOperand());
  1523. }
  1524. OS << ")";
  1525. }
  1526. void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
  1527. PrintExpr(Node->getBaseExpr());
  1528. if (Node->isArrow())
  1529. OS << "->";
  1530. else
  1531. OS << ".";
  1532. if (NestedNameSpecifier *Qualifier =
  1533. Node->getQualifierLoc().getNestedNameSpecifier())
  1534. Qualifier->print(OS, Policy);
  1535. OS << Node->getPropertyDecl()->getDeclName();
  1536. }
  1537. void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
  1538. PrintExpr(Node->getBase());
  1539. OS << "[";
  1540. PrintExpr(Node->getIdx());
  1541. OS << "]";
  1542. }
  1543. void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
  1544. switch (Node->getLiteralOperatorKind()) {
  1545. case UserDefinedLiteral::LOK_Raw:
  1546. OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
  1547. break;
  1548. case UserDefinedLiteral::LOK_Template: {
  1549. const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
  1550. const TemplateArgumentList *Args =
  1551. cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
  1552. assert(Args);
  1553. if (Args->size() != 1) {
  1554. OS << "operator\"\"" << Node->getUDSuffix()->getName();
  1555. printTemplateArgumentList(OS, Args->asArray(), Policy);
  1556. OS << "()";
  1557. return;
  1558. }
  1559. const TemplateArgument &Pack = Args->get(0);
  1560. for (const auto &P : Pack.pack_elements()) {
  1561. char C = (char)P.getAsIntegral().getZExtValue();
  1562. OS << C;
  1563. }
  1564. break;
  1565. }
  1566. case UserDefinedLiteral::LOK_Integer: {
  1567. // Print integer literal without suffix.
  1568. const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
  1569. OS << Int->getValue().toString(10, /*isSigned*/false);
  1570. break;
  1571. }
  1572. case UserDefinedLiteral::LOK_Floating: {
  1573. // Print floating literal without suffix.
  1574. auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
  1575. PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
  1576. break;
  1577. }
  1578. case UserDefinedLiteral::LOK_String:
  1579. case UserDefinedLiteral::LOK_Character:
  1580. PrintExpr(Node->getCookedLiteral());
  1581. break;
  1582. }
  1583. OS << Node->getUDSuffix()->getName();
  1584. }
  1585. void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
  1586. OS << (Node->getValue() ? "true" : "false");
  1587. }
  1588. void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
  1589. OS << "nullptr";
  1590. }
  1591. void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
  1592. OS << "this";
  1593. }
  1594. void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
  1595. if (!Node->getSubExpr())
  1596. OS << "throw";
  1597. else {
  1598. OS << "throw ";
  1599. PrintExpr(Node->getSubExpr());
  1600. }
  1601. }
  1602. void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
  1603. // Nothing to print: we picked up the default argument.
  1604. }
  1605. void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
  1606. // Nothing to print: we picked up the default initializer.
  1607. }
  1608. void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
  1609. Node->getType().print(OS, Policy);
  1610. // If there are no parens, this is list-initialization, and the braces are
  1611. // part of the syntax of the inner construct.
  1612. if (Node->getLParenLoc().isValid())
  1613. OS << "(";
  1614. PrintExpr(Node->getSubExpr());
  1615. if (Node->getLParenLoc().isValid())
  1616. OS << ")";
  1617. }
  1618. void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
  1619. PrintExpr(Node->getSubExpr());
  1620. }
  1621. void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
  1622. Node->getType().print(OS, Policy);
  1623. if (Node->isStdInitListInitialization())
  1624. /* Nothing to do; braces are part of creating the std::initializer_list. */;
  1625. else if (Node->isListInitialization())
  1626. OS << "{";
  1627. else
  1628. OS << "(";
  1629. for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
  1630. ArgEnd = Node->arg_end();
  1631. Arg != ArgEnd; ++Arg) {
  1632. if ((*Arg)->isDefaultArgument())
  1633. break;
  1634. if (Arg != Node->arg_begin())
  1635. OS << ", ";
  1636. PrintExpr(*Arg);
  1637. }
  1638. if (Node->isStdInitListInitialization())
  1639. /* See above. */;
  1640. else if (Node->isListInitialization())
  1641. OS << "}";
  1642. else
  1643. OS << ")";
  1644. }
  1645. void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
  1646. OS << '[';
  1647. bool NeedComma = false;
  1648. switch (Node->getCaptureDefault()) {
  1649. case LCD_None:
  1650. break;
  1651. case LCD_ByCopy:
  1652. OS << '=';
  1653. NeedComma = true;
  1654. break;
  1655. case LCD_ByRef:
  1656. OS << '&';
  1657. NeedComma = true;
  1658. break;
  1659. }
  1660. for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
  1661. CEnd = Node->explicit_capture_end();
  1662. C != CEnd;
  1663. ++C) {
  1664. if (C->capturesVLAType())
  1665. continue;
  1666. if (NeedComma)
  1667. OS << ", ";
  1668. NeedComma = true;
  1669. switch (C->getCaptureKind()) {
  1670. case LCK_This:
  1671. OS << "this";
  1672. break;
  1673. case LCK_StarThis:
  1674. OS << "*this";
  1675. break;
  1676. case LCK_ByRef:
  1677. if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
  1678. OS << '&';
  1679. OS << C->getCapturedVar()->getName();
  1680. break;
  1681. case LCK_ByCopy:
  1682. OS << C->getCapturedVar()->getName();
  1683. break;
  1684. case LCK_VLAType:
  1685. llvm_unreachable("VLA type in explicit captures.");
  1686. }
  1687. if (C->isPackExpansion())
  1688. OS << "...";
  1689. if (Node->isInitCapture(C))
  1690. PrintExpr(C->getCapturedVar()->getInit());
  1691. }
  1692. OS << ']';
  1693. if (!Node->getExplicitTemplateParameters().empty()) {
  1694. Node->getTemplateParameterList()->print(
  1695. OS, Node->getLambdaClass()->getASTContext(),
  1696. /*OmitTemplateKW*/true);
  1697. }
  1698. if (Node->hasExplicitParameters()) {
  1699. OS << '(';
  1700. CXXMethodDecl *Method = Node->getCallOperator();
  1701. NeedComma = false;
  1702. for (const auto *P : Method->parameters()) {
  1703. if (NeedComma) {
  1704. OS << ", ";
  1705. } else {
  1706. NeedComma = true;
  1707. }
  1708. std::string ParamStr = P->getNameAsString();
  1709. P->getOriginalType().print(OS, Policy, ParamStr);
  1710. }
  1711. if (Method->isVariadic()) {
  1712. if (NeedComma)
  1713. OS << ", ";
  1714. OS << "...";
  1715. }
  1716. OS << ')';
  1717. if (Node->isMutable())
  1718. OS << " mutable";
  1719. auto *Proto = Method->getType()->castAs<FunctionProtoType>();
  1720. Proto->printExceptionSpecification(OS, Policy);
  1721. // FIXME: Attributes
  1722. // Print the trailing return type if it was specified in the source.
  1723. if (Node->hasExplicitResultType()) {
  1724. OS << " -> ";
  1725. Proto->getReturnType().print(OS, Policy);
  1726. }
  1727. }
  1728. // Print the body.
  1729. OS << ' ';
  1730. if (Policy.TerseOutput)
  1731. OS << "{}";
  1732. else
  1733. PrintRawCompoundStmt(Node->getBody());
  1734. }
  1735. void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
  1736. if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
  1737. TSInfo->getType().print(OS, Policy);
  1738. else
  1739. Node->getType().print(OS, Policy);
  1740. OS << "()";
  1741. }
  1742. void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
  1743. if (E->isGlobalNew())
  1744. OS << "::";
  1745. OS << "new ";
  1746. unsigned NumPlace = E->getNumPlacementArgs();
  1747. if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
  1748. OS << "(";
  1749. PrintExpr(E->getPlacementArg(0));
  1750. for (unsigned i = 1; i < NumPlace; ++i) {
  1751. if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
  1752. break;
  1753. OS << ", ";
  1754. PrintExpr(E->getPlacementArg(i));
  1755. }
  1756. OS << ") ";
  1757. }
  1758. if (E->isParenTypeId())
  1759. OS << "(";
  1760. std::string TypeS;
  1761. if (Optional<Expr *> Size = E->getArraySize()) {
  1762. llvm::raw_string_ostream s(TypeS);
  1763. s << '[';
  1764. if (*Size)
  1765. (*Size)->printPretty(s, Helper, Policy);
  1766. s << ']';
  1767. }
  1768. E->getAllocatedType().print(OS, Policy, TypeS);
  1769. if (E->isParenTypeId())
  1770. OS << ")";
  1771. CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
  1772. if (InitStyle) {
  1773. if (InitStyle == CXXNewExpr::CallInit)
  1774. OS << "(";
  1775. PrintExpr(E->getInitializer());
  1776. if (InitStyle == CXXNewExpr::CallInit)
  1777. OS << ")";
  1778. }
  1779. }
  1780. void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
  1781. if (E->isGlobalDelete())
  1782. OS << "::";
  1783. OS << "delete ";
  1784. if (E->isArrayForm())
  1785. OS << "[] ";
  1786. PrintExpr(E->getArgument());
  1787. }
  1788. void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
  1789. PrintExpr(E->getBase());
  1790. if (E->isArrow())
  1791. OS << "->";
  1792. else
  1793. OS << '.';
  1794. if (E->getQualifier())
  1795. E->getQualifier()->print(OS, Policy);
  1796. OS << "~";
  1797. if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
  1798. OS << II->getName();
  1799. else
  1800. E->getDestroyedType().print(OS, Policy);
  1801. }
  1802. void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
  1803. if (E->isListInitialization() && !E->isStdInitListInitialization())
  1804. OS << "{";
  1805. for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
  1806. if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
  1807. // Don't print any defaulted arguments
  1808. break;
  1809. }
  1810. if (i) OS << ", ";
  1811. PrintExpr(E->getArg(i));
  1812. }
  1813. if (E->isListInitialization() && !E->isStdInitListInitialization())
  1814. OS << "}";
  1815. }
  1816. void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
  1817. // Parens are printed by the surrounding context.
  1818. OS << "<forwarded>";
  1819. }
  1820. void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
  1821. PrintExpr(E->getSubExpr());
  1822. }
  1823. void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
  1824. // Just forward to the subexpression.
  1825. PrintExpr(E->getSubExpr());
  1826. }
  1827. void
  1828. StmtPrinter::VisitCXXUnresolvedConstructExpr(
  1829. CXXUnresolvedConstructExpr *Node) {
  1830. Node->getTypeAsWritten().print(OS, Policy);
  1831. OS << "(";
  1832. for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
  1833. ArgEnd = Node->arg_end();
  1834. Arg != ArgEnd; ++Arg) {
  1835. if (Arg != Node->arg_begin())
  1836. OS << ", ";
  1837. PrintExpr(*Arg);
  1838. }
  1839. OS << ")";
  1840. }
  1841. void StmtPrinter::VisitCXXDependentScopeMemberExpr(
  1842. CXXDependentScopeMemberExpr *Node) {
  1843. if (!Node->isImplicitAccess()) {
  1844. PrintExpr(Node->getBase());
  1845. OS << (Node->isArrow() ? "->" : ".");
  1846. }
  1847. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1848. Qualifier->print(OS, Policy);
  1849. if (Node->hasTemplateKeyword())
  1850. OS << "template ";
  1851. OS << Node->getMemberNameInfo();
  1852. if (Node->hasExplicitTemplateArgs())
  1853. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  1854. }
  1855. void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
  1856. if (!Node->isImplicitAccess()) {
  1857. PrintExpr(Node->getBase());
  1858. OS << (Node->isArrow() ? "->" : ".");
  1859. }
  1860. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1861. Qualifier->print(OS, Policy);
  1862. if (Node->hasTemplateKeyword())
  1863. OS << "template ";
  1864. OS << Node->getMemberNameInfo();
  1865. if (Node->hasExplicitTemplateArgs())
  1866. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  1867. }
  1868. static const char *getTypeTraitName(TypeTrait TT) {
  1869. switch (TT) {
  1870. #define TYPE_TRAIT_1(Spelling, Name, Key) \
  1871. case clang::UTT_##Name: return #Spelling;
  1872. #define TYPE_TRAIT_2(Spelling, Name, Key) \
  1873. case clang::BTT_##Name: return #Spelling;
  1874. #define TYPE_TRAIT_N(Spelling, Name, Key) \
  1875. case clang::TT_##Name: return #Spelling;
  1876. #include "clang/Basic/TokenKinds.def"
  1877. }
  1878. llvm_unreachable("Type trait not covered by switch");
  1879. }
  1880. static const char *getTypeTraitName(ArrayTypeTrait ATT) {
  1881. switch (ATT) {
  1882. case ATT_ArrayRank: return "__array_rank";
  1883. case ATT_ArrayExtent: return "__array_extent";
  1884. }
  1885. llvm_unreachable("Array type trait not covered by switch");
  1886. }
  1887. static const char *getExpressionTraitName(ExpressionTrait ET) {
  1888. switch (ET) {
  1889. case ET_IsLValueExpr: return "__is_lvalue_expr";
  1890. case ET_IsRValueExpr: return "__is_rvalue_expr";
  1891. }
  1892. llvm_unreachable("Expression type trait not covered by switch");
  1893. }
  1894. void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
  1895. OS << getTypeTraitName(E->getTrait()) << "(";
  1896. for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
  1897. if (I > 0)
  1898. OS << ", ";
  1899. E->getArg(I)->getType().print(OS, Policy);
  1900. }
  1901. OS << ")";
  1902. }
  1903. void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
  1904. OS << getTypeTraitName(E->getTrait()) << '(';
  1905. E->getQueriedType().print(OS, Policy);
  1906. OS << ')';
  1907. }
  1908. void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
  1909. OS << getExpressionTraitName(E->getTrait()) << '(';
  1910. PrintExpr(E->getQueriedExpression());
  1911. OS << ')';
  1912. }
  1913. void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
  1914. OS << "noexcept(";
  1915. PrintExpr(E->getOperand());
  1916. OS << ")";
  1917. }
  1918. void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
  1919. PrintExpr(E->getPattern());
  1920. OS << "...";
  1921. }
  1922. void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
  1923. OS << "sizeof...(" << *E->getPack() << ")";
  1924. }
  1925. void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
  1926. SubstNonTypeTemplateParmPackExpr *Node) {
  1927. OS << *Node->getParameterPack();
  1928. }
  1929. void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
  1930. SubstNonTypeTemplateParmExpr *Node) {
  1931. Visit(Node->getReplacement());
  1932. }
  1933. void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
  1934. OS << *E->getParameterPack();
  1935. }
  1936. void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
  1937. PrintExpr(Node->GetTemporaryExpr());
  1938. }
  1939. void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
  1940. OS << "(";
  1941. if (E->getLHS()) {
  1942. PrintExpr(E->getLHS());
  1943. OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
  1944. }
  1945. OS << "...";
  1946. if (E->getRHS()) {
  1947. OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
  1948. PrintExpr(E->getRHS());
  1949. }
  1950. OS << ")";
  1951. }
  1952. void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
  1953. NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
  1954. if (NNS)
  1955. NNS.getNestedNameSpecifier()->print(OS, Policy);
  1956. if (E->getTemplateKWLoc().isValid())
  1957. OS << "template ";
  1958. OS << E->getFoundDecl()->getName();
  1959. printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
  1960. Policy);
  1961. }
  1962. // C++ Coroutines TS
  1963. void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
  1964. Visit(S->getBody());
  1965. }
  1966. void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
  1967. OS << "co_return";
  1968. if (S->getOperand()) {
  1969. OS << " ";
  1970. Visit(S->getOperand());
  1971. }
  1972. OS << ";";
  1973. }
  1974. void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
  1975. OS << "co_await ";
  1976. PrintExpr(S->getOperand());
  1977. }
  1978. void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
  1979. OS << "co_await ";
  1980. PrintExpr(S->getOperand());
  1981. }
  1982. void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
  1983. OS << "co_yield ";
  1984. PrintExpr(S->getOperand());
  1985. }
  1986. // Obj-C
  1987. void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
  1988. OS << "@";
  1989. VisitStringLiteral(Node->getString());
  1990. }
  1991. void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
  1992. OS << "@";
  1993. Visit(E->getSubExpr());
  1994. }
  1995. void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
  1996. OS << "@[ ";
  1997. ObjCArrayLiteral::child_range Ch = E->children();
  1998. for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
  1999. if (I != Ch.begin())
  2000. OS << ", ";
  2001. Visit(*I);
  2002. }
  2003. OS << " ]";
  2004. }
  2005. void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
  2006. OS << "@{ ";
  2007. for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
  2008. if (I > 0)
  2009. OS << ", ";
  2010. ObjCDictionaryElement Element = E->getKeyValueElement(I);
  2011. Visit(Element.Key);
  2012. OS << " : ";
  2013. Visit(Element.Value);
  2014. if (Element.isPackExpansion())
  2015. OS << "...";
  2016. }
  2017. OS << " }";
  2018. }
  2019. void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
  2020. OS << "@encode(";
  2021. Node->getEncodedType().print(OS, Policy);
  2022. OS << ')';
  2023. }
  2024. void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
  2025. OS << "@selector(";
  2026. Node->getSelector().print(OS);
  2027. OS << ')';
  2028. }
  2029. void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
  2030. OS << "@protocol(" << *Node->getProtocol() << ')';
  2031. }
  2032. void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
  2033. OS << "[";
  2034. switch (Mess->getReceiverKind()) {
  2035. case ObjCMessageExpr::Instance:
  2036. PrintExpr(Mess->getInstanceReceiver());
  2037. break;
  2038. case ObjCMessageExpr::Class:
  2039. Mess->getClassReceiver().print(OS, Policy);
  2040. break;
  2041. case ObjCMessageExpr::SuperInstance:
  2042. case ObjCMessageExpr::SuperClass:
  2043. OS << "Super";
  2044. break;
  2045. }
  2046. OS << ' ';
  2047. Selector selector = Mess->getSelector();
  2048. if (selector.isUnarySelector()) {
  2049. OS << selector.getNameForSlot(0);
  2050. } else {
  2051. for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
  2052. if (i < selector.getNumArgs()) {
  2053. if (i > 0) OS << ' ';
  2054. if (selector.getIdentifierInfoForSlot(i))
  2055. OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
  2056. else
  2057. OS << ":";
  2058. }
  2059. else OS << ", "; // Handle variadic methods.
  2060. PrintExpr(Mess->getArg(i));
  2061. }
  2062. }
  2063. OS << "]";
  2064. }
  2065. void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
  2066. OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
  2067. }
  2068. void
  2069. StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
  2070. PrintExpr(E->getSubExpr());
  2071. }
  2072. void
  2073. StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
  2074. OS << '(' << E->getBridgeKindName();
  2075. E->getType().print(OS, Policy);
  2076. OS << ')';
  2077. PrintExpr(E->getSubExpr());
  2078. }
  2079. void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
  2080. BlockDecl *BD = Node->getBlockDecl();
  2081. OS << "^";
  2082. const FunctionType *AFT = Node->getFunctionType();
  2083. if (isa<FunctionNoProtoType>(AFT)) {
  2084. OS << "()";
  2085. } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
  2086. OS << '(';
  2087. for (BlockDecl::param_iterator AI = BD->param_begin(),
  2088. E = BD->param_end(); AI != E; ++AI) {
  2089. if (AI != BD->param_begin()) OS << ", ";
  2090. std::string ParamStr = (*AI)->getNameAsString();
  2091. (*AI)->getType().print(OS, Policy, ParamStr);
  2092. }
  2093. const auto *FT = cast<FunctionProtoType>(AFT);
  2094. if (FT->isVariadic()) {
  2095. if (!BD->param_empty()) OS << ", ";
  2096. OS << "...";
  2097. }
  2098. OS << ')';
  2099. }
  2100. OS << "{ }";
  2101. }
  2102. void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
  2103. PrintExpr(Node->getSourceExpr());
  2104. }
  2105. void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
  2106. // TODO: Print something reasonable for a TypoExpr, if necessary.
  2107. llvm_unreachable("Cannot print TypoExpr nodes");
  2108. }
  2109. void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
  2110. OS << "__builtin_astype(";
  2111. PrintExpr(Node->getSrcExpr());
  2112. OS << ", ";
  2113. Node->getType().print(OS, Policy);
  2114. OS << ")";
  2115. }
  2116. //===----------------------------------------------------------------------===//
  2117. // Stmt method implementations
  2118. //===----------------------------------------------------------------------===//
  2119. void Stmt::dumpPretty(const ASTContext &Context) const {
  2120. printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
  2121. }
  2122. void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
  2123. const PrintingPolicy &Policy, unsigned Indentation,
  2124. StringRef NL, const ASTContext *Context) const {
  2125. StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
  2126. P.Visit(const_cast<Stmt *>(this));
  2127. }
  2128. void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
  2129. const PrintingPolicy &Policy, bool AddQuotes) const {
  2130. std::string Buf;
  2131. llvm::raw_string_ostream TempOut(Buf);
  2132. printPretty(TempOut, Helper, Policy);
  2133. Out << JsonFormat(TempOut.str(), AddQuotes);
  2134. }
  2135. //===----------------------------------------------------------------------===//
  2136. // PrinterHelper
  2137. //===----------------------------------------------------------------------===//
  2138. // Implement virtual destructor.
  2139. PrinterHelper::~PrinterHelper() = default;