StmtPrinter.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  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::Min16Int: // HLSL Change
  975. case BuiltinType::Short: OS << "i16"; break;
  976. case BuiltinType::Min16UInt: // HLSL Change
  977. case BuiltinType::UShort: OS << "Ui16"; break;
  978. case BuiltinType::LitInt: break; // HLSL Change
  979. case BuiltinType::Int: break; // no suffix.
  980. case BuiltinType::UInt: OS << 'U'; break;
  981. case BuiltinType::Long: OS << 'L'; break;
  982. case BuiltinType::ULong: OS << "UL"; break;
  983. case BuiltinType::LongLong: OS << "LL"; break;
  984. case BuiltinType::ULongLong: OS << "ULL"; break;
  985. case BuiltinType::Int128: OS << "i128"; break;
  986. case BuiltinType::UInt128: OS << "Ui128"; break;
  987. case BuiltinType::Int8_4Packed: OS << "i8_4pk"; break; // HLSL Change
  988. case BuiltinType::UInt8_4Packed: OS << "Ui8_4pk"; break; // HLSL Change
  989. }
  990. }
  991. static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
  992. bool PrintSuffix,
  993. bool HLSLSuffix = false) { // HLSL Change - added HLSLSuffix
  994. SmallString<16> Str;
  995. Node->getValue().toString(Str);
  996. OS << Str;
  997. if (Str.find_first_not_of("-0123456789") == StringRef::npos)
  998. OS << '.'; // Trailing dot in order to separate from ints.
  999. if (!PrintSuffix)
  1000. return;
  1001. // Emit suffixes. Float literals are always a builtin float type.
  1002. // HLSL Change Starts - override suffixes
  1003. if (HLSLSuffix) {
  1004. switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
  1005. default: llvm_unreachable("Unexpected type for float literal!");
  1006. case BuiltinType::LitFloat: break; // HLSL Change -- no suffix
  1007. case BuiltinType::Min10Float: break; // no suffix, as this is a literal and 'F' would pollute expression
  1008. case BuiltinType::HalfFloat:
  1009. case BuiltinType::Min16Float:
  1010. case BuiltinType::Half: OS << 'H'; break; // HLSL Change -- added suffix
  1011. case BuiltinType::Double: OS << 'L'; break; // HLSL Change -- added suffix
  1012. case BuiltinType::Float: OS << 'F'; break;
  1013. // Note absence of LongDouble - should not be possible in HLSL
  1014. }
  1015. return;
  1016. }
  1017. // HLSL Change Ends
  1018. // Emit suffixes. Float literals are always a builtin float type.
  1019. switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
  1020. default: llvm_unreachable("Unexpected type for float literal!");
  1021. case BuiltinType::Half: break; // FIXME: suffix?
  1022. case BuiltinType::Double: break; // no suffix.
  1023. case BuiltinType::Float: OS << 'F'; break;
  1024. case BuiltinType::LongDouble: OS << 'L'; break;
  1025. }
  1026. }
  1027. void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
  1028. PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true, Policy.LangOpts.HLSL); // HLSL Change
  1029. }
  1030. void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
  1031. PrintExpr(Node->getSubExpr());
  1032. OS << "i";
  1033. }
  1034. void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
  1035. Str->outputString(OS);
  1036. }
  1037. void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
  1038. OS << "(";
  1039. PrintExpr(Node->getSubExpr());
  1040. OS << ")";
  1041. }
  1042. void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
  1043. if (!Node->isPostfix()) {
  1044. OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
  1045. // Print a space if this is an "identifier operator" like __real, or if
  1046. // it might be concatenated incorrectly like '+'.
  1047. switch (Node->getOpcode()) {
  1048. default: break;
  1049. case UO_Real:
  1050. case UO_Imag:
  1051. case UO_Extension:
  1052. OS << ' ';
  1053. break;
  1054. case UO_Plus:
  1055. case UO_Minus:
  1056. if (isa<UnaryOperator>(Node->getSubExpr()))
  1057. OS << ' ';
  1058. break;
  1059. }
  1060. }
  1061. PrintExpr(Node->getSubExpr());
  1062. if (Node->isPostfix())
  1063. OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
  1064. }
  1065. void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
  1066. OS << "__builtin_offsetof(";
  1067. Node->getTypeSourceInfo()->getType().print(OS, Policy);
  1068. OS << ", ";
  1069. bool PrintedSomething = false;
  1070. for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
  1071. OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
  1072. if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
  1073. // Array node
  1074. OS << "[";
  1075. PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
  1076. OS << "]";
  1077. PrintedSomething = true;
  1078. continue;
  1079. }
  1080. // Skip implicit base indirections.
  1081. if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
  1082. continue;
  1083. // Field or identifier node.
  1084. IdentifierInfo *Id = ON.getFieldName();
  1085. if (!Id)
  1086. continue;
  1087. if (PrintedSomething)
  1088. OS << ".";
  1089. else
  1090. PrintedSomething = true;
  1091. OS << Id->getName();
  1092. }
  1093. OS << ")";
  1094. }
  1095. void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
  1096. // HLSL Change Begin
  1097. if (Node->getKind() == UETT_ArrayLength) {
  1098. PrintExpr(Node->getArgumentExpr());
  1099. OS << ".Length";
  1100. return;
  1101. }
  1102. // HLSL Change Ends
  1103. switch(Node->getKind()) {
  1104. case UETT_SizeOf:
  1105. OS << "sizeof";
  1106. break;
  1107. case UETT_AlignOf:
  1108. if (Policy.LangOpts.CPlusPlus)
  1109. OS << "alignof";
  1110. else if (Policy.LangOpts.C11)
  1111. OS << "_Alignof";
  1112. else
  1113. OS << "__alignof";
  1114. break;
  1115. case UETT_VecStep:
  1116. OS << "vec_step";
  1117. break;
  1118. case UETT_OpenMPRequiredSimdAlign:
  1119. OS << "__builtin_omp_required_simd_align";
  1120. break;
  1121. }
  1122. if (Node->isArgumentType()) {
  1123. OS << '(';
  1124. Node->getArgumentType().print(OS, Policy);
  1125. OS << ')';
  1126. } else {
  1127. OS << " ";
  1128. PrintExpr(Node->getArgumentExpr());
  1129. }
  1130. }
  1131. void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
  1132. OS << "_Generic(";
  1133. PrintExpr(Node->getControllingExpr());
  1134. for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
  1135. OS << ", ";
  1136. QualType T = Node->getAssocType(i);
  1137. if (T.isNull())
  1138. OS << "default";
  1139. else
  1140. T.print(OS, Policy);
  1141. OS << ": ";
  1142. PrintExpr(Node->getAssocExpr(i));
  1143. }
  1144. OS << ")";
  1145. }
  1146. void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
  1147. PrintExpr(Node->getLHS());
  1148. OS << "[";
  1149. PrintExpr(Node->getRHS());
  1150. OS << "]";
  1151. }
  1152. void StmtPrinter::PrintCallArgs(CallExpr *Call) {
  1153. for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
  1154. if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
  1155. // Don't print any defaulted arguments
  1156. break;
  1157. }
  1158. if (i) OS << ", ";
  1159. PrintExpr(Call->getArg(i));
  1160. }
  1161. }
  1162. void StmtPrinter::VisitCallExpr(CallExpr *Call) {
  1163. PrintExpr(Call->getCallee());
  1164. OS << "(";
  1165. PrintCallArgs(Call);
  1166. OS << ")";
  1167. }
  1168. void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
  1169. // FIXME: Suppress printing implicit bases (like "this")
  1170. PrintExpr(Node->getBase());
  1171. MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
  1172. FieldDecl *ParentDecl = ParentMember
  1173. ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
  1174. if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
  1175. OS << (!Policy.LangOpts.HLSL && Node->isArrow() ? "->" : "."); // HLSL Change
  1176. if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
  1177. if (FD->isAnonymousStructOrUnion())
  1178. return;
  1179. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1180. Qualifier->print(OS, Policy);
  1181. if (Node->hasTemplateKeyword())
  1182. OS << "template ";
  1183. OS << Node->getMemberNameInfo();
  1184. if (Node->hasExplicitTemplateArgs())
  1185. TemplateSpecializationType::PrintTemplateArgumentList(
  1186. OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
  1187. }
  1188. void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
  1189. PrintExpr(Node->getBase());
  1190. OS << (Node->isArrow() ? "->isa" : ".isa");
  1191. }
  1192. void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
  1193. PrintExpr(Node->getBase());
  1194. OS << ".";
  1195. OS << Node->getAccessor().getName();
  1196. }
  1197. // HLSL Change Starts
  1198. void StmtPrinter::VisitExtMatrixElementExpr(ExtMatrixElementExpr *Node) {
  1199. PrintExpr(Node->getBase());
  1200. OS << ".";
  1201. OS << Node->getAccessor().getName();
  1202. }
  1203. void StmtPrinter::VisitHLSLVectorElementExpr(HLSLVectorElementExpr *Node) {
  1204. PrintExpr(Node->getBase());
  1205. OS << ".";
  1206. OS << Node->getAccessor().getName();
  1207. }
  1208. // HLSL Change Ends
  1209. void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
  1210. OS << '(';
  1211. Node->getTypeAsWritten().print(OS, Policy);
  1212. OS << ')';
  1213. PrintExpr(Node->getSubExpr());
  1214. }
  1215. void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
  1216. OS << '(';
  1217. Node->getType().print(OS, Policy);
  1218. OS << ')';
  1219. PrintExpr(Node->getInitializer());
  1220. }
  1221. void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
  1222. // No need to print anything, simply forward to the subexpression.
  1223. PrintExpr(Node->getSubExpr());
  1224. }
  1225. void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
  1226. PrintExpr(Node->getLHS());
  1227. OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
  1228. PrintExpr(Node->getRHS());
  1229. }
  1230. void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
  1231. PrintExpr(Node->getLHS());
  1232. OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
  1233. PrintExpr(Node->getRHS());
  1234. }
  1235. void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
  1236. PrintExpr(Node->getCond());
  1237. OS << " ? ";
  1238. PrintExpr(Node->getLHS());
  1239. OS << " : ";
  1240. PrintExpr(Node->getRHS());
  1241. }
  1242. // GNU extensions.
  1243. void
  1244. StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
  1245. PrintExpr(Node->getCommon());
  1246. OS << " ?: ";
  1247. PrintExpr(Node->getFalseExpr());
  1248. }
  1249. void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
  1250. OS << "&&" << Node->getLabel()->getName();
  1251. }
  1252. void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
  1253. OS << "(";
  1254. PrintRawCompoundStmt(E->getSubStmt());
  1255. OS << ")";
  1256. }
  1257. void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
  1258. OS << "__builtin_choose_expr(";
  1259. PrintExpr(Node->getCond());
  1260. OS << ", ";
  1261. PrintExpr(Node->getLHS());
  1262. OS << ", ";
  1263. PrintExpr(Node->getRHS());
  1264. OS << ")";
  1265. }
  1266. void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
  1267. OS << "__null";
  1268. }
  1269. void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
  1270. OS << "__builtin_shufflevector(";
  1271. for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
  1272. if (i) OS << ", ";
  1273. PrintExpr(Node->getExpr(i));
  1274. }
  1275. OS << ")";
  1276. }
  1277. void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
  1278. OS << "__builtin_convertvector(";
  1279. PrintExpr(Node->getSrcExpr());
  1280. OS << ", ";
  1281. Node->getType().print(OS, Policy);
  1282. OS << ")";
  1283. }
  1284. void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
  1285. if (Node->getSyntacticForm()) {
  1286. Visit(Node->getSyntacticForm());
  1287. return;
  1288. }
  1289. if (!Policy.LangOpts.HLSL || (Node->getLBraceLoc().isValid() || Node->getRBraceLoc().isValid())) // HLSL Change
  1290. OS << "{ ";
  1291. for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
  1292. if (i) OS << ", ";
  1293. if (Node->getInit(i))
  1294. PrintExpr(Node->getInit(i));
  1295. else
  1296. OS << "{}";
  1297. }
  1298. if (!Policy.LangOpts.HLSL || (Node->getLBraceLoc().isValid() || Node->getRBraceLoc().isValid())) // HLSL Change
  1299. OS << " }";
  1300. }
  1301. void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
  1302. OS << "(";
  1303. for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
  1304. if (i) OS << ", ";
  1305. PrintExpr(Node->getExpr(i));
  1306. }
  1307. OS << ")";
  1308. }
  1309. void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
  1310. bool NeedsEquals = true;
  1311. for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
  1312. DEnd = Node->designators_end();
  1313. D != DEnd; ++D) {
  1314. if (D->isFieldDesignator()) {
  1315. if (D->getDotLoc().isInvalid()) {
  1316. if (IdentifierInfo *II = D->getFieldName()) {
  1317. OS << II->getName() << ":";
  1318. NeedsEquals = false;
  1319. }
  1320. } else {
  1321. OS << "." << D->getFieldName()->getName();
  1322. }
  1323. } else {
  1324. OS << "[";
  1325. if (D->isArrayDesignator()) {
  1326. PrintExpr(Node->getArrayIndex(*D));
  1327. } else {
  1328. PrintExpr(Node->getArrayRangeStart(*D));
  1329. OS << " ... ";
  1330. PrintExpr(Node->getArrayRangeEnd(*D));
  1331. }
  1332. OS << "]";
  1333. }
  1334. }
  1335. if (NeedsEquals)
  1336. OS << " = ";
  1337. else
  1338. OS << " ";
  1339. PrintExpr(Node->getInit());
  1340. }
  1341. void StmtPrinter::VisitDesignatedInitUpdateExpr(
  1342. DesignatedInitUpdateExpr *Node) {
  1343. OS << "{";
  1344. OS << "/*base*/";
  1345. PrintExpr(Node->getBase());
  1346. OS << ", ";
  1347. OS << "/*updater*/";
  1348. PrintExpr(Node->getUpdater());
  1349. OS << "}";
  1350. }
  1351. void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
  1352. OS << "/*no init*/";
  1353. }
  1354. void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
  1355. if (Policy.LangOpts.CPlusPlus) {
  1356. OS << "/*implicit*/";
  1357. Node->getType().print(OS, Policy);
  1358. OS << "()";
  1359. } else {
  1360. OS << "/*implicit*/(";
  1361. Node->getType().print(OS, Policy);
  1362. OS << ')';
  1363. if (Node->getType()->isRecordType())
  1364. OS << "{}";
  1365. else
  1366. OS << 0;
  1367. }
  1368. }
  1369. void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
  1370. OS << "__builtin_va_arg(";
  1371. PrintExpr(Node->getSubExpr());
  1372. OS << ", ";
  1373. Node->getType().print(OS, Policy);
  1374. OS << ")";
  1375. }
  1376. void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
  1377. PrintExpr(Node->getSyntacticForm());
  1378. }
  1379. void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
  1380. const char *Name = nullptr;
  1381. switch (Node->getOp()) {
  1382. #define BUILTIN(ID, TYPE, ATTRS)
  1383. #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
  1384. case AtomicExpr::AO ## ID: \
  1385. Name = #ID "("; \
  1386. break;
  1387. #include "clang/Basic/Builtins.def"
  1388. }
  1389. OS << Name;
  1390. // AtomicExpr stores its subexpressions in a permuted order.
  1391. PrintExpr(Node->getPtr());
  1392. if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
  1393. Node->getOp() != AtomicExpr::AO__atomic_load_n) {
  1394. OS << ", ";
  1395. PrintExpr(Node->getVal1());
  1396. }
  1397. if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
  1398. Node->isCmpXChg()) {
  1399. OS << ", ";
  1400. PrintExpr(Node->getVal2());
  1401. }
  1402. if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
  1403. Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
  1404. OS << ", ";
  1405. PrintExpr(Node->getWeak());
  1406. }
  1407. if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
  1408. OS << ", ";
  1409. PrintExpr(Node->getOrder());
  1410. }
  1411. if (Node->isCmpXChg()) {
  1412. OS << ", ";
  1413. PrintExpr(Node->getOrderFail());
  1414. }
  1415. OS << ")";
  1416. }
  1417. // C++
  1418. void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
  1419. const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
  1420. "",
  1421. #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
  1422. Spelling,
  1423. #include "clang/Basic/OperatorKinds.def"
  1424. };
  1425. OverloadedOperatorKind Kind = Node->getOperator();
  1426. if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
  1427. if (Node->getNumArgs() == 1) {
  1428. OS << OpStrings[Kind] << ' ';
  1429. PrintExpr(Node->getArg(0));
  1430. } else {
  1431. PrintExpr(Node->getArg(0));
  1432. OS << ' ' << OpStrings[Kind];
  1433. }
  1434. } else if (Kind == OO_Arrow) {
  1435. PrintExpr(Node->getArg(0));
  1436. } else if (Kind == OO_Call) {
  1437. PrintExpr(Node->getArg(0));
  1438. OS << '(';
  1439. for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
  1440. if (ArgIdx > 1)
  1441. OS << ", ";
  1442. if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
  1443. PrintExpr(Node->getArg(ArgIdx));
  1444. }
  1445. OS << ')';
  1446. } else if (Kind == OO_Subscript) {
  1447. PrintExpr(Node->getArg(0));
  1448. OS << '[';
  1449. PrintExpr(Node->getArg(1));
  1450. OS << ']';
  1451. } else if (Node->getNumArgs() == 1) {
  1452. OS << OpStrings[Kind] << ' ';
  1453. PrintExpr(Node->getArg(0));
  1454. } else if (Node->getNumArgs() == 2) {
  1455. PrintExpr(Node->getArg(0));
  1456. OS << ' ' << OpStrings[Kind] << ' ';
  1457. PrintExpr(Node->getArg(1));
  1458. } else {
  1459. llvm_unreachable("unknown overloaded operator");
  1460. }
  1461. }
  1462. void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
  1463. // If we have a conversion operator call only print the argument.
  1464. CXXMethodDecl *MD = Node->getMethodDecl();
  1465. if (MD && isa<CXXConversionDecl>(MD)) {
  1466. PrintExpr(Node->getImplicitObjectArgument());
  1467. return;
  1468. }
  1469. VisitCallExpr(cast<CallExpr>(Node));
  1470. }
  1471. void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
  1472. PrintExpr(Node->getCallee());
  1473. OS << "<<<";
  1474. PrintCallArgs(Node->getConfig());
  1475. OS << ">>>(";
  1476. PrintCallArgs(Node);
  1477. OS << ")";
  1478. }
  1479. void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
  1480. OS << Node->getCastName() << '<';
  1481. Node->getTypeAsWritten().print(OS, Policy);
  1482. OS << ">(";
  1483. PrintExpr(Node->getSubExpr());
  1484. OS << ")";
  1485. }
  1486. void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
  1487. VisitCXXNamedCastExpr(Node);
  1488. }
  1489. void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
  1490. VisitCXXNamedCastExpr(Node);
  1491. }
  1492. void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
  1493. VisitCXXNamedCastExpr(Node);
  1494. }
  1495. void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
  1496. VisitCXXNamedCastExpr(Node);
  1497. }
  1498. void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
  1499. OS << "typeid(";
  1500. if (Node->isTypeOperand()) {
  1501. Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
  1502. } else {
  1503. PrintExpr(Node->getExprOperand());
  1504. }
  1505. OS << ")";
  1506. }
  1507. void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
  1508. OS << "__uuidof(";
  1509. if (Node->isTypeOperand()) {
  1510. Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
  1511. } else {
  1512. PrintExpr(Node->getExprOperand());
  1513. }
  1514. OS << ")";
  1515. }
  1516. void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
  1517. PrintExpr(Node->getBaseExpr());
  1518. if (Node->isArrow())
  1519. OS << "->";
  1520. else
  1521. OS << ".";
  1522. if (NestedNameSpecifier *Qualifier =
  1523. Node->getQualifierLoc().getNestedNameSpecifier())
  1524. Qualifier->print(OS, Policy);
  1525. OS << Node->getPropertyDecl()->getDeclName();
  1526. }
  1527. void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
  1528. switch (Node->getLiteralOperatorKind()) {
  1529. case UserDefinedLiteral::LOK_Raw:
  1530. OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
  1531. break;
  1532. case UserDefinedLiteral::LOK_Template: {
  1533. DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
  1534. const TemplateArgumentList *Args =
  1535. cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
  1536. assert(Args);
  1537. if (Args->size() != 1) {
  1538. OS << "operator \"\" " << Node->getUDSuffix()->getName();
  1539. TemplateSpecializationType::PrintTemplateArgumentList(
  1540. OS, Args->data(), Args->size(), Policy);
  1541. OS << "()";
  1542. return;
  1543. }
  1544. const TemplateArgument &Pack = Args->get(0);
  1545. for (const auto &P : Pack.pack_elements()) {
  1546. char C = (char)P.getAsIntegral().getZExtValue();
  1547. OS << C;
  1548. }
  1549. break;
  1550. }
  1551. case UserDefinedLiteral::LOK_Integer: {
  1552. // Print integer literal without suffix.
  1553. IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
  1554. OS << Int->getValue().toString(10, /*isSigned*/false);
  1555. break;
  1556. }
  1557. case UserDefinedLiteral::LOK_Floating: {
  1558. // Print floating literal without suffix.
  1559. FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
  1560. PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false, Policy.LangOpts.HLSL); // HLSL Change
  1561. break;
  1562. }
  1563. case UserDefinedLiteral::LOK_String:
  1564. case UserDefinedLiteral::LOK_Character:
  1565. PrintExpr(Node->getCookedLiteral());
  1566. break;
  1567. }
  1568. OS << Node->getUDSuffix()->getName();
  1569. }
  1570. void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
  1571. OS << (Node->getValue() ? "true" : "false");
  1572. }
  1573. void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
  1574. OS << "nullptr";
  1575. }
  1576. void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
  1577. OS << "this";
  1578. }
  1579. void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
  1580. if (!Node->getSubExpr())
  1581. OS << "throw";
  1582. else {
  1583. OS << "throw ";
  1584. PrintExpr(Node->getSubExpr());
  1585. }
  1586. }
  1587. void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
  1588. // Nothing to print: we picked up the default argument.
  1589. }
  1590. void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
  1591. // Nothing to print: we picked up the default initializer.
  1592. }
  1593. void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
  1594. Node->getType().print(OS, Policy);
  1595. // If there are no parens, this is list-initialization, and the braces are
  1596. // part of the syntax of the inner construct.
  1597. if (Node->getLParenLoc().isValid())
  1598. OS << "(";
  1599. PrintExpr(Node->getSubExpr());
  1600. if (Node->getLParenLoc().isValid())
  1601. OS << ")";
  1602. }
  1603. void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
  1604. PrintExpr(Node->getSubExpr());
  1605. }
  1606. void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
  1607. Node->getType().print(OS, Policy);
  1608. if (Node->isStdInitListInitialization())
  1609. /* Nothing to do; braces are part of creating the std::initializer_list. */;
  1610. else if (Node->isListInitialization())
  1611. OS << "{";
  1612. else
  1613. OS << "(";
  1614. for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
  1615. ArgEnd = Node->arg_end();
  1616. Arg != ArgEnd; ++Arg) {
  1617. if (Arg->isDefaultArgument())
  1618. break;
  1619. if (Arg != Node->arg_begin())
  1620. OS << ", ";
  1621. PrintExpr(*Arg);
  1622. }
  1623. if (Node->isStdInitListInitialization())
  1624. /* See above. */;
  1625. else if (Node->isListInitialization())
  1626. OS << "}";
  1627. else
  1628. OS << ")";
  1629. }
  1630. void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
  1631. OS << '[';
  1632. bool NeedComma = false;
  1633. switch (Node->getCaptureDefault()) {
  1634. case LCD_None:
  1635. break;
  1636. case LCD_ByCopy:
  1637. OS << '=';
  1638. NeedComma = true;
  1639. break;
  1640. case LCD_ByRef:
  1641. OS << '&';
  1642. NeedComma = true;
  1643. break;
  1644. }
  1645. for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
  1646. CEnd = Node->explicit_capture_end();
  1647. C != CEnd;
  1648. ++C) {
  1649. if (NeedComma)
  1650. OS << ", ";
  1651. NeedComma = true;
  1652. switch (C->getCaptureKind()) {
  1653. case LCK_This:
  1654. OS << "this";
  1655. break;
  1656. case LCK_ByRef:
  1657. if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
  1658. OS << '&';
  1659. OS << C->getCapturedVar()->getName();
  1660. break;
  1661. case LCK_ByCopy:
  1662. OS << C->getCapturedVar()->getName();
  1663. break;
  1664. case LCK_VLAType:
  1665. llvm_unreachable("VLA type in explicit captures.");
  1666. }
  1667. if (Node->isInitCapture(C))
  1668. PrintExpr(C->getCapturedVar()->getInit());
  1669. }
  1670. OS << ']';
  1671. if (Node->hasExplicitParameters()) {
  1672. OS << " (";
  1673. CXXMethodDecl *Method = Node->getCallOperator();
  1674. NeedComma = false;
  1675. for (auto P : Method->params()) {
  1676. if (NeedComma) {
  1677. OS << ", ";
  1678. } else {
  1679. NeedComma = true;
  1680. }
  1681. std::string ParamStr = P->getNameAsString();
  1682. P->getOriginalType().print(OS, Policy, ParamStr);
  1683. }
  1684. if (Method->isVariadic()) {
  1685. if (NeedComma)
  1686. OS << ", ";
  1687. OS << "...";
  1688. }
  1689. OS << ')';
  1690. if (Node->isMutable())
  1691. OS << " mutable";
  1692. const FunctionProtoType *Proto
  1693. = Method->getType()->getAs<FunctionProtoType>();
  1694. Proto->printExceptionSpecification(OS, Policy);
  1695. // FIXME: Attributes
  1696. // Print the trailing return type if it was specified in the source.
  1697. if (Node->hasExplicitResultType()) {
  1698. OS << " -> ";
  1699. Proto->getReturnType().print(OS, Policy);
  1700. }
  1701. }
  1702. // Print the body.
  1703. CompoundStmt *Body = Node->getBody();
  1704. OS << ' ';
  1705. PrintStmt(Body);
  1706. }
  1707. void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
  1708. if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
  1709. TSInfo->getType().print(OS, Policy);
  1710. else
  1711. Node->getType().print(OS, Policy);
  1712. OS << "()";
  1713. }
  1714. void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
  1715. if (E->isGlobalNew())
  1716. OS << "::";
  1717. OS << "new ";
  1718. unsigned NumPlace = E->getNumPlacementArgs();
  1719. if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
  1720. OS << "(";
  1721. PrintExpr(E->getPlacementArg(0));
  1722. for (unsigned i = 1; i < NumPlace; ++i) {
  1723. if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
  1724. break;
  1725. OS << ", ";
  1726. PrintExpr(E->getPlacementArg(i));
  1727. }
  1728. OS << ") ";
  1729. }
  1730. if (E->isParenTypeId())
  1731. OS << "(";
  1732. std::string TypeS;
  1733. if (Expr *Size = E->getArraySize()) {
  1734. llvm::raw_string_ostream s(TypeS);
  1735. s << '[';
  1736. Size->printPretty(s, Helper, Policy);
  1737. s << ']';
  1738. }
  1739. E->getAllocatedType().print(OS, Policy, TypeS);
  1740. if (E->isParenTypeId())
  1741. OS << ")";
  1742. CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
  1743. if (InitStyle) {
  1744. if (InitStyle == CXXNewExpr::CallInit)
  1745. OS << "(";
  1746. PrintExpr(E->getInitializer());
  1747. if (InitStyle == CXXNewExpr::CallInit)
  1748. OS << ")";
  1749. }
  1750. }
  1751. void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
  1752. if (E->isGlobalDelete())
  1753. OS << "::";
  1754. OS << "delete ";
  1755. if (E->isArrayForm())
  1756. OS << "[] ";
  1757. PrintExpr(E->getArgument());
  1758. }
  1759. void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
  1760. PrintExpr(E->getBase());
  1761. if (E->isArrow())
  1762. OS << "->";
  1763. else
  1764. OS << '.';
  1765. if (E->getQualifier())
  1766. E->getQualifier()->print(OS, Policy);
  1767. OS << "~";
  1768. if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
  1769. OS << II->getName();
  1770. else
  1771. E->getDestroyedType().print(OS, Policy);
  1772. }
  1773. void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
  1774. if (E->isListInitialization() && !E->isStdInitListInitialization())
  1775. OS << "{";
  1776. for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
  1777. if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
  1778. // Don't print any defaulted arguments
  1779. break;
  1780. }
  1781. if (i) OS << ", ";
  1782. PrintExpr(E->getArg(i));
  1783. }
  1784. if (E->isListInitialization() && !E->isStdInitListInitialization())
  1785. OS << "}";
  1786. }
  1787. void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
  1788. PrintExpr(E->getSubExpr());
  1789. }
  1790. void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
  1791. // Just forward to the subexpression.
  1792. PrintExpr(E->getSubExpr());
  1793. }
  1794. void
  1795. StmtPrinter::VisitCXXUnresolvedConstructExpr(
  1796. CXXUnresolvedConstructExpr *Node) {
  1797. Node->getTypeAsWritten().print(OS, Policy);
  1798. OS << "(";
  1799. for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
  1800. ArgEnd = Node->arg_end();
  1801. Arg != ArgEnd; ++Arg) {
  1802. if (Arg != Node->arg_begin())
  1803. OS << ", ";
  1804. PrintExpr(*Arg);
  1805. }
  1806. OS << ")";
  1807. }
  1808. void StmtPrinter::VisitCXXDependentScopeMemberExpr(
  1809. CXXDependentScopeMemberExpr *Node) {
  1810. if (!Node->isImplicitAccess()) {
  1811. PrintExpr(Node->getBase());
  1812. OS << (Node->isArrow() ? "->" : ".");
  1813. }
  1814. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1815. Qualifier->print(OS, Policy);
  1816. if (Node->hasTemplateKeyword())
  1817. OS << "template ";
  1818. OS << Node->getMemberNameInfo();
  1819. if (Node->hasExplicitTemplateArgs())
  1820. TemplateSpecializationType::PrintTemplateArgumentList(
  1821. OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
  1822. }
  1823. void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
  1824. if (!Node->isImplicitAccess()) {
  1825. PrintExpr(Node->getBase());
  1826. OS << (Node->isArrow() ? "->" : ".");
  1827. }
  1828. if (NestedNameSpecifier *Qualifier = Node->getQualifier())
  1829. Qualifier->print(OS, Policy);
  1830. if (Node->hasTemplateKeyword())
  1831. OS << "template ";
  1832. OS << Node->getMemberNameInfo();
  1833. if (Node->hasExplicitTemplateArgs())
  1834. TemplateSpecializationType::PrintTemplateArgumentList(
  1835. OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
  1836. }
  1837. static const char *getTypeTraitName(TypeTrait TT) {
  1838. switch (TT) {
  1839. #define TYPE_TRAIT_1(Spelling, Name, Key) \
  1840. case clang::UTT_##Name: return #Spelling;
  1841. #define TYPE_TRAIT_2(Spelling, Name, Key) \
  1842. case clang::BTT_##Name: return #Spelling;
  1843. #define TYPE_TRAIT_N(Spelling, Name, Key) \
  1844. case clang::TT_##Name: return #Spelling;
  1845. #include "clang/Basic/TokenKinds.def"
  1846. }
  1847. llvm_unreachable("Type trait not covered by switch");
  1848. }
  1849. static const char *getTypeTraitName(ArrayTypeTrait ATT) {
  1850. switch (ATT) {
  1851. case ATT_ArrayRank: return "__array_rank";
  1852. case ATT_ArrayExtent: return "__array_extent";
  1853. }
  1854. llvm_unreachable("Array type trait not covered by switch");
  1855. }
  1856. static const char *getExpressionTraitName(ExpressionTrait ET) {
  1857. switch (ET) {
  1858. case ET_IsLValueExpr: return "__is_lvalue_expr";
  1859. case ET_IsRValueExpr: return "__is_rvalue_expr";
  1860. }
  1861. llvm_unreachable("Expression type trait not covered by switch");
  1862. }
  1863. void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
  1864. OS << getTypeTraitName(E->getTrait()) << "(";
  1865. for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
  1866. if (I > 0)
  1867. OS << ", ";
  1868. E->getArg(I)->getType().print(OS, Policy);
  1869. }
  1870. OS << ")";
  1871. }
  1872. void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
  1873. OS << getTypeTraitName(E->getTrait()) << '(';
  1874. E->getQueriedType().print(OS, Policy);
  1875. OS << ')';
  1876. }
  1877. void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
  1878. OS << getExpressionTraitName(E->getTrait()) << '(';
  1879. PrintExpr(E->getQueriedExpression());
  1880. OS << ')';
  1881. }
  1882. void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
  1883. OS << "noexcept(";
  1884. PrintExpr(E->getOperand());
  1885. OS << ")";
  1886. }
  1887. void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
  1888. PrintExpr(E->getPattern());
  1889. OS << "...";
  1890. }
  1891. void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
  1892. OS << "sizeof...(" << *E->getPack() << ")";
  1893. }
  1894. void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
  1895. SubstNonTypeTemplateParmPackExpr *Node) {
  1896. OS << *Node->getParameterPack();
  1897. }
  1898. void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
  1899. SubstNonTypeTemplateParmExpr *Node) {
  1900. Visit(Node->getReplacement());
  1901. }
  1902. void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
  1903. OS << *E->getParameterPack();
  1904. }
  1905. void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
  1906. PrintExpr(Node->GetTemporaryExpr());
  1907. }
  1908. void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
  1909. OS << "(";
  1910. if (E->getLHS()) {
  1911. PrintExpr(E->getLHS());
  1912. OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
  1913. }
  1914. OS << "...";
  1915. if (E->getRHS()) {
  1916. OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
  1917. PrintExpr(E->getRHS());
  1918. }
  1919. OS << ")";
  1920. }
  1921. // Obj-C
  1922. void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
  1923. OS << "@";
  1924. VisitStringLiteral(Node->getString());
  1925. }
  1926. void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
  1927. OS << "@";
  1928. Visit(E->getSubExpr());
  1929. }
  1930. void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
  1931. OS << "@[ ";
  1932. StmtRange ch = E->children();
  1933. if (ch.first != ch.second) {
  1934. while (1) {
  1935. Visit(*ch.first);
  1936. ++ch.first;
  1937. if (ch.first == ch.second) break;
  1938. OS << ", ";
  1939. }
  1940. }
  1941. OS << " ]";
  1942. }
  1943. void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
  1944. OS << "@{ ";
  1945. for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
  1946. if (I > 0)
  1947. OS << ", ";
  1948. ObjCDictionaryElement Element = E->getKeyValueElement(I);
  1949. Visit(Element.Key);
  1950. OS << " : ";
  1951. Visit(Element.Value);
  1952. if (Element.isPackExpansion())
  1953. OS << "...";
  1954. }
  1955. OS << " }";
  1956. }
  1957. void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
  1958. OS << "@encode(";
  1959. Node->getEncodedType().print(OS, Policy);
  1960. OS << ')';
  1961. }
  1962. void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
  1963. OS << "@selector(";
  1964. Node->getSelector().print(OS);
  1965. OS << ')';
  1966. }
  1967. void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
  1968. OS << "@protocol(" << *Node->getProtocol() << ')';
  1969. }
  1970. void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
  1971. OS << "[";
  1972. switch (Mess->getReceiverKind()) {
  1973. case ObjCMessageExpr::Instance:
  1974. PrintExpr(Mess->getInstanceReceiver());
  1975. break;
  1976. case ObjCMessageExpr::Class:
  1977. Mess->getClassReceiver().print(OS, Policy);
  1978. break;
  1979. case ObjCMessageExpr::SuperInstance:
  1980. case ObjCMessageExpr::SuperClass:
  1981. OS << "Super";
  1982. break;
  1983. }
  1984. OS << ' ';
  1985. Selector selector = Mess->getSelector();
  1986. if (selector.isUnarySelector()) {
  1987. OS << selector.getNameForSlot(0);
  1988. } else {
  1989. for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
  1990. if (i < selector.getNumArgs()) {
  1991. if (i > 0) OS << ' ';
  1992. if (selector.getIdentifierInfoForSlot(i))
  1993. OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
  1994. else
  1995. OS << ":";
  1996. }
  1997. else OS << ", "; // Handle variadic methods.
  1998. PrintExpr(Mess->getArg(i));
  1999. }
  2000. }
  2001. OS << "]";
  2002. }
  2003. void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
  2004. OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
  2005. }
  2006. void
  2007. StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
  2008. PrintExpr(E->getSubExpr());
  2009. }
  2010. void
  2011. StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
  2012. OS << '(' << E->getBridgeKindName();
  2013. E->getType().print(OS, Policy);
  2014. OS << ')';
  2015. PrintExpr(E->getSubExpr());
  2016. }
  2017. void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
  2018. BlockDecl *BD = Node->getBlockDecl();
  2019. OS << "^";
  2020. const FunctionType *AFT = Node->getFunctionType();
  2021. if (isa<FunctionNoProtoType>(AFT)) {
  2022. OS << "()";
  2023. } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
  2024. OS << '(';
  2025. for (BlockDecl::param_iterator AI = BD->param_begin(),
  2026. E = BD->param_end(); AI != E; ++AI) {
  2027. if (AI != BD->param_begin()) OS << ", ";
  2028. std::string ParamStr = (*AI)->getNameAsString();
  2029. (*AI)->getType().print(OS, Policy, ParamStr);
  2030. }
  2031. const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
  2032. if (FT->isVariadic()) {
  2033. if (!BD->param_empty()) OS << ", ";
  2034. OS << "...";
  2035. }
  2036. OS << ')';
  2037. }
  2038. OS << "{ }";
  2039. }
  2040. void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
  2041. PrintExpr(Node->getSourceExpr());
  2042. }
  2043. void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
  2044. // TODO: Print something reasonable for a TypoExpr, if necessary.
  2045. assert(false && "Cannot print TypoExpr nodes");
  2046. }
  2047. void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
  2048. OS << "__builtin_astype(";
  2049. PrintExpr(Node->getSrcExpr());
  2050. OS << ", ";
  2051. Node->getType().print(OS, Policy);
  2052. OS << ")";
  2053. }
  2054. //===----------------------------------------------------------------------===//
  2055. // Stmt method implementations
  2056. //===----------------------------------------------------------------------===//
  2057. void Stmt::dumpPretty(const ASTContext &Context) const {
  2058. printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
  2059. }
  2060. void Stmt::printPretty(raw_ostream &OS,
  2061. PrinterHelper *Helper,
  2062. const PrintingPolicy &Policy,
  2063. unsigned Indentation) const {
  2064. StmtPrinter P(OS, Helper, Policy, Indentation);
  2065. P.Visit(const_cast<Stmt*>(this));
  2066. }
  2067. //===----------------------------------------------------------------------===//
  2068. // PrinterHelper
  2069. //===----------------------------------------------------------------------===//
  2070. // Implement virtual destructor.
  2071. PrinterHelper::~PrinterHelper() {}