StmtPrinter.cpp 64 KB

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