StmtPrinter.cpp 77 KB

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