StmtPrinter.cpp 73 KB

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