StmtPrinter.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  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::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
  706. Indent() << "#pragma omp distribute";
  707. PrintOMPExecutableDirective(Node);
  708. }
  709. void StmtPrinter::VisitOMPTargetUpdateDirective(
  710. OMPTargetUpdateDirective *Node) {
  711. Indent() << "#pragma omp target update";
  712. PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
  713. }
  714. void StmtPrinter::VisitOMPDistributeParallelForDirective(
  715. OMPDistributeParallelForDirective *Node) {
  716. Indent() << "#pragma omp distribute parallel for";
  717. PrintOMPExecutableDirective(Node);
  718. }
  719. void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
  720. OMPDistributeParallelForSimdDirective *Node) {
  721. Indent() << "#pragma omp distribute parallel for simd";
  722. PrintOMPExecutableDirective(Node);
  723. }
  724. void StmtPrinter::VisitOMPDistributeSimdDirective(
  725. OMPDistributeSimdDirective *Node) {
  726. Indent() << "#pragma omp distribute simd";
  727. PrintOMPExecutableDirective(Node);
  728. }
  729. void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
  730. OMPTargetParallelForSimdDirective *Node) {
  731. Indent() << "#pragma omp target parallel for simd";
  732. PrintOMPExecutableDirective(Node);
  733. }
  734. void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
  735. Indent() << "#pragma omp target simd";
  736. PrintOMPExecutableDirective(Node);
  737. }
  738. void StmtPrinter::VisitOMPTeamsDistributeDirective(
  739. OMPTeamsDistributeDirective *Node) {
  740. Indent() << "#pragma omp teams distribute";
  741. PrintOMPExecutableDirective(Node);
  742. }
  743. void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
  744. OMPTeamsDistributeSimdDirective *Node) {
  745. Indent() << "#pragma omp teams distribute simd";
  746. PrintOMPExecutableDirective(Node);
  747. }
  748. void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
  749. OMPTeamsDistributeParallelForSimdDirective *Node) {
  750. Indent() << "#pragma omp teams distribute parallel for simd";
  751. PrintOMPExecutableDirective(Node);
  752. }
  753. void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
  754. OMPTeamsDistributeParallelForDirective *Node) {
  755. Indent() << "#pragma omp teams distribute parallel for";
  756. PrintOMPExecutableDirective(Node);
  757. }
  758. void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
  759. Indent() << "#pragma omp target teams";
  760. PrintOMPExecutableDirective(Node);
  761. }
  762. void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
  763. OMPTargetTeamsDistributeDirective *Node) {
  764. Indent() << "#pragma omp target teams distribute";
  765. PrintOMPExecutableDirective(Node);
  766. }
  767. void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
  768. OMPTargetTeamsDistributeParallelForDirective *Node) {
  769. Indent() << "#pragma omp target teams distribute parallel for";
  770. PrintOMPExecutableDirective(Node);
  771. }
  772. void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
  773. OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
  774. Indent() << "#pragma omp target teams distribute parallel for simd";
  775. PrintOMPExecutableDirective(Node);
  776. }
  777. void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
  778. OMPTargetTeamsDistributeSimdDirective *Node) {
  779. Indent() << "#pragma omp target teams distribute simd";
  780. PrintOMPExecutableDirective(Node);
  781. }
  782. //===----------------------------------------------------------------------===//
  783. // Expr printing methods.
  784. //===----------------------------------------------------------------------===//
  785. void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
  786. OS << Node->getBuiltinStr() << "()";
  787. }
  788. void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
  789. PrintExpr(Node->getSubExpr());
  790. }
  791. void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
  792. if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
  793. OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
  794. return;
  795. }
  796. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  797. Qualifier->print(OS, Policy);
  798. if (Node->hasTemplateKeyword())
  799. OS << "template ";
  800. OS << Node->getNameInfo();
  801. if (Node->hasExplicitTemplateArgs())
  802. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  803. }
  804. void StmtPrinter::VisitDependentScopeDeclRefExpr(
  805. DependentScopeDeclRefExpr *Node) {
  806. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  807. Qualifier->print(OS, Policy);
  808. if (Node->hasTemplateKeyword())
  809. OS << "template ";
  810. OS << Node->getNameInfo();
  811. if (Node->hasExplicitTemplateArgs())
  812. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  813. }
  814. void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
  815. if (Node->getQualifier())
  816. Node->getQualifier()->print(OS, Policy);
  817. if (Node->hasTemplateKeyword())
  818. OS << "template ";
  819. OS << Node->getNameInfo();
  820. if (Node->hasExplicitTemplateArgs())
  821. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  822. }
  823. static bool isImplicitSelf(const Expr *E) {
  824. if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
  825. if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
  826. if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
  827. DRE->getBeginLoc().isInvalid())
  828. return true;
  829. }
  830. }
  831. return false;
  832. }
  833. void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
  834. if (Node->getBase()) {
  835. if (!Policy.SuppressImplicitBase ||
  836. !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
  837. PrintExpr(Node->getBase());
  838. OS << (Node->isArrow() ? "->" : ".");
  839. }
  840. }
  841. OS << *Node->getDecl();
  842. }
  843. void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
  844. if (Node->isSuperReceiver())
  845. OS << "super.";
  846. else if (Node->isObjectReceiver() && Node->getBase()) {
  847. PrintExpr(Node->getBase());
  848. OS << ".";
  849. } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
  850. OS << Node->getClassReceiver()->getName() << ".";
  851. }
  852. if (Node->isImplicitProperty()) {
  853. if (const auto *Getter = Node->getImplicitPropertyGetter())
  854. Getter->getSelector().print(OS);
  855. else
  856. OS << SelectorTable::getPropertyNameFromSetterSelector(
  857. Node->getImplicitPropertySetter()->getSelector());
  858. } else
  859. OS << Node->getExplicitProperty()->getName();
  860. }
  861. void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
  862. PrintExpr(Node->getBaseExpr());
  863. OS << "[";
  864. PrintExpr(Node->getKeyExpr());
  865. OS << "]";
  866. }
  867. void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
  868. OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
  869. }
  870. void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
  871. unsigned value = Node->getValue();
  872. switch (Node->getKind()) {
  873. case CharacterLiteral::Ascii: break; // no prefix.
  874. case CharacterLiteral::Wide: OS << 'L'; break;
  875. case CharacterLiteral::UTF8: OS << "u8"; break;
  876. case CharacterLiteral::UTF16: OS << 'u'; break;
  877. case CharacterLiteral::UTF32: OS << 'U'; break;
  878. }
  879. switch (value) {
  880. case '\\':
  881. OS << "'\\\\'";
  882. break;
  883. case '\'':
  884. OS << "'\\''";
  885. break;
  886. case '\a':
  887. // TODO: K&R: the meaning of '\\a' is different in traditional C
  888. OS << "'\\a'";
  889. break;
  890. case '\b':
  891. OS << "'\\b'";
  892. break;
  893. // Nonstandard escape sequence.
  894. /*case '\e':
  895. OS << "'\\e'";
  896. break;*/
  897. case '\f':
  898. OS << "'\\f'";
  899. break;
  900. case '\n':
  901. OS << "'\\n'";
  902. break;
  903. case '\r':
  904. OS << "'\\r'";
  905. break;
  906. case '\t':
  907. OS << "'\\t'";
  908. break;
  909. case '\v':
  910. OS << "'\\v'";
  911. break;
  912. default:
  913. // A character literal might be sign-extended, which
  914. // would result in an invalid \U escape sequence.
  915. // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
  916. // are not correctly handled.
  917. if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
  918. value &= 0xFFu;
  919. if (value < 256 && isPrintable((unsigned char)value))
  920. OS << "'" << (char)value << "'";
  921. else if (value < 256)
  922. OS << "'\\x" << llvm::format("%02x", value) << "'";
  923. else if (value <= 0xFFFF)
  924. OS << "'\\u" << llvm::format("%04x", value) << "'";
  925. else
  926. OS << "'\\U" << llvm::format("%08x", value) << "'";
  927. }
  928. }
  929. /// Prints the given expression using the original source text. Returns true on
  930. /// success, false otherwise.
  931. static bool printExprAsWritten(raw_ostream &OS, Expr *E,
  932. const ASTContext *Context) {
  933. if (!Context)
  934. return false;
  935. bool Invalid = false;
  936. StringRef Source = Lexer::getSourceText(
  937. CharSourceRange::getTokenRange(E->getSourceRange()),
  938. Context->getSourceManager(), Context->getLangOpts(), &Invalid);
  939. if (!Invalid) {
  940. OS << Source;
  941. return true;
  942. }
  943. return false;
  944. }
  945. void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
  946. if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
  947. return;
  948. bool isSigned = Node->getType()->isSignedIntegerType();
  949. OS << Node->getValue().toString(10, isSigned);
  950. // Emit suffixes. Integer literals are always a builtin integer type.
  951. switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
  952. default: llvm_unreachable("Unexpected type for integer literal!");
  953. case BuiltinType::Char_S:
  954. case BuiltinType::Char_U: OS << "i8"; break;
  955. case BuiltinType::UChar: OS << "Ui8"; break;
  956. case BuiltinType::Short: OS << "i16"; break;
  957. case BuiltinType::UShort: OS << "Ui16"; break;
  958. case BuiltinType::Int: break; // no suffix.
  959. case BuiltinType::UInt: OS << 'U'; break;
  960. case BuiltinType::Long: OS << 'L'; break;
  961. case BuiltinType::ULong: OS << "UL"; break;
  962. case BuiltinType::LongLong: OS << "LL"; break;
  963. case BuiltinType::ULongLong: OS << "ULL"; break;
  964. }
  965. }
  966. void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
  967. if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
  968. return;
  969. OS << Node->getValueAsString(/*Radix=*/10);
  970. switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
  971. default: llvm_unreachable("Unexpected type for fixed point literal!");
  972. case BuiltinType::ShortFract: OS << "hr"; break;
  973. case BuiltinType::ShortAccum: OS << "hk"; break;
  974. case BuiltinType::UShortFract: OS << "uhr"; break;
  975. case BuiltinType::UShortAccum: OS << "uhk"; break;
  976. case BuiltinType::Fract: OS << "r"; break;
  977. case BuiltinType::Accum: OS << "k"; break;
  978. case BuiltinType::UFract: OS << "ur"; break;
  979. case BuiltinType::UAccum: OS << "uk"; break;
  980. case BuiltinType::LongFract: OS << "lr"; break;
  981. case BuiltinType::LongAccum: OS << "lk"; break;
  982. case BuiltinType::ULongFract: OS << "ulr"; break;
  983. case BuiltinType::ULongAccum: OS << "ulk"; break;
  984. }
  985. }
  986. static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
  987. bool PrintSuffix) {
  988. SmallString<16> Str;
  989. Node->getValue().toString(Str);
  990. OS << Str;
  991. if (Str.find_first_not_of("-0123456789") == StringRef::npos)
  992. OS << '.'; // Trailing dot in order to separate from ints.
  993. if (!PrintSuffix)
  994. return;
  995. // Emit suffixes. Float literals are always a builtin float type.
  996. switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
  997. default: llvm_unreachable("Unexpected type for float literal!");
  998. case BuiltinType::Half: break; // FIXME: suffix?
  999. case BuiltinType::Double: break; // no suffix.
  1000. case BuiltinType::Float16: OS << "F16"; break;
  1001. case BuiltinType::Float: OS << 'F'; break;
  1002. case BuiltinType::LongDouble: OS << 'L'; break;
  1003. case BuiltinType::Float128: OS << 'Q'; break;
  1004. }
  1005. }
  1006. void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
  1007. if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
  1008. return;
  1009. PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
  1010. }
  1011. void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
  1012. PrintExpr(Node->getSubExpr());
  1013. OS << "i";
  1014. }
  1015. void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
  1016. Str->outputString(OS);
  1017. }
  1018. void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
  1019. OS << "(";
  1020. PrintExpr(Node->getSubExpr());
  1021. OS << ")";
  1022. }
  1023. void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
  1024. if (!Node->isPostfix()) {
  1025. OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
  1026. // Print a space if this is an "identifier operator" like __real, or if
  1027. // it might be concatenated incorrectly like '+'.
  1028. switch (Node->getOpcode()) {
  1029. default: break;
  1030. case UO_Real:
  1031. case UO_Imag:
  1032. case UO_Extension:
  1033. OS << ' ';
  1034. break;
  1035. case UO_Plus:
  1036. case UO_Minus:
  1037. if (isa<UnaryOperator>(Node->getSubExpr()))
  1038. OS << ' ';
  1039. break;
  1040. }
  1041. }
  1042. PrintExpr(Node->getSubExpr());
  1043. if (Node->isPostfix())
  1044. OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
  1045. }
  1046. void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
  1047. OS << "__builtin_offsetof(";
  1048. Node->getTypeSourceInfo()->getType().print(OS, Policy);
  1049. OS << ", ";
  1050. bool PrintedSomething = false;
  1051. for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
  1052. OffsetOfNode ON = Node->getComponent(i);
  1053. if (ON.getKind() == OffsetOfNode::Array) {
  1054. // Array node
  1055. OS << "[";
  1056. PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
  1057. OS << "]";
  1058. PrintedSomething = true;
  1059. continue;
  1060. }
  1061. // Skip implicit base indirections.
  1062. if (ON.getKind() == OffsetOfNode::Base)
  1063. continue;
  1064. // Field or identifier node.
  1065. IdentifierInfo *Id = ON.getFieldName();
  1066. if (!Id)
  1067. continue;
  1068. if (PrintedSomething)
  1069. OS << ".";
  1070. else
  1071. PrintedSomething = true;
  1072. OS << Id->getName();
  1073. }
  1074. OS << ")";
  1075. }
  1076. void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
  1077. switch(Node->getKind()) {
  1078. case UETT_SizeOf:
  1079. OS << "sizeof";
  1080. break;
  1081. case UETT_AlignOf:
  1082. if (Policy.Alignof)
  1083. OS << "alignof";
  1084. else if (Policy.UnderscoreAlignof)
  1085. OS << "_Alignof";
  1086. else
  1087. OS << "__alignof";
  1088. break;
  1089. case UETT_PreferredAlignOf:
  1090. OS << "__alignof";
  1091. break;
  1092. case UETT_VecStep:
  1093. OS << "vec_step";
  1094. break;
  1095. case UETT_OpenMPRequiredSimdAlign:
  1096. OS << "__builtin_omp_required_simd_align";
  1097. break;
  1098. }
  1099. if (Node->isArgumentType()) {
  1100. OS << '(';
  1101. Node->getArgumentType().print(OS, Policy);
  1102. OS << ')';
  1103. } else {
  1104. OS << " ";
  1105. PrintExpr(Node->getArgumentExpr());
  1106. }
  1107. }
  1108. void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
  1109. OS << "_Generic(";
  1110. PrintExpr(Node->getControllingExpr());
  1111. for (const GenericSelectionExpr::Association &Assoc : Node->associations()) {
  1112. OS << ", ";
  1113. QualType T = Assoc.getType();
  1114. if (T.isNull())
  1115. OS << "default";
  1116. else
  1117. T.print(OS, Policy);
  1118. OS << ": ";
  1119. PrintExpr(Assoc.getAssociationExpr());
  1120. }
  1121. OS << ")";
  1122. }
  1123. void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
  1124. PrintExpr(Node->getLHS());
  1125. OS << "[";
  1126. PrintExpr(Node->getRHS());
  1127. OS << "]";
  1128. }
  1129. void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
  1130. PrintExpr(Node->getBase());
  1131. OS << "[";
  1132. if (Node->getLowerBound())
  1133. PrintExpr(Node->getLowerBound());
  1134. if (Node->getColonLoc().isValid()) {
  1135. OS << ":";
  1136. if (Node->getLength())
  1137. PrintExpr(Node->getLength());
  1138. }
  1139. OS << "]";
  1140. }
  1141. void StmtPrinter::PrintCallArgs(CallExpr *Call) {
  1142. for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
  1143. if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
  1144. // Don't print any defaulted arguments
  1145. break;
  1146. }
  1147. if (i) OS << ", ";
  1148. PrintExpr(Call->getArg(i));
  1149. }
  1150. }
  1151. void StmtPrinter::VisitCallExpr(CallExpr *Call) {
  1152. PrintExpr(Call->getCallee());
  1153. OS << "(";
  1154. PrintCallArgs(Call);
  1155. OS << ")";
  1156. }
  1157. static bool isImplicitThis(const Expr *E) {
  1158. if (const auto *TE = dyn_cast<CXXThisExpr>(E))
  1159. return TE->isImplicit();
  1160. return false;
  1161. }
  1162. void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
  1163. if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
  1164. PrintExpr(Node->getBase());
  1165. auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
  1166. FieldDecl *ParentDecl =
  1167. ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
  1168. : nullptr;
  1169. if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
  1170. OS << (Node->isArrow() ? "->" : ".");
  1171. }
  1172. if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
  1173. if (FD->isAnonymousStructOrUnion())
  1174. return;
  1175. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1176. Qualifier->print(OS, Policy);
  1177. if (Node->hasTemplateKeyword())
  1178. OS << "template ";
  1179. OS << Node->getMemberNameInfo();
  1180. if (Node->hasExplicitTemplateArgs())
  1181. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  1182. }
  1183. void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
  1184. PrintExpr(Node->getBase());
  1185. OS << (Node->isArrow() ? "->isa" : ".isa");
  1186. }
  1187. void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
  1188. PrintExpr(Node->getBase());
  1189. OS << ".";
  1190. OS << Node->getAccessor().getName();
  1191. }
  1192. void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
  1193. OS << '(';
  1194. Node->getTypeAsWritten().print(OS, Policy);
  1195. OS << ')';
  1196. PrintExpr(Node->getSubExpr());
  1197. }
  1198. void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
  1199. OS << '(';
  1200. Node->getType().print(OS, Policy);
  1201. OS << ')';
  1202. PrintExpr(Node->getInitializer());
  1203. }
  1204. void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
  1205. // No need to print anything, simply forward to the subexpression.
  1206. PrintExpr(Node->getSubExpr());
  1207. }
  1208. void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
  1209. PrintExpr(Node->getLHS());
  1210. OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
  1211. PrintExpr(Node->getRHS());
  1212. }
  1213. void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
  1214. PrintExpr(Node->getLHS());
  1215. OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
  1216. PrintExpr(Node->getRHS());
  1217. }
  1218. void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
  1219. PrintExpr(Node->getCond());
  1220. OS << " ? ";
  1221. PrintExpr(Node->getLHS());
  1222. OS << " : ";
  1223. PrintExpr(Node->getRHS());
  1224. }
  1225. // GNU extensions.
  1226. void
  1227. StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
  1228. PrintExpr(Node->getCommon());
  1229. OS << " ?: ";
  1230. PrintExpr(Node->getFalseExpr());
  1231. }
  1232. void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
  1233. OS << "&&" << Node->getLabel()->getName();
  1234. }
  1235. void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
  1236. OS << "(";
  1237. PrintRawCompoundStmt(E->getSubStmt());
  1238. OS << ")";
  1239. }
  1240. void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
  1241. OS << "__builtin_choose_expr(";
  1242. PrintExpr(Node->getCond());
  1243. OS << ", ";
  1244. PrintExpr(Node->getLHS());
  1245. OS << ", ";
  1246. PrintExpr(Node->getRHS());
  1247. OS << ")";
  1248. }
  1249. void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
  1250. OS << "__null";
  1251. }
  1252. void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
  1253. OS << "__builtin_shufflevector(";
  1254. for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
  1255. if (i) OS << ", ";
  1256. PrintExpr(Node->getExpr(i));
  1257. }
  1258. OS << ")";
  1259. }
  1260. void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
  1261. OS << "__builtin_convertvector(";
  1262. PrintExpr(Node->getSrcExpr());
  1263. OS << ", ";
  1264. Node->getType().print(OS, Policy);
  1265. OS << ")";
  1266. }
  1267. void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
  1268. if (Node->getSyntacticForm()) {
  1269. Visit(Node->getSyntacticForm());
  1270. return;
  1271. }
  1272. OS << "{";
  1273. for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
  1274. if (i) OS << ", ";
  1275. if (Node->getInit(i))
  1276. PrintExpr(Node->getInit(i));
  1277. else
  1278. OS << "{}";
  1279. }
  1280. OS << "}";
  1281. }
  1282. void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
  1283. // There's no way to express this expression in any of our supported
  1284. // languages, so just emit something terse and (hopefully) clear.
  1285. OS << "{";
  1286. PrintExpr(Node->getSubExpr());
  1287. OS << "}";
  1288. }
  1289. void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
  1290. OS << "*";
  1291. }
  1292. void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
  1293. OS << "(";
  1294. for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
  1295. if (i) OS << ", ";
  1296. PrintExpr(Node->getExpr(i));
  1297. }
  1298. OS << ")";
  1299. }
  1300. void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
  1301. bool NeedsEquals = true;
  1302. for (const DesignatedInitExpr::Designator &D : Node->designators()) {
  1303. if (D.isFieldDesignator()) {
  1304. if (D.getDotLoc().isInvalid()) {
  1305. if (IdentifierInfo *II = D.getFieldName()) {
  1306. OS << II->getName() << ":";
  1307. NeedsEquals = false;
  1308. }
  1309. } else {
  1310. OS << "." << D.getFieldName()->getName();
  1311. }
  1312. } else {
  1313. OS << "[";
  1314. if (D.isArrayDesignator()) {
  1315. PrintExpr(Node->getArrayIndex(D));
  1316. } else {
  1317. PrintExpr(Node->getArrayRangeStart(D));
  1318. OS << " ... ";
  1319. PrintExpr(Node->getArrayRangeEnd(D));
  1320. }
  1321. OS << "]";
  1322. }
  1323. }
  1324. if (NeedsEquals)
  1325. OS << " = ";
  1326. else
  1327. OS << " ";
  1328. PrintExpr(Node->getInit());
  1329. }
  1330. void StmtPrinter::VisitDesignatedInitUpdateExpr(
  1331. DesignatedInitUpdateExpr *Node) {
  1332. OS << "{";
  1333. OS << "/*base*/";
  1334. PrintExpr(Node->getBase());
  1335. OS << ", ";
  1336. OS << "/*updater*/";
  1337. PrintExpr(Node->getUpdater());
  1338. OS << "}";
  1339. }
  1340. void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
  1341. OS << "/*no init*/";
  1342. }
  1343. void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
  1344. if (Node->getType()->getAsCXXRecordDecl()) {
  1345. OS << "/*implicit*/";
  1346. Node->getType().print(OS, Policy);
  1347. OS << "()";
  1348. } else {
  1349. OS << "/*implicit*/(";
  1350. Node->getType().print(OS, Policy);
  1351. OS << ')';
  1352. if (Node->getType()->isRecordType())
  1353. OS << "{}";
  1354. else
  1355. OS << 0;
  1356. }
  1357. }
  1358. void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
  1359. OS << "__builtin_va_arg(";
  1360. PrintExpr(Node->getSubExpr());
  1361. OS << ", ";
  1362. Node->getType().print(OS, Policy);
  1363. OS << ")";
  1364. }
  1365. void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
  1366. PrintExpr(Node->getSyntacticForm());
  1367. }
  1368. void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
  1369. const char *Name = nullptr;
  1370. switch (Node->getOp()) {
  1371. #define BUILTIN(ID, TYPE, ATTRS)
  1372. #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
  1373. case AtomicExpr::AO ## ID: \
  1374. Name = #ID "("; \
  1375. break;
  1376. #include "clang/Basic/Builtins.def"
  1377. }
  1378. OS << Name;
  1379. // AtomicExpr stores its subexpressions in a permuted order.
  1380. PrintExpr(Node->getPtr());
  1381. if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
  1382. Node->getOp() != AtomicExpr::AO__atomic_load_n &&
  1383. Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
  1384. OS << ", ";
  1385. PrintExpr(Node->getVal1());
  1386. }
  1387. if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
  1388. Node->isCmpXChg()) {
  1389. OS << ", ";
  1390. PrintExpr(Node->getVal2());
  1391. }
  1392. if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
  1393. Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
  1394. OS << ", ";
  1395. PrintExpr(Node->getWeak());
  1396. }
  1397. if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
  1398. Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
  1399. OS << ", ";
  1400. PrintExpr(Node->getOrder());
  1401. }
  1402. if (Node->isCmpXChg()) {
  1403. OS << ", ";
  1404. PrintExpr(Node->getOrderFail());
  1405. }
  1406. OS << ")";
  1407. }
  1408. // C++
  1409. void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
  1410. OverloadedOperatorKind Kind = Node->getOperator();
  1411. if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
  1412. if (Node->getNumArgs() == 1) {
  1413. OS << getOperatorSpelling(Kind) << ' ';
  1414. PrintExpr(Node->getArg(0));
  1415. } else {
  1416. PrintExpr(Node->getArg(0));
  1417. OS << ' ' << getOperatorSpelling(Kind);
  1418. }
  1419. } else if (Kind == OO_Arrow) {
  1420. PrintExpr(Node->getArg(0));
  1421. } else if (Kind == OO_Call) {
  1422. PrintExpr(Node->getArg(0));
  1423. OS << '(';
  1424. for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
  1425. if (ArgIdx > 1)
  1426. OS << ", ";
  1427. if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
  1428. PrintExpr(Node->getArg(ArgIdx));
  1429. }
  1430. OS << ')';
  1431. } else if (Kind == OO_Subscript) {
  1432. PrintExpr(Node->getArg(0));
  1433. OS << '[';
  1434. PrintExpr(Node->getArg(1));
  1435. OS << ']';
  1436. } else if (Node->getNumArgs() == 1) {
  1437. OS << getOperatorSpelling(Kind) << ' ';
  1438. PrintExpr(Node->getArg(0));
  1439. } else if (Node->getNumArgs() == 2) {
  1440. PrintExpr(Node->getArg(0));
  1441. OS << ' ' << getOperatorSpelling(Kind) << ' ';
  1442. PrintExpr(Node->getArg(1));
  1443. } else {
  1444. llvm_unreachable("unknown overloaded operator");
  1445. }
  1446. }
  1447. void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
  1448. // If we have a conversion operator call only print the argument.
  1449. CXXMethodDecl *MD = Node->getMethodDecl();
  1450. if (MD && isa<CXXConversionDecl>(MD)) {
  1451. PrintExpr(Node->getImplicitObjectArgument());
  1452. return;
  1453. }
  1454. VisitCallExpr(cast<CallExpr>(Node));
  1455. }
  1456. void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
  1457. PrintExpr(Node->getCallee());
  1458. OS << "<<<";
  1459. PrintCallArgs(Node->getConfig());
  1460. OS << ">>>(";
  1461. PrintCallArgs(Node);
  1462. OS << ")";
  1463. }
  1464. void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
  1465. OS << Node->getCastName() << '<';
  1466. Node->getTypeAsWritten().print(OS, Policy);
  1467. OS << ">(";
  1468. PrintExpr(Node->getSubExpr());
  1469. OS << ")";
  1470. }
  1471. void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
  1472. VisitCXXNamedCastExpr(Node);
  1473. }
  1474. void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
  1475. VisitCXXNamedCastExpr(Node);
  1476. }
  1477. void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
  1478. VisitCXXNamedCastExpr(Node);
  1479. }
  1480. void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
  1481. VisitCXXNamedCastExpr(Node);
  1482. }
  1483. void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
  1484. OS << "__builtin_bit_cast(";
  1485. Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
  1486. OS << ", ";
  1487. PrintExpr(Node->getSubExpr());
  1488. OS << ")";
  1489. }
  1490. void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
  1491. OS << "typeid(";
  1492. if (Node->isTypeOperand()) {
  1493. Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
  1494. } else {
  1495. PrintExpr(Node->getExprOperand());
  1496. }
  1497. OS << ")";
  1498. }
  1499. void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
  1500. OS << "__uuidof(";
  1501. if (Node->isTypeOperand()) {
  1502. Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
  1503. } else {
  1504. PrintExpr(Node->getExprOperand());
  1505. }
  1506. OS << ")";
  1507. }
  1508. void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
  1509. PrintExpr(Node->getBaseExpr());
  1510. if (Node->isArrow())
  1511. OS << "->";
  1512. else
  1513. OS << ".";
  1514. if (NestedNameSpecifier *Qualifier =
  1515. Node->getQualifierLoc().getNestedNameSpecifier())
  1516. Qualifier->print(OS, Policy);
  1517. OS << Node->getPropertyDecl()->getDeclName();
  1518. }
  1519. void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
  1520. PrintExpr(Node->getBase());
  1521. OS << "[";
  1522. PrintExpr(Node->getIdx());
  1523. OS << "]";
  1524. }
  1525. void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
  1526. switch (Node->getLiteralOperatorKind()) {
  1527. case UserDefinedLiteral::LOK_Raw:
  1528. OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
  1529. break;
  1530. case UserDefinedLiteral::LOK_Template: {
  1531. const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
  1532. const TemplateArgumentList *Args =
  1533. cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
  1534. assert(Args);
  1535. if (Args->size() != 1) {
  1536. OS << "operator\"\"" << Node->getUDSuffix()->getName();
  1537. printTemplateArgumentList(OS, Args->asArray(), Policy);
  1538. OS << "()";
  1539. return;
  1540. }
  1541. const TemplateArgument &Pack = Args->get(0);
  1542. for (const auto &P : Pack.pack_elements()) {
  1543. char C = (char)P.getAsIntegral().getZExtValue();
  1544. OS << C;
  1545. }
  1546. break;
  1547. }
  1548. case UserDefinedLiteral::LOK_Integer: {
  1549. // Print integer literal without suffix.
  1550. const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
  1551. OS << Int->getValue().toString(10, /*isSigned*/false);
  1552. break;
  1553. }
  1554. case UserDefinedLiteral::LOK_Floating: {
  1555. // Print floating literal without suffix.
  1556. auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
  1557. PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
  1558. break;
  1559. }
  1560. case UserDefinedLiteral::LOK_String:
  1561. case UserDefinedLiteral::LOK_Character:
  1562. PrintExpr(Node->getCookedLiteral());
  1563. break;
  1564. }
  1565. OS << Node->getUDSuffix()->getName();
  1566. }
  1567. void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
  1568. OS << (Node->getValue() ? "true" : "false");
  1569. }
  1570. void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
  1571. OS << "nullptr";
  1572. }
  1573. void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
  1574. OS << "this";
  1575. }
  1576. void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
  1577. if (!Node->getSubExpr())
  1578. OS << "throw";
  1579. else {
  1580. OS << "throw ";
  1581. PrintExpr(Node->getSubExpr());
  1582. }
  1583. }
  1584. void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
  1585. // Nothing to print: we picked up the default argument.
  1586. }
  1587. void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
  1588. // Nothing to print: we picked up the default initializer.
  1589. }
  1590. void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
  1591. Node->getType().print(OS, Policy);
  1592. // If there are no parens, this is list-initialization, and the braces are
  1593. // part of the syntax of the inner construct.
  1594. if (Node->getLParenLoc().isValid())
  1595. OS << "(";
  1596. PrintExpr(Node->getSubExpr());
  1597. if (Node->getLParenLoc().isValid())
  1598. OS << ")";
  1599. }
  1600. void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
  1601. PrintExpr(Node->getSubExpr());
  1602. }
  1603. void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
  1604. Node->getType().print(OS, Policy);
  1605. if (Node->isStdInitListInitialization())
  1606. /* Nothing to do; braces are part of creating the std::initializer_list. */;
  1607. else if (Node->isListInitialization())
  1608. OS << "{";
  1609. else
  1610. OS << "(";
  1611. for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
  1612. ArgEnd = Node->arg_end();
  1613. Arg != ArgEnd; ++Arg) {
  1614. if ((*Arg)->isDefaultArgument())
  1615. break;
  1616. if (Arg != Node->arg_begin())
  1617. OS << ", ";
  1618. PrintExpr(*Arg);
  1619. }
  1620. if (Node->isStdInitListInitialization())
  1621. /* See above. */;
  1622. else if (Node->isListInitialization())
  1623. OS << "}";
  1624. else
  1625. OS << ")";
  1626. }
  1627. void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
  1628. OS << '[';
  1629. bool NeedComma = false;
  1630. switch (Node->getCaptureDefault()) {
  1631. case LCD_None:
  1632. break;
  1633. case LCD_ByCopy:
  1634. OS << '=';
  1635. NeedComma = true;
  1636. break;
  1637. case LCD_ByRef:
  1638. OS << '&';
  1639. NeedComma = true;
  1640. break;
  1641. }
  1642. for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
  1643. CEnd = Node->explicit_capture_end();
  1644. C != CEnd;
  1645. ++C) {
  1646. if (C->capturesVLAType())
  1647. continue;
  1648. if (NeedComma)
  1649. OS << ", ";
  1650. NeedComma = true;
  1651. switch (C->getCaptureKind()) {
  1652. case LCK_This:
  1653. OS << "this";
  1654. break;
  1655. case LCK_StarThis:
  1656. OS << "*this";
  1657. break;
  1658. case LCK_ByRef:
  1659. if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
  1660. OS << '&';
  1661. OS << C->getCapturedVar()->getName();
  1662. break;
  1663. case LCK_ByCopy:
  1664. OS << C->getCapturedVar()->getName();
  1665. break;
  1666. case LCK_VLAType:
  1667. llvm_unreachable("VLA type in explicit captures.");
  1668. }
  1669. if (C->isPackExpansion())
  1670. OS << "...";
  1671. if (Node->isInitCapture(C))
  1672. PrintExpr(C->getCapturedVar()->getInit());
  1673. }
  1674. OS << ']';
  1675. if (!Node->getExplicitTemplateParameters().empty()) {
  1676. Node->getTemplateParameterList()->print(
  1677. OS, Node->getLambdaClass()->getASTContext(),
  1678. /*OmitTemplateKW*/true);
  1679. }
  1680. if (Node->hasExplicitParameters()) {
  1681. OS << '(';
  1682. CXXMethodDecl *Method = Node->getCallOperator();
  1683. NeedComma = false;
  1684. for (const auto *P : Method->parameters()) {
  1685. if (NeedComma) {
  1686. OS << ", ";
  1687. } else {
  1688. NeedComma = true;
  1689. }
  1690. std::string ParamStr = P->getNameAsString();
  1691. P->getOriginalType().print(OS, Policy, ParamStr);
  1692. }
  1693. if (Method->isVariadic()) {
  1694. if (NeedComma)
  1695. OS << ", ";
  1696. OS << "...";
  1697. }
  1698. OS << ')';
  1699. if (Node->isMutable())
  1700. OS << " mutable";
  1701. auto *Proto = Method->getType()->castAs<FunctionProtoType>();
  1702. Proto->printExceptionSpecification(OS, Policy);
  1703. // FIXME: Attributes
  1704. // Print the trailing return type if it was specified in the source.
  1705. if (Node->hasExplicitResultType()) {
  1706. OS << " -> ";
  1707. Proto->getReturnType().print(OS, Policy);
  1708. }
  1709. }
  1710. // Print the body.
  1711. OS << ' ';
  1712. if (Policy.TerseOutput)
  1713. OS << "{}";
  1714. else
  1715. PrintRawCompoundStmt(Node->getBody());
  1716. }
  1717. void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
  1718. if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
  1719. TSInfo->getType().print(OS, Policy);
  1720. else
  1721. Node->getType().print(OS, Policy);
  1722. OS << "()";
  1723. }
  1724. void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
  1725. if (E->isGlobalNew())
  1726. OS << "::";
  1727. OS << "new ";
  1728. unsigned NumPlace = E->getNumPlacementArgs();
  1729. if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
  1730. OS << "(";
  1731. PrintExpr(E->getPlacementArg(0));
  1732. for (unsigned i = 1; i < NumPlace; ++i) {
  1733. if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
  1734. break;
  1735. OS << ", ";
  1736. PrintExpr(E->getPlacementArg(i));
  1737. }
  1738. OS << ") ";
  1739. }
  1740. if (E->isParenTypeId())
  1741. OS << "(";
  1742. std::string TypeS;
  1743. if (Optional<Expr *> Size = E->getArraySize()) {
  1744. llvm::raw_string_ostream s(TypeS);
  1745. s << '[';
  1746. if (*Size)
  1747. (*Size)->printPretty(s, Helper, Policy);
  1748. s << ']';
  1749. }
  1750. E->getAllocatedType().print(OS, Policy, TypeS);
  1751. if (E->isParenTypeId())
  1752. OS << ")";
  1753. CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
  1754. if (InitStyle) {
  1755. if (InitStyle == CXXNewExpr::CallInit)
  1756. OS << "(";
  1757. PrintExpr(E->getInitializer());
  1758. if (InitStyle == CXXNewExpr::CallInit)
  1759. OS << ")";
  1760. }
  1761. }
  1762. void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
  1763. if (E->isGlobalDelete())
  1764. OS << "::";
  1765. OS << "delete ";
  1766. if (E->isArrayForm())
  1767. OS << "[] ";
  1768. PrintExpr(E->getArgument());
  1769. }
  1770. void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
  1771. PrintExpr(E->getBase());
  1772. if (E->isArrow())
  1773. OS << "->";
  1774. else
  1775. OS << '.';
  1776. if (E->getQualifier())
  1777. E->getQualifier()->print(OS, Policy);
  1778. OS << "~";
  1779. if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
  1780. OS << II->getName();
  1781. else
  1782. E->getDestroyedType().print(OS, Policy);
  1783. }
  1784. void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
  1785. if (E->isListInitialization() && !E->isStdInitListInitialization())
  1786. OS << "{";
  1787. for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
  1788. if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
  1789. // Don't print any defaulted arguments
  1790. break;
  1791. }
  1792. if (i) OS << ", ";
  1793. PrintExpr(E->getArg(i));
  1794. }
  1795. if (E->isListInitialization() && !E->isStdInitListInitialization())
  1796. OS << "}";
  1797. }
  1798. void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
  1799. // Parens are printed by the surrounding context.
  1800. OS << "<forwarded>";
  1801. }
  1802. void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
  1803. PrintExpr(E->getSubExpr());
  1804. }
  1805. void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
  1806. // Just forward to the subexpression.
  1807. PrintExpr(E->getSubExpr());
  1808. }
  1809. void
  1810. StmtPrinter::VisitCXXUnresolvedConstructExpr(
  1811. CXXUnresolvedConstructExpr *Node) {
  1812. Node->getTypeAsWritten().print(OS, Policy);
  1813. OS << "(";
  1814. for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
  1815. ArgEnd = Node->arg_end();
  1816. Arg != ArgEnd; ++Arg) {
  1817. if (Arg != Node->arg_begin())
  1818. OS << ", ";
  1819. PrintExpr(*Arg);
  1820. }
  1821. OS << ")";
  1822. }
  1823. void StmtPrinter::VisitCXXDependentScopeMemberExpr(
  1824. CXXDependentScopeMemberExpr *Node) {
  1825. if (!Node->isImplicitAccess()) {
  1826. PrintExpr(Node->getBase());
  1827. OS << (Node->isArrow() ? "->" : ".");
  1828. }
  1829. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1830. Qualifier->print(OS, Policy);
  1831. if (Node->hasTemplateKeyword())
  1832. OS << "template ";
  1833. OS << Node->getMemberNameInfo();
  1834. if (Node->hasExplicitTemplateArgs())
  1835. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  1836. }
  1837. void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
  1838. if (!Node->isImplicitAccess()) {
  1839. PrintExpr(Node->getBase());
  1840. OS << (Node->isArrow() ? "->" : ".");
  1841. }
  1842. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1843. Qualifier->print(OS, Policy);
  1844. if (Node->hasTemplateKeyword())
  1845. OS << "template ";
  1846. OS << Node->getMemberNameInfo();
  1847. if (Node->hasExplicitTemplateArgs())
  1848. printTemplateArgumentList(OS, Node->template_arguments(), Policy);
  1849. }
  1850. static const char *getTypeTraitName(TypeTrait TT) {
  1851. switch (TT) {
  1852. #define TYPE_TRAIT_1(Spelling, Name, Key) \
  1853. case clang::UTT_##Name: return #Spelling;
  1854. #define TYPE_TRAIT_2(Spelling, Name, Key) \
  1855. case clang::BTT_##Name: return #Spelling;
  1856. #define TYPE_TRAIT_N(Spelling, Name, Key) \
  1857. case clang::TT_##Name: return #Spelling;
  1858. #include "clang/Basic/TokenKinds.def"
  1859. }
  1860. llvm_unreachable("Type trait not covered by switch");
  1861. }
  1862. static const char *getTypeTraitName(ArrayTypeTrait ATT) {
  1863. switch (ATT) {
  1864. case ATT_ArrayRank: return "__array_rank";
  1865. case ATT_ArrayExtent: return "__array_extent";
  1866. }
  1867. llvm_unreachable("Array type trait not covered by switch");
  1868. }
  1869. static const char *getExpressionTraitName(ExpressionTrait ET) {
  1870. switch (ET) {
  1871. case ET_IsLValueExpr: return "__is_lvalue_expr";
  1872. case ET_IsRValueExpr: return "__is_rvalue_expr";
  1873. }
  1874. llvm_unreachable("Expression type trait not covered by switch");
  1875. }
  1876. void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
  1877. OS << getTypeTraitName(E->getTrait()) << "(";
  1878. for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
  1879. if (I > 0)
  1880. OS << ", ";
  1881. E->getArg(I)->getType().print(OS, Policy);
  1882. }
  1883. OS << ")";
  1884. }
  1885. void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
  1886. OS << getTypeTraitName(E->getTrait()) << '(';
  1887. E->getQueriedType().print(OS, Policy);
  1888. OS << ')';
  1889. }
  1890. void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
  1891. OS << getExpressionTraitName(E->getTrait()) << '(';
  1892. PrintExpr(E->getQueriedExpression());
  1893. OS << ')';
  1894. }
  1895. void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
  1896. OS << "noexcept(";
  1897. PrintExpr(E->getOperand());
  1898. OS << ")";
  1899. }
  1900. void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
  1901. PrintExpr(E->getPattern());
  1902. OS << "...";
  1903. }
  1904. void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
  1905. OS << "sizeof...(" << *E->getPack() << ")";
  1906. }
  1907. void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
  1908. SubstNonTypeTemplateParmPackExpr *Node) {
  1909. OS << *Node->getParameterPack();
  1910. }
  1911. void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
  1912. SubstNonTypeTemplateParmExpr *Node) {
  1913. Visit(Node->getReplacement());
  1914. }
  1915. void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
  1916. OS << *E->getParameterPack();
  1917. }
  1918. void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
  1919. PrintExpr(Node->GetTemporaryExpr());
  1920. }
  1921. void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
  1922. OS << "(";
  1923. if (E->getLHS()) {
  1924. PrintExpr(E->getLHS());
  1925. OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
  1926. }
  1927. OS << "...";
  1928. if (E->getRHS()) {
  1929. OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
  1930. PrintExpr(E->getRHS());
  1931. }
  1932. OS << ")";
  1933. }
  1934. // C++ Coroutines TS
  1935. void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
  1936. Visit(S->getBody());
  1937. }
  1938. void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
  1939. OS << "co_return";
  1940. if (S->getOperand()) {
  1941. OS << " ";
  1942. Visit(S->getOperand());
  1943. }
  1944. OS << ";";
  1945. }
  1946. void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
  1947. OS << "co_await ";
  1948. PrintExpr(S->getOperand());
  1949. }
  1950. void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
  1951. OS << "co_await ";
  1952. PrintExpr(S->getOperand());
  1953. }
  1954. void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
  1955. OS << "co_yield ";
  1956. PrintExpr(S->getOperand());
  1957. }
  1958. // Obj-C
  1959. void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
  1960. OS << "@";
  1961. VisitStringLiteral(Node->getString());
  1962. }
  1963. void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
  1964. OS << "@";
  1965. Visit(E->getSubExpr());
  1966. }
  1967. void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
  1968. OS << "@[ ";
  1969. ObjCArrayLiteral::child_range Ch = E->children();
  1970. for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
  1971. if (I != Ch.begin())
  1972. OS << ", ";
  1973. Visit(*I);
  1974. }
  1975. OS << " ]";
  1976. }
  1977. void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
  1978. OS << "@{ ";
  1979. for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
  1980. if (I > 0)
  1981. OS << ", ";
  1982. ObjCDictionaryElement Element = E->getKeyValueElement(I);
  1983. Visit(Element.Key);
  1984. OS << " : ";
  1985. Visit(Element.Value);
  1986. if (Element.isPackExpansion())
  1987. OS << "...";
  1988. }
  1989. OS << " }";
  1990. }
  1991. void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
  1992. OS << "@encode(";
  1993. Node->getEncodedType().print(OS, Policy);
  1994. OS << ')';
  1995. }
  1996. void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
  1997. OS << "@selector(";
  1998. Node->getSelector().print(OS);
  1999. OS << ')';
  2000. }
  2001. void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
  2002. OS << "@protocol(" << *Node->getProtocol() << ')';
  2003. }
  2004. void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
  2005. OS << "[";
  2006. switch (Mess->getReceiverKind()) {
  2007. case ObjCMessageExpr::Instance:
  2008. PrintExpr(Mess->getInstanceReceiver());
  2009. break;
  2010. case ObjCMessageExpr::Class:
  2011. Mess->getClassReceiver().print(OS, Policy);
  2012. break;
  2013. case ObjCMessageExpr::SuperInstance:
  2014. case ObjCMessageExpr::SuperClass:
  2015. OS << "Super";
  2016. break;
  2017. }
  2018. OS << ' ';
  2019. Selector selector = Mess->getSelector();
  2020. if (selector.isUnarySelector()) {
  2021. OS << selector.getNameForSlot(0);
  2022. } else {
  2023. for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
  2024. if (i < selector.getNumArgs()) {
  2025. if (i > 0) OS << ' ';
  2026. if (selector.getIdentifierInfoForSlot(i))
  2027. OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
  2028. else
  2029. OS << ":";
  2030. }
  2031. else OS << ", "; // Handle variadic methods.
  2032. PrintExpr(Mess->getArg(i));
  2033. }
  2034. }
  2035. OS << "]";
  2036. }
  2037. void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
  2038. OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
  2039. }
  2040. void
  2041. StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
  2042. PrintExpr(E->getSubExpr());
  2043. }
  2044. void
  2045. StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
  2046. OS << '(' << E->getBridgeKindName();
  2047. E->getType().print(OS, Policy);
  2048. OS << ')';
  2049. PrintExpr(E->getSubExpr());
  2050. }
  2051. void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
  2052. BlockDecl *BD = Node->getBlockDecl();
  2053. OS << "^";
  2054. const FunctionType *AFT = Node->getFunctionType();
  2055. if (isa<FunctionNoProtoType>(AFT)) {
  2056. OS << "()";
  2057. } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
  2058. OS << '(';
  2059. for (BlockDecl::param_iterator AI = BD->param_begin(),
  2060. E = BD->param_end(); AI != E; ++AI) {
  2061. if (AI != BD->param_begin()) OS << ", ";
  2062. std::string ParamStr = (*AI)->getNameAsString();
  2063. (*AI)->getType().print(OS, Policy, ParamStr);
  2064. }
  2065. const auto *FT = cast<FunctionProtoType>(AFT);
  2066. if (FT->isVariadic()) {
  2067. if (!BD->param_empty()) OS << ", ";
  2068. OS << "...";
  2069. }
  2070. OS << ')';
  2071. }
  2072. OS << "{ }";
  2073. }
  2074. void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
  2075. PrintExpr(Node->getSourceExpr());
  2076. }
  2077. void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
  2078. // TODO: Print something reasonable for a TypoExpr, if necessary.
  2079. llvm_unreachable("Cannot print TypoExpr nodes");
  2080. }
  2081. void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
  2082. OS << "__builtin_astype(";
  2083. PrintExpr(Node->getSrcExpr());
  2084. OS << ", ";
  2085. Node->getType().print(OS, Policy);
  2086. OS << ")";
  2087. }
  2088. //===----------------------------------------------------------------------===//
  2089. // Stmt method implementations
  2090. //===----------------------------------------------------------------------===//
  2091. void Stmt::dumpPretty(const ASTContext &Context) const {
  2092. printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
  2093. }
  2094. void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
  2095. const PrintingPolicy &Policy, unsigned Indentation,
  2096. StringRef NL, const ASTContext *Context) const {
  2097. StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
  2098. P.Visit(const_cast<Stmt *>(this));
  2099. }
  2100. void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
  2101. const PrintingPolicy &Policy, bool AddQuotes) const {
  2102. std::string Buf;
  2103. llvm::raw_string_ostream TempOut(Buf);
  2104. printPretty(TempOut, Helper, Policy);
  2105. Out << JsonFormat(TempOut.str(), AddQuotes);
  2106. }
  2107. //===----------------------------------------------------------------------===//
  2108. // PrinterHelper
  2109. //===----------------------------------------------------------------------===//
  2110. // Implement virtual destructor.
  2111. PrinterHelper::~PrinterHelper() = default;