c-pretty-print.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  1. /* Subroutines common to both C and C++ pretty-printers.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "options.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "stor-layout.h"
  32. #include "attribs.h"
  33. #include "intl.h"
  34. #include "c-pretty-print.h"
  35. #include "tree-pretty-print.h"
  36. #include "tree-iterator.h"
  37. #include "diagnostic.h"
  38. #include "wide-int-print.h"
  39. /* The pretty-printer code is primarily designed to closely follow
  40. (GNU) C and C++ grammars. That is to be contrasted with spaghetti
  41. codes we used to have in the past. Following a structured
  42. approach (preferably the official grammars) is believed to make it
  43. much easier to add extensions and nifty pretty-printing effects that
  44. takes expression or declaration contexts into account. */
  45. #define pp_c_maybe_whitespace(PP) \
  46. do { \
  47. if ((PP)->padding == pp_before) \
  48. pp_c_whitespace (PP); \
  49. } while (0)
  50. /* literal */
  51. static void pp_c_char (c_pretty_printer *, int);
  52. /* postfix-expression */
  53. static void pp_c_initializer_list (c_pretty_printer *, tree);
  54. static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
  55. static void pp_c_additive_expression (c_pretty_printer *, tree);
  56. static void pp_c_shift_expression (c_pretty_printer *, tree);
  57. static void pp_c_relational_expression (c_pretty_printer *, tree);
  58. static void pp_c_equality_expression (c_pretty_printer *, tree);
  59. static void pp_c_and_expression (c_pretty_printer *, tree);
  60. static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
  61. static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
  62. static void pp_c_logical_and_expression (c_pretty_printer *, tree);
  63. /* declarations. */
  64. /* Helper functions. */
  65. void
  66. pp_c_whitespace (c_pretty_printer *pp)
  67. {
  68. pp_space (pp);
  69. pp->padding = pp_none;
  70. }
  71. void
  72. pp_c_left_paren (c_pretty_printer *pp)
  73. {
  74. pp_left_paren (pp);
  75. pp->padding = pp_none;
  76. }
  77. void
  78. pp_c_right_paren (c_pretty_printer *pp)
  79. {
  80. pp_right_paren (pp);
  81. pp->padding = pp_none;
  82. }
  83. void
  84. pp_c_left_brace (c_pretty_printer *pp)
  85. {
  86. pp_left_brace (pp);
  87. pp->padding = pp_none;
  88. }
  89. void
  90. pp_c_right_brace (c_pretty_printer *pp)
  91. {
  92. pp_right_brace (pp);
  93. pp->padding = pp_none;
  94. }
  95. void
  96. pp_c_left_bracket (c_pretty_printer *pp)
  97. {
  98. pp_left_bracket (pp);
  99. pp->padding = pp_none;
  100. }
  101. void
  102. pp_c_right_bracket (c_pretty_printer *pp)
  103. {
  104. pp_right_bracket (pp);
  105. pp->padding = pp_none;
  106. }
  107. void
  108. pp_c_dot (c_pretty_printer *pp)
  109. {
  110. pp_dot (pp);
  111. pp->padding = pp_none;
  112. }
  113. void
  114. pp_c_ampersand (c_pretty_printer *pp)
  115. {
  116. pp_ampersand (pp);
  117. pp->padding = pp_none;
  118. }
  119. void
  120. pp_c_star (c_pretty_printer *pp)
  121. {
  122. pp_star (pp);
  123. pp->padding = pp_none;
  124. }
  125. void
  126. pp_c_arrow (c_pretty_printer *pp)
  127. {
  128. pp_arrow (pp);
  129. pp->padding = pp_none;
  130. }
  131. void
  132. pp_c_semicolon (c_pretty_printer *pp)
  133. {
  134. pp_semicolon (pp);
  135. pp->padding = pp_none;
  136. }
  137. void
  138. pp_c_complement (c_pretty_printer *pp)
  139. {
  140. pp_complement (pp);
  141. pp->padding = pp_none;
  142. }
  143. void
  144. pp_c_exclamation (c_pretty_printer *pp)
  145. {
  146. pp_exclamation (pp);
  147. pp->padding = pp_none;
  148. }
  149. /* Print out the external representation of QUALIFIERS. */
  150. void
  151. pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
  152. {
  153. const char *p = pp_last_position_in_text (pp);
  154. bool previous = false;
  155. if (!qualifiers)
  156. return;
  157. /* The C programming language does not have references, but it is much
  158. simpler to handle those here rather than going through the same
  159. logic in the C++ pretty-printer. */
  160. if (p != NULL && (*p == '*' || *p == '&'))
  161. pp_c_whitespace (pp);
  162. if (qualifiers & TYPE_QUAL_ATOMIC)
  163. {
  164. pp_c_ws_string (pp, "_Atomic");
  165. previous = true;
  166. }
  167. if (qualifiers & TYPE_QUAL_CONST)
  168. {
  169. if (previous)
  170. pp_c_whitespace (pp);
  171. pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
  172. previous = true;
  173. }
  174. if (qualifiers & TYPE_QUAL_VOLATILE)
  175. {
  176. if (previous)
  177. pp_c_whitespace (pp);
  178. pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
  179. previous = true;
  180. }
  181. if (qualifiers & TYPE_QUAL_RESTRICT)
  182. {
  183. if (previous)
  184. pp_c_whitespace (pp);
  185. pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
  186. ? "restrict" : "__restrict__"));
  187. }
  188. }
  189. /* Pretty-print T using the type-cast notation '( type-name )'. */
  190. static void
  191. pp_c_type_cast (c_pretty_printer *pp, tree t)
  192. {
  193. pp_c_left_paren (pp);
  194. pp->type_id (t);
  195. pp_c_right_paren (pp);
  196. }
  197. /* We're about to pretty-print a pointer type as indicated by T.
  198. Output a whitespace, if needed, preparing for subsequent output. */
  199. void
  200. pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
  201. {
  202. if (POINTER_TYPE_P (t))
  203. {
  204. tree pointee = strip_pointer_operator (TREE_TYPE (t));
  205. if (TREE_CODE (pointee) != ARRAY_TYPE
  206. && TREE_CODE (pointee) != FUNCTION_TYPE)
  207. pp_c_whitespace (pp);
  208. }
  209. }
  210. /* Declarations. */
  211. /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
  212. cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
  213. of its type. Take care of possible extensions.
  214. type-qualifier-list:
  215. type-qualifier
  216. type-qualifier-list type-qualifier
  217. type-qualifier:
  218. const
  219. restrict -- C99
  220. __restrict__ -- GNU C
  221. address-space-qualifier -- GNU C
  222. volatile
  223. _Atomic -- C11
  224. address-space-qualifier:
  225. identifier -- GNU C */
  226. void
  227. pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
  228. {
  229. int qualifiers;
  230. if (!t || t == error_mark_node)
  231. return;
  232. if (!TYPE_P (t))
  233. t = TREE_TYPE (t);
  234. qualifiers = TYPE_QUALS (t);
  235. pp_c_cv_qualifiers (pp, qualifiers,
  236. TREE_CODE (t) == FUNCTION_TYPE);
  237. if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
  238. {
  239. const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
  240. pp_c_identifier (pp, as);
  241. }
  242. }
  243. /* pointer:
  244. * type-qualifier-list(opt)
  245. * type-qualifier-list(opt) pointer */
  246. static void
  247. pp_c_pointer (c_pretty_printer *pp, tree t)
  248. {
  249. if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
  250. t = TREE_TYPE (t);
  251. switch (TREE_CODE (t))
  252. {
  253. case POINTER_TYPE:
  254. /* It is easier to handle C++ reference types here. */
  255. case REFERENCE_TYPE:
  256. if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
  257. pp_c_pointer (pp, TREE_TYPE (t));
  258. if (TREE_CODE (t) == POINTER_TYPE)
  259. pp_c_star (pp);
  260. else
  261. pp_c_ampersand (pp);
  262. pp_c_type_qualifier_list (pp, t);
  263. break;
  264. /* ??? This node is now in GENERIC and so shouldn't be here. But
  265. we'll fix that later. */
  266. case DECL_EXPR:
  267. pp->declaration (DECL_EXPR_DECL (t));
  268. pp_needs_newline (pp) = true;
  269. break;
  270. default:
  271. pp_unsupported_tree (pp, t);
  272. }
  273. }
  274. /* simple-type-specifier:
  275. type-specifier
  276. type-specifier:
  277. void
  278. char
  279. short
  280. int
  281. long
  282. float
  283. double
  284. signed
  285. unsigned
  286. _Bool -- C99
  287. _Complex -- C99
  288. _Imaginary -- C99
  289. struct-or-union-specifier
  290. enum-specifier
  291. typedef-name.
  292. GNU extensions.
  293. simple-type-specifier:
  294. __complex__
  295. __vector__ */
  296. void
  297. c_pretty_printer::simple_type_specifier (tree t)
  298. {
  299. const enum tree_code code = TREE_CODE (t);
  300. switch (code)
  301. {
  302. case ERROR_MARK:
  303. translate_string ("<type-error>");
  304. break;
  305. case IDENTIFIER_NODE:
  306. pp_c_identifier (this, IDENTIFIER_POINTER (t));
  307. break;
  308. case VOID_TYPE:
  309. case BOOLEAN_TYPE:
  310. case INTEGER_TYPE:
  311. case REAL_TYPE:
  312. case FIXED_POINT_TYPE:
  313. if (TYPE_NAME (t))
  314. {
  315. t = TYPE_NAME (t);
  316. simple_type_specifier (t);
  317. }
  318. else
  319. {
  320. int prec = TYPE_PRECISION (t);
  321. if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
  322. t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
  323. else
  324. t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
  325. if (TYPE_NAME (t))
  326. {
  327. simple_type_specifier (t);
  328. if (TYPE_PRECISION (t) != prec)
  329. {
  330. pp_colon (this);
  331. pp_decimal_int (this, prec);
  332. }
  333. }
  334. else
  335. {
  336. switch (code)
  337. {
  338. case INTEGER_TYPE:
  339. translate_string (TYPE_UNSIGNED (t)
  340. ? "<unnamed-unsigned:"
  341. : "<unnamed-signed:");
  342. break;
  343. case REAL_TYPE:
  344. translate_string ("<unnamed-float:");
  345. break;
  346. case FIXED_POINT_TYPE:
  347. translate_string ("<unnamed-fixed:");
  348. break;
  349. default:
  350. gcc_unreachable ();
  351. }
  352. pp_decimal_int (this, prec);
  353. pp_greater (this);
  354. }
  355. }
  356. break;
  357. case TYPE_DECL:
  358. if (DECL_NAME (t))
  359. id_expression (t);
  360. else
  361. translate_string ("<typedef-error>");
  362. break;
  363. case UNION_TYPE:
  364. case RECORD_TYPE:
  365. case ENUMERAL_TYPE:
  366. if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  367. /* Don't decorate the type if this is a typedef name. */;
  368. else if (code == UNION_TYPE)
  369. pp_c_ws_string (this, "union");
  370. else if (code == RECORD_TYPE)
  371. pp_c_ws_string (this, "struct");
  372. else if (code == ENUMERAL_TYPE)
  373. pp_c_ws_string (this, "enum");
  374. else
  375. translate_string ("<tag-error>");
  376. if (TYPE_NAME (t))
  377. id_expression (TYPE_NAME (t));
  378. else
  379. translate_string ("<anonymous>");
  380. break;
  381. default:
  382. pp_unsupported_tree (this, t);
  383. break;
  384. }
  385. }
  386. /* specifier-qualifier-list:
  387. type-specifier specifier-qualifier-list-opt
  388. type-qualifier specifier-qualifier-list-opt
  389. Implementation note: Because of the non-linearities in array or
  390. function declarations, this routine prints not just the
  391. specifier-qualifier-list of such entities or types of such entities,
  392. but also the 'pointer' production part of their declarators. The
  393. remaining part is done by declarator() or abstract_declarator(). */
  394. void
  395. pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
  396. {
  397. const enum tree_code code = TREE_CODE (t);
  398. if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
  399. pp_c_type_qualifier_list (pp, t);
  400. switch (code)
  401. {
  402. case REFERENCE_TYPE:
  403. case POINTER_TYPE:
  404. {
  405. /* Get the types-specifier of this type. */
  406. tree pointee = strip_pointer_operator (TREE_TYPE (t));
  407. pp_c_specifier_qualifier_list (pp, pointee);
  408. if (TREE_CODE (pointee) == ARRAY_TYPE
  409. || TREE_CODE (pointee) == FUNCTION_TYPE)
  410. {
  411. pp_c_whitespace (pp);
  412. pp_c_left_paren (pp);
  413. pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
  414. }
  415. else if (!c_dialect_cxx ())
  416. pp_c_whitespace (pp);
  417. pp_ptr_operator (pp, t);
  418. }
  419. break;
  420. case FUNCTION_TYPE:
  421. case ARRAY_TYPE:
  422. pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
  423. break;
  424. case VECTOR_TYPE:
  425. case COMPLEX_TYPE:
  426. if (code == COMPLEX_TYPE)
  427. pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
  428. ? "_Complex" : "__complex__"));
  429. else if (code == VECTOR_TYPE)
  430. {
  431. pp_c_ws_string (pp, "__vector");
  432. pp_c_left_paren (pp);
  433. pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
  434. pp_c_right_paren (pp);
  435. pp_c_whitespace (pp);
  436. }
  437. pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
  438. break;
  439. default:
  440. pp->simple_type_specifier (t);
  441. break;
  442. }
  443. if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
  444. pp_c_type_qualifier_list (pp, t);
  445. }
  446. /* parameter-type-list:
  447. parameter-list
  448. parameter-list , ...
  449. parameter-list:
  450. parameter-declaration
  451. parameter-list , parameter-declaration
  452. parameter-declaration:
  453. declaration-specifiers declarator
  454. declaration-specifiers abstract-declarator(opt) */
  455. void
  456. pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
  457. {
  458. bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
  459. tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
  460. pp_c_left_paren (pp);
  461. if (parms == void_list_node)
  462. pp_c_ws_string (pp, "void");
  463. else
  464. {
  465. bool first = true;
  466. for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
  467. {
  468. if (!first)
  469. pp_separate_with (pp, ',');
  470. first = false;
  471. pp->declaration_specifiers
  472. (want_parm_decl ? parms : TREE_VALUE (parms));
  473. if (want_parm_decl)
  474. pp->declarator (parms);
  475. else
  476. pp->abstract_declarator (TREE_VALUE (parms));
  477. }
  478. }
  479. pp_c_right_paren (pp);
  480. }
  481. /* abstract-declarator:
  482. pointer
  483. pointer(opt) direct-abstract-declarator */
  484. void
  485. c_pretty_printer::abstract_declarator (tree t)
  486. {
  487. if (TREE_CODE (t) == POINTER_TYPE)
  488. {
  489. if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
  490. || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
  491. pp_c_right_paren (this);
  492. t = TREE_TYPE (t);
  493. }
  494. direct_abstract_declarator (t);
  495. }
  496. /* direct-abstract-declarator:
  497. ( abstract-declarator )
  498. direct-abstract-declarator(opt) [ assignment-expression(opt) ]
  499. direct-abstract-declarator(opt) [ * ]
  500. direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
  501. void
  502. c_pretty_printer::direct_abstract_declarator (tree t)
  503. {
  504. switch (TREE_CODE (t))
  505. {
  506. case POINTER_TYPE:
  507. abstract_declarator (t);
  508. break;
  509. case FUNCTION_TYPE:
  510. pp_c_parameter_type_list (this, t);
  511. direct_abstract_declarator (TREE_TYPE (t));
  512. break;
  513. case ARRAY_TYPE:
  514. pp_c_left_bracket (this);
  515. if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
  516. {
  517. tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
  518. tree type = TREE_TYPE (maxval);
  519. if (tree_fits_shwi_p (maxval))
  520. pp_wide_integer (this, tree_to_shwi (maxval) + 1);
  521. else
  522. expression (fold_build2 (PLUS_EXPR, type, maxval,
  523. build_int_cst (type, 1)));
  524. }
  525. pp_c_right_bracket (this);
  526. direct_abstract_declarator (TREE_TYPE (t));
  527. break;
  528. case IDENTIFIER_NODE:
  529. case VOID_TYPE:
  530. case BOOLEAN_TYPE:
  531. case INTEGER_TYPE:
  532. case REAL_TYPE:
  533. case FIXED_POINT_TYPE:
  534. case ENUMERAL_TYPE:
  535. case RECORD_TYPE:
  536. case UNION_TYPE:
  537. case VECTOR_TYPE:
  538. case COMPLEX_TYPE:
  539. case TYPE_DECL:
  540. break;
  541. default:
  542. pp_unsupported_tree (this, t);
  543. break;
  544. }
  545. }
  546. /* type-name:
  547. specifier-qualifier-list abstract-declarator(opt) */
  548. void
  549. c_pretty_printer::type_id (tree t)
  550. {
  551. pp_c_specifier_qualifier_list (this, t);
  552. abstract_declarator (t);
  553. }
  554. /* storage-class-specifier:
  555. typedef
  556. extern
  557. static
  558. auto
  559. register */
  560. void
  561. c_pretty_printer::storage_class_specifier (tree t)
  562. {
  563. if (TREE_CODE (t) == TYPE_DECL)
  564. pp_c_ws_string (this, "typedef");
  565. else if (DECL_P (t))
  566. {
  567. if (DECL_REGISTER (t))
  568. pp_c_ws_string (this, "register");
  569. else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
  570. pp_c_ws_string (this, "static");
  571. }
  572. }
  573. /* function-specifier:
  574. inline */
  575. void
  576. c_pretty_printer::function_specifier (tree t)
  577. {
  578. if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
  579. pp_c_ws_string (this, "inline");
  580. }
  581. /* declaration-specifiers:
  582. storage-class-specifier declaration-specifiers(opt)
  583. type-specifier declaration-specifiers(opt)
  584. type-qualifier declaration-specifiers(opt)
  585. function-specifier declaration-specifiers(opt) */
  586. void
  587. c_pretty_printer::declaration_specifiers (tree t)
  588. {
  589. storage_class_specifier (t);
  590. function_specifier (t);
  591. pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
  592. }
  593. /* direct-declarator
  594. identifier
  595. ( declarator )
  596. direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
  597. direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
  598. direct-declarator [ type-qualifier-list static assignment-expression ]
  599. direct-declarator [ type-qualifier-list * ]
  600. direct-declarator ( parameter-type-list )
  601. direct-declarator ( identifier-list(opt) ) */
  602. void
  603. c_pretty_printer::direct_declarator (tree t)
  604. {
  605. switch (TREE_CODE (t))
  606. {
  607. case VAR_DECL:
  608. case PARM_DECL:
  609. case TYPE_DECL:
  610. case FIELD_DECL:
  611. case LABEL_DECL:
  612. pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
  613. pp_c_tree_decl_identifier (this, t);
  614. break;
  615. case ARRAY_TYPE:
  616. case POINTER_TYPE:
  617. abstract_declarator (TREE_TYPE (t));
  618. break;
  619. case FUNCTION_TYPE:
  620. pp_parameter_list (this, t);
  621. abstract_declarator (TREE_TYPE (t));
  622. break;
  623. case FUNCTION_DECL:
  624. pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
  625. pp_c_tree_decl_identifier (this, t);
  626. if (flags & pp_c_flag_abstract)
  627. abstract_declarator (TREE_TYPE (t));
  628. else
  629. {
  630. pp_parameter_list (this, t);
  631. abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
  632. }
  633. break;
  634. case INTEGER_TYPE:
  635. case REAL_TYPE:
  636. case FIXED_POINT_TYPE:
  637. case ENUMERAL_TYPE:
  638. case UNION_TYPE:
  639. case RECORD_TYPE:
  640. break;
  641. default:
  642. pp_unsupported_tree (this, t);
  643. break;
  644. }
  645. }
  646. /* declarator:
  647. pointer(opt) direct-declarator */
  648. void
  649. c_pretty_printer::declarator (tree t)
  650. {
  651. switch (TREE_CODE (t))
  652. {
  653. case INTEGER_TYPE:
  654. case REAL_TYPE:
  655. case FIXED_POINT_TYPE:
  656. case ENUMERAL_TYPE:
  657. case UNION_TYPE:
  658. case RECORD_TYPE:
  659. break;
  660. case VAR_DECL:
  661. case PARM_DECL:
  662. case FIELD_DECL:
  663. case ARRAY_TYPE:
  664. case FUNCTION_TYPE:
  665. case FUNCTION_DECL:
  666. case TYPE_DECL:
  667. direct_declarator (t);
  668. break;
  669. default:
  670. pp_unsupported_tree (this, t);
  671. break;
  672. }
  673. }
  674. /* declaration:
  675. declaration-specifiers init-declarator-list(opt) ; */
  676. void
  677. c_pretty_printer::declaration (tree t)
  678. {
  679. declaration_specifiers (t);
  680. pp_c_init_declarator (this, t);
  681. }
  682. /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
  683. void
  684. pp_c_attributes (c_pretty_printer *pp, tree attributes)
  685. {
  686. if (attributes == NULL_TREE)
  687. return;
  688. pp_c_ws_string (pp, "__attribute__");
  689. pp_c_left_paren (pp);
  690. pp_c_left_paren (pp);
  691. for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
  692. {
  693. pp_tree_identifier (pp, TREE_PURPOSE (attributes));
  694. if (TREE_VALUE (attributes))
  695. pp_c_call_argument_list (pp, TREE_VALUE (attributes));
  696. if (TREE_CHAIN (attributes))
  697. pp_separate_with (pp, ',');
  698. }
  699. pp_c_right_paren (pp);
  700. pp_c_right_paren (pp);
  701. }
  702. /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
  703. marked to be displayed on disgnostic. */
  704. void
  705. pp_c_attributes_display (c_pretty_printer *pp, tree a)
  706. {
  707. bool is_first = true;
  708. if (a == NULL_TREE)
  709. return;
  710. for (; a != NULL_TREE; a = TREE_CHAIN (a))
  711. {
  712. const struct attribute_spec *as;
  713. as = lookup_attribute_spec (TREE_PURPOSE (a));
  714. if (!as || as->affects_type_identity == false)
  715. continue;
  716. if (is_first)
  717. {
  718. pp_c_ws_string (pp, "__attribute__");
  719. pp_c_left_paren (pp);
  720. pp_c_left_paren (pp);
  721. is_first = false;
  722. }
  723. else
  724. {
  725. pp_separate_with (pp, ',');
  726. }
  727. pp_tree_identifier (pp, TREE_PURPOSE (a));
  728. if (TREE_VALUE (a))
  729. pp_c_call_argument_list (pp, TREE_VALUE (a));
  730. }
  731. if (!is_first)
  732. {
  733. pp_c_right_paren (pp);
  734. pp_c_right_paren (pp);
  735. pp_c_whitespace (pp);
  736. }
  737. }
  738. /* function-definition:
  739. declaration-specifiers declarator compound-statement */
  740. void
  741. pp_c_function_definition (c_pretty_printer *pp, tree t)
  742. {
  743. pp->declaration_specifiers (t);
  744. pp->declarator (t);
  745. pp_needs_newline (pp) = true;
  746. pp->statement (DECL_SAVED_TREE (t));
  747. pp_newline_and_flush (pp);
  748. }
  749. /* Expressions. */
  750. /* Print out a c-char. This is called solely for characters which are
  751. in the *target* execution character set. We ought to convert them
  752. back to the *host* execution character set before printing, but we
  753. have no way to do this at present. A decent compromise is to print
  754. all characters as if they were in the host execution character set,
  755. and not attempt to recover any named escape characters, but render
  756. all unprintables as octal escapes. If the host and target character
  757. sets are the same, this produces relatively readable output. If they
  758. are not the same, strings may appear as gibberish, but that's okay
  759. (in fact, it may well be what the reader wants, e.g. if they are looking
  760. to see if conversion to the target character set happened correctly).
  761. A special case: we need to prefix \, ", and ' with backslashes. It is
  762. correct to do so for the *host*'s \, ", and ', because the rest of the
  763. file appears in the host character set. */
  764. static void
  765. pp_c_char (c_pretty_printer *pp, int c)
  766. {
  767. if (ISPRINT (c))
  768. {
  769. switch (c)
  770. {
  771. case '\\': pp_string (pp, "\\\\"); break;
  772. case '\'': pp_string (pp, "\\\'"); break;
  773. case '\"': pp_string (pp, "\\\""); break;
  774. default: pp_character (pp, c);
  775. }
  776. }
  777. else
  778. pp_scalar (pp, "\\%03o", (unsigned) c);
  779. }
  780. /* Print out a STRING literal. */
  781. void
  782. pp_c_string_literal (c_pretty_printer *pp, tree s)
  783. {
  784. const char *p = TREE_STRING_POINTER (s);
  785. int n = TREE_STRING_LENGTH (s) - 1;
  786. int i;
  787. pp_doublequote (pp);
  788. for (i = 0; i < n; ++i)
  789. pp_c_char (pp, p[i]);
  790. pp_doublequote (pp);
  791. }
  792. /* Pretty-print a VOID_CST (void_node). */
  793. static void
  794. pp_c_void_constant (c_pretty_printer *pp)
  795. {
  796. pp_c_type_cast (pp, void_type_node);
  797. pp_string (pp, "0");
  798. }
  799. /* Pretty-print an INTEGER literal. */
  800. static void
  801. pp_c_integer_constant (c_pretty_printer *pp, tree i)
  802. {
  803. int idx;
  804. /* We are going to compare the type of I to other types using
  805. pointer comparison so we need to use its canonical type. */
  806. tree type =
  807. TYPE_CANONICAL (TREE_TYPE (i))
  808. ? TYPE_CANONICAL (TREE_TYPE (i))
  809. : TREE_TYPE (i);
  810. if (tree_fits_shwi_p (i))
  811. pp_wide_integer (pp, tree_to_shwi (i));
  812. else if (tree_fits_uhwi_p (i))
  813. pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
  814. else
  815. {
  816. wide_int wi = i;
  817. if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
  818. {
  819. pp_minus (pp);
  820. wi = -wi;
  821. }
  822. print_hex (wi, pp_buffer (pp)->digit_buffer);
  823. pp_string (pp, pp_buffer (pp)->digit_buffer);
  824. }
  825. if (TYPE_UNSIGNED (type))
  826. pp_character (pp, 'u');
  827. if (type == long_integer_type_node || type == long_unsigned_type_node)
  828. pp_character (pp, 'l');
  829. else if (type == long_long_integer_type_node
  830. || type == long_long_unsigned_type_node)
  831. pp_string (pp, "ll");
  832. else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++)
  833. if (int_n_enabled_p[idx])
  834. {
  835. char buf[2+20];
  836. if (type == int_n_trees[idx].signed_type
  837. || type == int_n_trees[idx].unsigned_type)
  838. {
  839. sprintf (buf, "I%d", int_n_data[idx].bitsize);
  840. pp_string (pp, buf);
  841. }
  842. }
  843. }
  844. /* Print out a CHARACTER literal. */
  845. static void
  846. pp_c_character_constant (c_pretty_printer *pp, tree c)
  847. {
  848. pp_quote (pp);
  849. pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
  850. pp_quote (pp);
  851. }
  852. /* Print out a BOOLEAN literal. */
  853. static void
  854. pp_c_bool_constant (c_pretty_printer *pp, tree b)
  855. {
  856. if (b == boolean_false_node)
  857. {
  858. if (c_dialect_cxx ())
  859. pp_c_ws_string (pp, "false");
  860. else if (flag_isoc99)
  861. pp_c_ws_string (pp, "_False");
  862. else
  863. pp_unsupported_tree (pp, b);
  864. }
  865. else if (b == boolean_true_node)
  866. {
  867. if (c_dialect_cxx ())
  868. pp_c_ws_string (pp, "true");
  869. else if (flag_isoc99)
  870. pp_c_ws_string (pp, "_True");
  871. else
  872. pp_unsupported_tree (pp, b);
  873. }
  874. else if (TREE_CODE (b) == INTEGER_CST)
  875. pp_c_integer_constant (pp, b);
  876. else
  877. pp_unsupported_tree (pp, b);
  878. }
  879. /* Attempt to print out an ENUMERATOR. Return true on success. Else return
  880. false; that means the value was obtained by a cast, in which case
  881. print out the type-id part of the cast-expression -- the casted value
  882. is then printed by pp_c_integer_literal. */
  883. static bool
  884. pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
  885. {
  886. bool value_is_named = true;
  887. tree type = TREE_TYPE (e);
  888. tree value;
  889. /* Find the name of this constant. */
  890. for (value = TYPE_VALUES (type);
  891. value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
  892. value = TREE_CHAIN (value))
  893. ;
  894. if (value != NULL_TREE)
  895. pp->id_expression (TREE_PURPOSE (value));
  896. else
  897. {
  898. /* Value must have been cast. */
  899. pp_c_type_cast (pp, type);
  900. value_is_named = false;
  901. }
  902. return value_is_named;
  903. }
  904. /* Print out a REAL value as a decimal-floating-constant. */
  905. static void
  906. pp_c_floating_constant (c_pretty_printer *pp, tree r)
  907. {
  908. const struct real_format *fmt
  909. = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
  910. REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
  911. bool is_decimal = floating_cst.decimal;
  912. /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
  913. log10(2) to 7 significant digits. */
  914. int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
  915. real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
  916. sizeof (pp_buffer (pp)->digit_buffer),
  917. max_digits10, 1);
  918. pp_string (pp, pp_buffer(pp)->digit_buffer);
  919. if (TREE_TYPE (r) == float_type_node)
  920. pp_character (pp, 'f');
  921. else if (TREE_TYPE (r) == long_double_type_node)
  922. pp_character (pp, 'l');
  923. else if (TREE_TYPE (r) == dfloat128_type_node)
  924. pp_string (pp, "dl");
  925. else if (TREE_TYPE (r) == dfloat64_type_node)
  926. pp_string (pp, "dd");
  927. else if (TREE_TYPE (r) == dfloat32_type_node)
  928. pp_string (pp, "df");
  929. }
  930. /* Print out a FIXED value as a decimal-floating-constant. */
  931. static void
  932. pp_c_fixed_constant (c_pretty_printer *pp, tree r)
  933. {
  934. fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
  935. sizeof (pp_buffer (pp)->digit_buffer));
  936. pp_string (pp, pp_buffer(pp)->digit_buffer);
  937. }
  938. /* Pretty-print a compound literal expression. GNU extensions include
  939. vector constants. */
  940. static void
  941. pp_c_compound_literal (c_pretty_printer *pp, tree e)
  942. {
  943. tree type = TREE_TYPE (e);
  944. pp_c_type_cast (pp, type);
  945. switch (TREE_CODE (type))
  946. {
  947. case RECORD_TYPE:
  948. case UNION_TYPE:
  949. case ARRAY_TYPE:
  950. case VECTOR_TYPE:
  951. case COMPLEX_TYPE:
  952. pp_c_brace_enclosed_initializer_list (pp, e);
  953. break;
  954. default:
  955. pp_unsupported_tree (pp, e);
  956. break;
  957. }
  958. }
  959. /* Pretty-print a COMPLEX_EXPR expression. */
  960. static void
  961. pp_c_complex_expr (c_pretty_printer *pp, tree e)
  962. {
  963. /* Handle a few common special cases, otherwise fallback
  964. to printing it as compound literal. */
  965. tree type = TREE_TYPE (e);
  966. tree realexpr = TREE_OPERAND (e, 0);
  967. tree imagexpr = TREE_OPERAND (e, 1);
  968. /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
  969. if (TREE_CODE (realexpr) == NOP_EXPR
  970. && TREE_CODE (imagexpr) == NOP_EXPR
  971. && TREE_TYPE (realexpr) == TREE_TYPE (type)
  972. && TREE_TYPE (imagexpr) == TREE_TYPE (type)
  973. && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
  974. && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
  975. && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
  976. == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
  977. {
  978. pp_c_type_cast (pp, type);
  979. pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
  980. return;
  981. }
  982. /* Cast of an scalar expression to COMPLEX_TYPE. */
  983. if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
  984. && TREE_TYPE (realexpr) == TREE_TYPE (type))
  985. {
  986. pp_c_type_cast (pp, type);
  987. if (TREE_CODE (realexpr) == NOP_EXPR)
  988. realexpr = TREE_OPERAND (realexpr, 0);
  989. pp->expression (realexpr);
  990. return;
  991. }
  992. pp_c_compound_literal (pp, e);
  993. }
  994. /* constant:
  995. integer-constant
  996. floating-constant
  997. fixed-point-constant
  998. enumeration-constant
  999. character-constant */
  1000. void
  1001. c_pretty_printer::constant (tree e)
  1002. {
  1003. const enum tree_code code = TREE_CODE (e);
  1004. switch (code)
  1005. {
  1006. case VOID_CST:
  1007. pp_c_void_constant (this);
  1008. break;
  1009. case INTEGER_CST:
  1010. {
  1011. tree type = TREE_TYPE (e);
  1012. if (type == boolean_type_node)
  1013. pp_c_bool_constant (this, e);
  1014. else if (type == char_type_node)
  1015. pp_c_character_constant (this, e);
  1016. else if (TREE_CODE (type) == ENUMERAL_TYPE
  1017. && pp_c_enumeration_constant (this, e))
  1018. ;
  1019. else
  1020. pp_c_integer_constant (this, e);
  1021. }
  1022. break;
  1023. case REAL_CST:
  1024. pp_c_floating_constant (this, e);
  1025. break;
  1026. case FIXED_CST:
  1027. pp_c_fixed_constant (this, e);
  1028. break;
  1029. case STRING_CST:
  1030. pp_c_string_literal (this, e);
  1031. break;
  1032. case COMPLEX_CST:
  1033. /* Sometimes, we are confused and we think a complex literal
  1034. is a constant. Such thing is a compound literal which
  1035. grammatically belongs to postfix-expr production. */
  1036. pp_c_compound_literal (this, e);
  1037. break;
  1038. default:
  1039. pp_unsupported_tree (this, e);
  1040. break;
  1041. }
  1042. }
  1043. /* Pretty-print a string such as an identifier, without changing its
  1044. encoding, preceded by whitespace is necessary. */
  1045. void
  1046. pp_c_ws_string (c_pretty_printer *pp, const char *str)
  1047. {
  1048. pp_c_maybe_whitespace (pp);
  1049. pp_string (pp, str);
  1050. pp->padding = pp_before;
  1051. }
  1052. void
  1053. c_pretty_printer::translate_string (const char *gmsgid)
  1054. {
  1055. if (pp_translate_identifiers (this))
  1056. pp_c_ws_string (this, _(gmsgid));
  1057. else
  1058. pp_c_ws_string (this, gmsgid);
  1059. }
  1060. /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
  1061. that need converting to the locale encoding, preceded by whitespace
  1062. is necessary. */
  1063. void
  1064. pp_c_identifier (c_pretty_printer *pp, const char *id)
  1065. {
  1066. pp_c_maybe_whitespace (pp);
  1067. pp_identifier (pp, id);
  1068. pp->padding = pp_before;
  1069. }
  1070. /* Pretty-print a C primary-expression.
  1071. primary-expression:
  1072. identifier
  1073. constant
  1074. string-literal
  1075. ( expression ) */
  1076. void
  1077. c_pretty_printer::primary_expression (tree e)
  1078. {
  1079. switch (TREE_CODE (e))
  1080. {
  1081. case VAR_DECL:
  1082. case PARM_DECL:
  1083. case FIELD_DECL:
  1084. case CONST_DECL:
  1085. case FUNCTION_DECL:
  1086. case LABEL_DECL:
  1087. pp_c_tree_decl_identifier (this, e);
  1088. break;
  1089. case IDENTIFIER_NODE:
  1090. pp_c_tree_identifier (this, e);
  1091. break;
  1092. case ERROR_MARK:
  1093. translate_string ("<erroneous-expression>");
  1094. break;
  1095. case RESULT_DECL:
  1096. translate_string ("<return-value>");
  1097. break;
  1098. case VOID_CST:
  1099. case INTEGER_CST:
  1100. case REAL_CST:
  1101. case FIXED_CST:
  1102. case STRING_CST:
  1103. constant (e);
  1104. break;
  1105. case TARGET_EXPR:
  1106. pp_c_ws_string (this, "__builtin_memcpy");
  1107. pp_c_left_paren (this);
  1108. pp_ampersand (this);
  1109. primary_expression (TREE_OPERAND (e, 0));
  1110. pp_separate_with (this, ',');
  1111. pp_ampersand (this);
  1112. initializer (TREE_OPERAND (e, 1));
  1113. if (TREE_OPERAND (e, 2))
  1114. {
  1115. pp_separate_with (this, ',');
  1116. expression (TREE_OPERAND (e, 2));
  1117. }
  1118. pp_c_right_paren (this);
  1119. break;
  1120. default:
  1121. /* FIXME: Make sure we won't get into an infinite loop. */
  1122. pp_c_left_paren (this);
  1123. expression (e);
  1124. pp_c_right_paren (this);
  1125. break;
  1126. }
  1127. }
  1128. /* Print out a C initializer -- also support C compound-literals.
  1129. initializer:
  1130. assignment-expression:
  1131. { initializer-list }
  1132. { initializer-list , } */
  1133. void
  1134. c_pretty_printer::initializer (tree e)
  1135. {
  1136. if (TREE_CODE (e) == CONSTRUCTOR)
  1137. pp_c_brace_enclosed_initializer_list (this, e);
  1138. else
  1139. expression (e);
  1140. }
  1141. /* init-declarator:
  1142. declarator:
  1143. declarator = initializer */
  1144. void
  1145. pp_c_init_declarator (c_pretty_printer *pp, tree t)
  1146. {
  1147. pp->declarator (t);
  1148. /* We don't want to output function definitions here. There are handled
  1149. elsewhere (and the syntactic form is bogus anyway). */
  1150. if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
  1151. {
  1152. tree init = DECL_INITIAL (t);
  1153. /* This C++ bit is handled here because it is easier to do so.
  1154. In templates, the C++ parser builds a TREE_LIST for a
  1155. direct-initialization; the TREE_PURPOSE is the variable to
  1156. initialize and the TREE_VALUE is the initializer. */
  1157. if (TREE_CODE (init) == TREE_LIST)
  1158. {
  1159. pp_c_left_paren (pp);
  1160. pp->expression (TREE_VALUE (init));
  1161. pp_right_paren (pp);
  1162. }
  1163. else
  1164. {
  1165. pp_space (pp);
  1166. pp_equal (pp);
  1167. pp_space (pp);
  1168. pp->initializer (init);
  1169. }
  1170. }
  1171. }
  1172. /* initializer-list:
  1173. designation(opt) initializer
  1174. initializer-list , designation(opt) initializer
  1175. designation:
  1176. designator-list =
  1177. designator-list:
  1178. designator
  1179. designator-list designator
  1180. designator:
  1181. [ constant-expression ]
  1182. identifier */
  1183. static void
  1184. pp_c_initializer_list (c_pretty_printer *pp, tree e)
  1185. {
  1186. tree type = TREE_TYPE (e);
  1187. const enum tree_code code = TREE_CODE (type);
  1188. if (TREE_CODE (e) == CONSTRUCTOR)
  1189. {
  1190. pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
  1191. return;
  1192. }
  1193. switch (code)
  1194. {
  1195. case RECORD_TYPE:
  1196. case UNION_TYPE:
  1197. case ARRAY_TYPE:
  1198. {
  1199. tree init = TREE_OPERAND (e, 0);
  1200. for (; init != NULL_TREE; init = TREE_CHAIN (init))
  1201. {
  1202. if (code == RECORD_TYPE || code == UNION_TYPE)
  1203. {
  1204. pp_c_dot (pp);
  1205. pp->primary_expression (TREE_PURPOSE (init));
  1206. }
  1207. else
  1208. {
  1209. pp_c_left_bracket (pp);
  1210. if (TREE_PURPOSE (init))
  1211. pp->constant (TREE_PURPOSE (init));
  1212. pp_c_right_bracket (pp);
  1213. }
  1214. pp_c_whitespace (pp);
  1215. pp_equal (pp);
  1216. pp_c_whitespace (pp);
  1217. pp->initializer (TREE_VALUE (init));
  1218. if (TREE_CHAIN (init))
  1219. pp_separate_with (pp, ',');
  1220. }
  1221. }
  1222. return;
  1223. case VECTOR_TYPE:
  1224. if (TREE_CODE (e) == VECTOR_CST)
  1225. {
  1226. unsigned i;
  1227. for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
  1228. {
  1229. if (i > 0)
  1230. pp_separate_with (pp, ',');
  1231. pp->expression (VECTOR_CST_ELT (e, i));
  1232. }
  1233. }
  1234. else
  1235. break;
  1236. return;
  1237. case COMPLEX_TYPE:
  1238. if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
  1239. {
  1240. const bool cst = TREE_CODE (e) == COMPLEX_CST;
  1241. pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
  1242. pp_separate_with (pp, ',');
  1243. pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
  1244. }
  1245. else
  1246. break;
  1247. return;
  1248. default:
  1249. break;
  1250. }
  1251. pp_unsupported_tree (pp, type);
  1252. }
  1253. /* Pretty-print a brace-enclosed initializer-list. */
  1254. static void
  1255. pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
  1256. {
  1257. pp_c_left_brace (pp);
  1258. pp_c_initializer_list (pp, l);
  1259. pp_c_right_brace (pp);
  1260. }
  1261. /* This is a convenient function, used to bridge gap between C and C++
  1262. grammars.
  1263. id-expression:
  1264. identifier */
  1265. void
  1266. c_pretty_printer::id_expression (tree t)
  1267. {
  1268. switch (TREE_CODE (t))
  1269. {
  1270. case VAR_DECL:
  1271. case PARM_DECL:
  1272. case CONST_DECL:
  1273. case TYPE_DECL:
  1274. case FUNCTION_DECL:
  1275. case FIELD_DECL:
  1276. case LABEL_DECL:
  1277. pp_c_tree_decl_identifier (this, t);
  1278. break;
  1279. case IDENTIFIER_NODE:
  1280. pp_c_tree_identifier (this, t);
  1281. break;
  1282. default:
  1283. pp_unsupported_tree (this, t);
  1284. break;
  1285. }
  1286. }
  1287. /* postfix-expression:
  1288. primary-expression
  1289. postfix-expression [ expression ]
  1290. postfix-expression ( argument-expression-list(opt) )
  1291. postfix-expression . identifier
  1292. postfix-expression -> identifier
  1293. postfix-expression ++
  1294. postfix-expression --
  1295. ( type-name ) { initializer-list }
  1296. ( type-name ) { initializer-list , } */
  1297. void
  1298. c_pretty_printer::postfix_expression (tree e)
  1299. {
  1300. enum tree_code code = TREE_CODE (e);
  1301. switch (code)
  1302. {
  1303. case POSTINCREMENT_EXPR:
  1304. case POSTDECREMENT_EXPR:
  1305. postfix_expression (TREE_OPERAND (e, 0));
  1306. pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
  1307. break;
  1308. case ARRAY_REF:
  1309. postfix_expression (TREE_OPERAND (e, 0));
  1310. pp_c_left_bracket (this);
  1311. expression (TREE_OPERAND (e, 1));
  1312. pp_c_right_bracket (this);
  1313. break;
  1314. case ARRAY_NOTATION_REF:
  1315. postfix_expression (ARRAY_NOTATION_ARRAY (e));
  1316. pp_c_left_bracket (this);
  1317. expression (ARRAY_NOTATION_START (e));
  1318. pp_colon (this);
  1319. expression (ARRAY_NOTATION_LENGTH (e));
  1320. pp_colon (this);
  1321. expression (ARRAY_NOTATION_STRIDE (e));
  1322. pp_c_right_bracket (this);
  1323. break;
  1324. case CALL_EXPR:
  1325. {
  1326. call_expr_arg_iterator iter;
  1327. tree arg;
  1328. postfix_expression (CALL_EXPR_FN (e));
  1329. pp_c_left_paren (this);
  1330. FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
  1331. {
  1332. expression (arg);
  1333. if (more_call_expr_args_p (&iter))
  1334. pp_separate_with (this, ',');
  1335. }
  1336. pp_c_right_paren (this);
  1337. break;
  1338. }
  1339. case UNORDERED_EXPR:
  1340. pp_c_ws_string (this, flag_isoc99
  1341. ? "isunordered"
  1342. : "__builtin_isunordered");
  1343. goto two_args_fun;
  1344. case ORDERED_EXPR:
  1345. pp_c_ws_string (this, flag_isoc99
  1346. ? "!isunordered"
  1347. : "!__builtin_isunordered");
  1348. goto two_args_fun;
  1349. case UNLT_EXPR:
  1350. pp_c_ws_string (this, flag_isoc99
  1351. ? "!isgreaterequal"
  1352. : "!__builtin_isgreaterequal");
  1353. goto two_args_fun;
  1354. case UNLE_EXPR:
  1355. pp_c_ws_string (this, flag_isoc99
  1356. ? "!isgreater"
  1357. : "!__builtin_isgreater");
  1358. goto two_args_fun;
  1359. case UNGT_EXPR:
  1360. pp_c_ws_string (this, flag_isoc99
  1361. ? "!islessequal"
  1362. : "!__builtin_islessequal");
  1363. goto two_args_fun;
  1364. case UNGE_EXPR:
  1365. pp_c_ws_string (this, flag_isoc99
  1366. ? "!isless"
  1367. : "!__builtin_isless");
  1368. goto two_args_fun;
  1369. case UNEQ_EXPR:
  1370. pp_c_ws_string (this, flag_isoc99
  1371. ? "!islessgreater"
  1372. : "!__builtin_islessgreater");
  1373. goto two_args_fun;
  1374. case LTGT_EXPR:
  1375. pp_c_ws_string (this, flag_isoc99
  1376. ? "islessgreater"
  1377. : "__builtin_islessgreater");
  1378. goto two_args_fun;
  1379. two_args_fun:
  1380. pp_c_left_paren (this);
  1381. expression (TREE_OPERAND (e, 0));
  1382. pp_separate_with (this, ',');
  1383. expression (TREE_OPERAND (e, 1));
  1384. pp_c_right_paren (this);
  1385. break;
  1386. case ABS_EXPR:
  1387. pp_c_ws_string (this, "__builtin_abs");
  1388. pp_c_left_paren (this);
  1389. expression (TREE_OPERAND (e, 0));
  1390. pp_c_right_paren (this);
  1391. break;
  1392. case COMPONENT_REF:
  1393. {
  1394. tree object = TREE_OPERAND (e, 0);
  1395. if (TREE_CODE (object) == INDIRECT_REF)
  1396. {
  1397. postfix_expression (TREE_OPERAND (object, 0));
  1398. pp_c_arrow (this);
  1399. }
  1400. else
  1401. {
  1402. postfix_expression (object);
  1403. pp_c_dot (this);
  1404. }
  1405. expression (TREE_OPERAND (e, 1));
  1406. }
  1407. break;
  1408. case BIT_FIELD_REF:
  1409. {
  1410. tree type = TREE_TYPE (e);
  1411. type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
  1412. if (type
  1413. && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
  1414. {
  1415. HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
  1416. HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
  1417. if ((bitpos % size) == 0)
  1418. {
  1419. pp_c_left_paren (this);
  1420. pp_c_left_paren (this);
  1421. type_id (type);
  1422. pp_c_star (this);
  1423. pp_c_right_paren (this);
  1424. pp_c_ampersand (this);
  1425. expression (TREE_OPERAND (e, 0));
  1426. pp_c_right_paren (this);
  1427. pp_c_left_bracket (this);
  1428. pp_wide_integer (this, bitpos / size);
  1429. pp_c_right_bracket (this);
  1430. break;
  1431. }
  1432. }
  1433. pp_unsupported_tree (this, e);
  1434. }
  1435. break;
  1436. case MEM_REF:
  1437. expression (e);
  1438. break;
  1439. case COMPLEX_CST:
  1440. case VECTOR_CST:
  1441. pp_c_compound_literal (this, e);
  1442. break;
  1443. case COMPLEX_EXPR:
  1444. pp_c_complex_expr (this, e);
  1445. break;
  1446. case COMPOUND_LITERAL_EXPR:
  1447. e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
  1448. /* Fall through. */
  1449. case CONSTRUCTOR:
  1450. initializer (e);
  1451. break;
  1452. case VA_ARG_EXPR:
  1453. pp_c_ws_string (this, "__builtin_va_arg");
  1454. pp_c_left_paren (this);
  1455. assignment_expression (TREE_OPERAND (e, 0));
  1456. pp_separate_with (this, ',');
  1457. type_id (TREE_TYPE (e));
  1458. pp_c_right_paren (this);
  1459. break;
  1460. case ADDR_EXPR:
  1461. if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
  1462. {
  1463. id_expression (TREE_OPERAND (e, 0));
  1464. break;
  1465. }
  1466. /* else fall through. */
  1467. default:
  1468. primary_expression (e);
  1469. break;
  1470. }
  1471. }
  1472. /* Print out an expression-list; E is expected to be a TREE_LIST. */
  1473. void
  1474. pp_c_expression_list (c_pretty_printer *pp, tree e)
  1475. {
  1476. for (; e != NULL_TREE; e = TREE_CHAIN (e))
  1477. {
  1478. pp->expression (TREE_VALUE (e));
  1479. if (TREE_CHAIN (e))
  1480. pp_separate_with (pp, ',');
  1481. }
  1482. }
  1483. /* Print out V, which contains the elements of a constructor. */
  1484. void
  1485. pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
  1486. {
  1487. unsigned HOST_WIDE_INT ix;
  1488. tree value;
  1489. FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
  1490. {
  1491. pp->expression (value);
  1492. if (ix != vec_safe_length (v) - 1)
  1493. pp_separate_with (pp, ',');
  1494. }
  1495. }
  1496. /* Print out an expression-list in parens, as if it were the argument
  1497. list to a function. */
  1498. void
  1499. pp_c_call_argument_list (c_pretty_printer *pp, tree t)
  1500. {
  1501. pp_c_left_paren (pp);
  1502. if (t && TREE_CODE (t) == TREE_LIST)
  1503. pp_c_expression_list (pp, t);
  1504. pp_c_right_paren (pp);
  1505. }
  1506. /* unary-expression:
  1507. postfix-expression
  1508. ++ cast-expression
  1509. -- cast-expression
  1510. unary-operator cast-expression
  1511. sizeof unary-expression
  1512. sizeof ( type-id )
  1513. unary-operator: one of
  1514. * & + - ! ~
  1515. GNU extensions.
  1516. unary-expression:
  1517. __alignof__ unary-expression
  1518. __alignof__ ( type-id )
  1519. __real__ unary-expression
  1520. __imag__ unary-expression */
  1521. void
  1522. c_pretty_printer::unary_expression (tree e)
  1523. {
  1524. enum tree_code code = TREE_CODE (e);
  1525. switch (code)
  1526. {
  1527. case PREINCREMENT_EXPR:
  1528. case PREDECREMENT_EXPR:
  1529. pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
  1530. unary_expression (TREE_OPERAND (e, 0));
  1531. break;
  1532. case ADDR_EXPR:
  1533. case INDIRECT_REF:
  1534. case NEGATE_EXPR:
  1535. case BIT_NOT_EXPR:
  1536. case TRUTH_NOT_EXPR:
  1537. case CONJ_EXPR:
  1538. /* String literal are used by address. */
  1539. if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
  1540. pp_ampersand (this);
  1541. else if (code == INDIRECT_REF)
  1542. pp_c_star (this);
  1543. else if (code == NEGATE_EXPR)
  1544. pp_minus (this);
  1545. else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
  1546. pp_complement (this);
  1547. else if (code == TRUTH_NOT_EXPR)
  1548. pp_exclamation (this);
  1549. pp_c_cast_expression (this, TREE_OPERAND (e, 0));
  1550. break;
  1551. case MEM_REF:
  1552. if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
  1553. && integer_zerop (TREE_OPERAND (e, 1)))
  1554. expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
  1555. else
  1556. {
  1557. pp_c_star (this);
  1558. if (!integer_zerop (TREE_OPERAND (e, 1)))
  1559. {
  1560. pp_c_left_paren (this);
  1561. if (!integer_onep (TYPE_SIZE_UNIT
  1562. (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
  1563. pp_c_type_cast (this, ptr_type_node);
  1564. }
  1565. pp_c_cast_expression (this, TREE_OPERAND (e, 0));
  1566. if (!integer_zerop (TREE_OPERAND (e, 1)))
  1567. {
  1568. pp_plus (this);
  1569. pp_c_integer_constant (this,
  1570. fold_convert (ssizetype,
  1571. TREE_OPERAND (e, 1)));
  1572. pp_c_right_paren (this);
  1573. }
  1574. }
  1575. break;
  1576. case REALPART_EXPR:
  1577. case IMAGPART_EXPR:
  1578. pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
  1579. pp_c_whitespace (this);
  1580. unary_expression (TREE_OPERAND (e, 0));
  1581. break;
  1582. default:
  1583. postfix_expression (e);
  1584. break;
  1585. }
  1586. }
  1587. /* cast-expression:
  1588. unary-expression
  1589. ( type-name ) cast-expression */
  1590. void
  1591. pp_c_cast_expression (c_pretty_printer *pp, tree e)
  1592. {
  1593. switch (TREE_CODE (e))
  1594. {
  1595. case FLOAT_EXPR:
  1596. case FIX_TRUNC_EXPR:
  1597. CASE_CONVERT:
  1598. case VIEW_CONVERT_EXPR:
  1599. pp_c_type_cast (pp, TREE_TYPE (e));
  1600. pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
  1601. break;
  1602. default:
  1603. pp->unary_expression (e);
  1604. }
  1605. }
  1606. /* multiplicative-expression:
  1607. cast-expression
  1608. multiplicative-expression * cast-expression
  1609. multiplicative-expression / cast-expression
  1610. multiplicative-expression % cast-expression */
  1611. void
  1612. c_pretty_printer::multiplicative_expression (tree e)
  1613. {
  1614. enum tree_code code = TREE_CODE (e);
  1615. switch (code)
  1616. {
  1617. case MULT_EXPR:
  1618. case TRUNC_DIV_EXPR:
  1619. case TRUNC_MOD_EXPR:
  1620. multiplicative_expression (TREE_OPERAND (e, 0));
  1621. pp_c_whitespace (this);
  1622. if (code == MULT_EXPR)
  1623. pp_c_star (this);
  1624. else if (code == TRUNC_DIV_EXPR)
  1625. pp_slash (this);
  1626. else
  1627. pp_modulo (this);
  1628. pp_c_whitespace (this);
  1629. pp_c_cast_expression (this, TREE_OPERAND (e, 1));
  1630. break;
  1631. default:
  1632. pp_c_cast_expression (this, e);
  1633. break;
  1634. }
  1635. }
  1636. /* additive-expression:
  1637. multiplicative-expression
  1638. additive-expression + multiplicative-expression
  1639. additive-expression - multiplicative-expression */
  1640. static void
  1641. pp_c_additive_expression (c_pretty_printer *pp, tree e)
  1642. {
  1643. enum tree_code code = TREE_CODE (e);
  1644. switch (code)
  1645. {
  1646. case POINTER_PLUS_EXPR:
  1647. case PLUS_EXPR:
  1648. case MINUS_EXPR:
  1649. pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
  1650. pp_c_whitespace (pp);
  1651. if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
  1652. pp_plus (pp);
  1653. else
  1654. pp_minus (pp);
  1655. pp_c_whitespace (pp);
  1656. pp->multiplicative_expression (TREE_OPERAND (e, 1));
  1657. break;
  1658. default:
  1659. pp->multiplicative_expression (e);
  1660. break;
  1661. }
  1662. }
  1663. /* additive-expression:
  1664. additive-expression
  1665. shift-expression << additive-expression
  1666. shift-expression >> additive-expression */
  1667. static void
  1668. pp_c_shift_expression (c_pretty_printer *pp, tree e)
  1669. {
  1670. enum tree_code code = TREE_CODE (e);
  1671. switch (code)
  1672. {
  1673. case LSHIFT_EXPR:
  1674. case RSHIFT_EXPR:
  1675. pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
  1676. pp_c_whitespace (pp);
  1677. pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
  1678. pp_c_whitespace (pp);
  1679. pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
  1680. break;
  1681. default:
  1682. pp_c_additive_expression (pp, e);
  1683. }
  1684. }
  1685. /* relational-expression:
  1686. shift-expression
  1687. relational-expression < shift-expression
  1688. relational-expression > shift-expression
  1689. relational-expression <= shift-expression
  1690. relational-expression >= shift-expression */
  1691. static void
  1692. pp_c_relational_expression (c_pretty_printer *pp, tree e)
  1693. {
  1694. enum tree_code code = TREE_CODE (e);
  1695. switch (code)
  1696. {
  1697. case LT_EXPR:
  1698. case GT_EXPR:
  1699. case LE_EXPR:
  1700. case GE_EXPR:
  1701. pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
  1702. pp_c_whitespace (pp);
  1703. if (code == LT_EXPR)
  1704. pp_less (pp);
  1705. else if (code == GT_EXPR)
  1706. pp_greater (pp);
  1707. else if (code == LE_EXPR)
  1708. pp_less_equal (pp);
  1709. else if (code == GE_EXPR)
  1710. pp_greater_equal (pp);
  1711. pp_c_whitespace (pp);
  1712. pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
  1713. break;
  1714. default:
  1715. pp_c_shift_expression (pp, e);
  1716. break;
  1717. }
  1718. }
  1719. /* equality-expression:
  1720. relational-expression
  1721. equality-expression == relational-expression
  1722. equality-equality != relational-expression */
  1723. static void
  1724. pp_c_equality_expression (c_pretty_printer *pp, tree e)
  1725. {
  1726. enum tree_code code = TREE_CODE (e);
  1727. switch (code)
  1728. {
  1729. case EQ_EXPR:
  1730. case NE_EXPR:
  1731. pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
  1732. pp_c_whitespace (pp);
  1733. pp_string (pp, code == EQ_EXPR ? "==" : "!=");
  1734. pp_c_whitespace (pp);
  1735. pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
  1736. break;
  1737. default:
  1738. pp_c_relational_expression (pp, e);
  1739. break;
  1740. }
  1741. }
  1742. /* AND-expression:
  1743. equality-expression
  1744. AND-expression & equality-equality */
  1745. static void
  1746. pp_c_and_expression (c_pretty_printer *pp, tree e)
  1747. {
  1748. if (TREE_CODE (e) == BIT_AND_EXPR)
  1749. {
  1750. pp_c_and_expression (pp, TREE_OPERAND (e, 0));
  1751. pp_c_whitespace (pp);
  1752. pp_ampersand (pp);
  1753. pp_c_whitespace (pp);
  1754. pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
  1755. }
  1756. else
  1757. pp_c_equality_expression (pp, e);
  1758. }
  1759. /* exclusive-OR-expression:
  1760. AND-expression
  1761. exclusive-OR-expression ^ AND-expression */
  1762. static void
  1763. pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
  1764. {
  1765. if (TREE_CODE (e) == BIT_XOR_EXPR
  1766. || TREE_CODE (e) == TRUTH_XOR_EXPR)
  1767. {
  1768. pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
  1769. if (TREE_CODE (e) == BIT_XOR_EXPR)
  1770. pp_c_maybe_whitespace (pp);
  1771. else
  1772. pp_c_whitespace (pp);
  1773. pp_carret (pp);
  1774. pp_c_whitespace (pp);
  1775. pp_c_and_expression (pp, TREE_OPERAND (e, 1));
  1776. }
  1777. else
  1778. pp_c_and_expression (pp, e);
  1779. }
  1780. /* inclusive-OR-expression:
  1781. exclusive-OR-expression
  1782. inclusive-OR-expression | exclusive-OR-expression */
  1783. static void
  1784. pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
  1785. {
  1786. if (TREE_CODE (e) == BIT_IOR_EXPR)
  1787. {
  1788. pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
  1789. pp_c_whitespace (pp);
  1790. pp_bar (pp);
  1791. pp_c_whitespace (pp);
  1792. pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
  1793. }
  1794. else
  1795. pp_c_exclusive_or_expression (pp, e);
  1796. }
  1797. /* logical-AND-expression:
  1798. inclusive-OR-expression
  1799. logical-AND-expression && inclusive-OR-expression */
  1800. static void
  1801. pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
  1802. {
  1803. if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
  1804. || TREE_CODE (e) == TRUTH_AND_EXPR)
  1805. {
  1806. pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
  1807. pp_c_whitespace (pp);
  1808. pp_ampersand_ampersand (pp);
  1809. pp_c_whitespace (pp);
  1810. pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
  1811. }
  1812. else
  1813. pp_c_inclusive_or_expression (pp, e);
  1814. }
  1815. /* logical-OR-expression:
  1816. logical-AND-expression
  1817. logical-OR-expression || logical-AND-expression */
  1818. void
  1819. pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
  1820. {
  1821. if (TREE_CODE (e) == TRUTH_ORIF_EXPR
  1822. || TREE_CODE (e) == TRUTH_OR_EXPR)
  1823. {
  1824. pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
  1825. pp_c_whitespace (pp);
  1826. pp_bar_bar (pp);
  1827. pp_c_whitespace (pp);
  1828. pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
  1829. }
  1830. else
  1831. pp_c_logical_and_expression (pp, e);
  1832. }
  1833. /* conditional-expression:
  1834. logical-OR-expression
  1835. logical-OR-expression ? expression : conditional-expression */
  1836. void
  1837. c_pretty_printer::conditional_expression (tree e)
  1838. {
  1839. if (TREE_CODE (e) == COND_EXPR)
  1840. {
  1841. pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
  1842. pp_c_whitespace (this);
  1843. pp_question (this);
  1844. pp_c_whitespace (this);
  1845. expression (TREE_OPERAND (e, 1));
  1846. pp_c_whitespace (this);
  1847. pp_colon (this);
  1848. pp_c_whitespace (this);
  1849. conditional_expression (TREE_OPERAND (e, 2));
  1850. }
  1851. else
  1852. pp_c_logical_or_expression (this, e);
  1853. }
  1854. /* assignment-expression:
  1855. conditional-expression
  1856. unary-expression assignment-operator assignment-expression
  1857. assignment-expression: one of
  1858. = *= /= %= += -= >>= <<= &= ^= |= */
  1859. void
  1860. c_pretty_printer::assignment_expression (tree e)
  1861. {
  1862. if (TREE_CODE (e) == MODIFY_EXPR
  1863. || TREE_CODE (e) == INIT_EXPR)
  1864. {
  1865. unary_expression (TREE_OPERAND (e, 0));
  1866. pp_c_whitespace (this);
  1867. pp_equal (this);
  1868. pp_space (this);
  1869. expression (TREE_OPERAND (e, 1));
  1870. }
  1871. else
  1872. conditional_expression (e);
  1873. }
  1874. /* expression:
  1875. assignment-expression
  1876. expression , assignment-expression
  1877. Implementation note: instead of going through the usual recursion
  1878. chain, I take the liberty of dispatching nodes to the appropriate
  1879. functions. This makes some redundancy, but it worths it. That also
  1880. prevents a possible infinite recursion between primary_expression ()
  1881. and expression (). */
  1882. void
  1883. c_pretty_printer::expression (tree e)
  1884. {
  1885. switch (TREE_CODE (e))
  1886. {
  1887. case VOID_CST:
  1888. pp_c_void_constant (this);
  1889. break;
  1890. case INTEGER_CST:
  1891. pp_c_integer_constant (this, e);
  1892. break;
  1893. case REAL_CST:
  1894. pp_c_floating_constant (this, e);
  1895. break;
  1896. case FIXED_CST:
  1897. pp_c_fixed_constant (this, e);
  1898. break;
  1899. case STRING_CST:
  1900. pp_c_string_literal (this, e);
  1901. break;
  1902. case IDENTIFIER_NODE:
  1903. case FUNCTION_DECL:
  1904. case VAR_DECL:
  1905. case CONST_DECL:
  1906. case PARM_DECL:
  1907. case RESULT_DECL:
  1908. case FIELD_DECL:
  1909. case LABEL_DECL:
  1910. case ERROR_MARK:
  1911. primary_expression (e);
  1912. break;
  1913. case SSA_NAME:
  1914. if (SSA_NAME_VAR (e)
  1915. && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
  1916. expression (SSA_NAME_VAR (e));
  1917. else
  1918. translate_string ("<unknown>");
  1919. break;
  1920. case POSTINCREMENT_EXPR:
  1921. case POSTDECREMENT_EXPR:
  1922. case ARRAY_REF:
  1923. case ARRAY_NOTATION_REF:
  1924. case CALL_EXPR:
  1925. case COMPONENT_REF:
  1926. case BIT_FIELD_REF:
  1927. case COMPLEX_CST:
  1928. case COMPLEX_EXPR:
  1929. case VECTOR_CST:
  1930. case ORDERED_EXPR:
  1931. case UNORDERED_EXPR:
  1932. case LTGT_EXPR:
  1933. case UNEQ_EXPR:
  1934. case UNLE_EXPR:
  1935. case UNLT_EXPR:
  1936. case UNGE_EXPR:
  1937. case UNGT_EXPR:
  1938. case ABS_EXPR:
  1939. case CONSTRUCTOR:
  1940. case COMPOUND_LITERAL_EXPR:
  1941. case VA_ARG_EXPR:
  1942. postfix_expression (e);
  1943. break;
  1944. case CONJ_EXPR:
  1945. case ADDR_EXPR:
  1946. case INDIRECT_REF:
  1947. case MEM_REF:
  1948. case NEGATE_EXPR:
  1949. case BIT_NOT_EXPR:
  1950. case TRUTH_NOT_EXPR:
  1951. case PREINCREMENT_EXPR:
  1952. case PREDECREMENT_EXPR:
  1953. case REALPART_EXPR:
  1954. case IMAGPART_EXPR:
  1955. unary_expression (e);
  1956. break;
  1957. case FLOAT_EXPR:
  1958. case FIX_TRUNC_EXPR:
  1959. CASE_CONVERT:
  1960. case VIEW_CONVERT_EXPR:
  1961. pp_c_cast_expression (this, e);
  1962. break;
  1963. case MULT_EXPR:
  1964. case TRUNC_MOD_EXPR:
  1965. case TRUNC_DIV_EXPR:
  1966. multiplicative_expression (e);
  1967. break;
  1968. case LSHIFT_EXPR:
  1969. case RSHIFT_EXPR:
  1970. pp_c_shift_expression (this, e);
  1971. break;
  1972. case LT_EXPR:
  1973. case GT_EXPR:
  1974. case LE_EXPR:
  1975. case GE_EXPR:
  1976. pp_c_relational_expression (this, e);
  1977. break;
  1978. case BIT_AND_EXPR:
  1979. pp_c_and_expression (this, e);
  1980. break;
  1981. case BIT_XOR_EXPR:
  1982. case TRUTH_XOR_EXPR:
  1983. pp_c_exclusive_or_expression (this, e);
  1984. break;
  1985. case BIT_IOR_EXPR:
  1986. pp_c_inclusive_or_expression (this, e);
  1987. break;
  1988. case TRUTH_ANDIF_EXPR:
  1989. case TRUTH_AND_EXPR:
  1990. pp_c_logical_and_expression (this, e);
  1991. break;
  1992. case TRUTH_ORIF_EXPR:
  1993. case TRUTH_OR_EXPR:
  1994. pp_c_logical_or_expression (this, e);
  1995. break;
  1996. case EQ_EXPR:
  1997. case NE_EXPR:
  1998. pp_c_equality_expression (this, e);
  1999. break;
  2000. case COND_EXPR:
  2001. conditional_expression (e);
  2002. break;
  2003. case POINTER_PLUS_EXPR:
  2004. case PLUS_EXPR:
  2005. case MINUS_EXPR:
  2006. pp_c_additive_expression (this, e);
  2007. break;
  2008. case MODIFY_EXPR:
  2009. case INIT_EXPR:
  2010. assignment_expression (e);
  2011. break;
  2012. case COMPOUND_EXPR:
  2013. pp_c_left_paren (this);
  2014. expression (TREE_OPERAND (e, 0));
  2015. pp_separate_with (this, ',');
  2016. assignment_expression (TREE_OPERAND (e, 1));
  2017. pp_c_right_paren (this);
  2018. break;
  2019. case NON_LVALUE_EXPR:
  2020. case SAVE_EXPR:
  2021. expression (TREE_OPERAND (e, 0));
  2022. break;
  2023. case TARGET_EXPR:
  2024. postfix_expression (TREE_OPERAND (e, 1));
  2025. break;
  2026. case BIND_EXPR:
  2027. case GOTO_EXPR:
  2028. /* We don't yet have a way of dumping statements in a
  2029. human-readable format. */
  2030. pp_string (this, "({...})");
  2031. break;
  2032. case C_MAYBE_CONST_EXPR:
  2033. expression (C_MAYBE_CONST_EXPR_EXPR (e));
  2034. break;
  2035. default:
  2036. pp_unsupported_tree (this, e);
  2037. break;
  2038. }
  2039. }
  2040. /* Statements. */
  2041. void
  2042. c_pretty_printer::statement (tree stmt)
  2043. {
  2044. if (stmt == NULL)
  2045. return;
  2046. if (pp_needs_newline (this))
  2047. pp_newline_and_indent (this, 0);
  2048. dump_generic_node (this, stmt, pp_indentation (this), 0, true);
  2049. }
  2050. /* Initialize the PRETTY-PRINTER for handling C codes. */
  2051. c_pretty_printer::c_pretty_printer ()
  2052. : pretty_printer (),
  2053. offset_list (),
  2054. flags ()
  2055. {
  2056. type_specifier_seq = pp_c_specifier_qualifier_list;
  2057. ptr_operator = pp_c_pointer;
  2058. parameter_list = pp_c_parameter_type_list;
  2059. }
  2060. /* Print the tree T in full, on file FILE. */
  2061. void
  2062. print_c_tree (FILE *file, tree t)
  2063. {
  2064. c_pretty_printer pp;
  2065. pp_needs_newline (&pp) = true;
  2066. pp.buffer->stream = file;
  2067. pp.statement (t);
  2068. pp_newline_and_flush (&pp);
  2069. }
  2070. /* Print the tree T in full, on stderr. */
  2071. DEBUG_FUNCTION void
  2072. debug_c_tree (tree t)
  2073. {
  2074. print_c_tree (stderr, t);
  2075. fputc ('\n', stderr);
  2076. }
  2077. /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
  2078. up of T's memory address. */
  2079. void
  2080. pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
  2081. {
  2082. const char *name;
  2083. gcc_assert (DECL_P (t));
  2084. if (DECL_NAME (t))
  2085. name = IDENTIFIER_POINTER (DECL_NAME (t));
  2086. else
  2087. {
  2088. static char xname[8];
  2089. sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
  2090. name = xname;
  2091. }
  2092. pp_c_identifier (pp, name);
  2093. }