StmtPrinter.cpp 64 KB

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