StmtPrinter.cpp 67 KB

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