cxx-pretty-print.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439
  1. /* Implementation of subroutines for the GNU C++ pretty-printer.
  2. Copyright (C) 2003-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 "intl.h"
  21. #include "cp-tree.h"
  22. #include "cxx-pretty-print.h"
  23. #include "tree-pretty-print.h"
  24. static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree);
  25. static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree);
  26. static void pp_cxx_qualified_id (cxx_pretty_printer *, tree);
  27. static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree);
  28. static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree);
  29. static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree);
  30. static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree);
  31. static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
  32. static void pp_cxx_cast_expression (cxx_pretty_printer *, tree);
  33. static void pp_cxx_typeid_expression (cxx_pretty_printer *, tree);
  34. static inline void
  35. pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
  36. {
  37. const char *p = pp_last_position_in_text (pp);
  38. if (p != NULL && *p == c)
  39. pp_cxx_whitespace (pp);
  40. pp_character (pp, c);
  41. pp->padding = pp_none;
  42. }
  43. #define pp_cxx_expression_list(PP, T) \
  44. pp_c_expression_list (PP, T)
  45. #define pp_cxx_space_for_pointer_operator(PP, T) \
  46. pp_c_space_for_pointer_operator (PP, T)
  47. #define pp_cxx_init_declarator(PP, T) \
  48. pp_c_init_declarator (PP, T)
  49. #define pp_cxx_call_argument_list(PP, T) \
  50. pp_c_call_argument_list (PP, T)
  51. void
  52. pp_cxx_colon_colon (cxx_pretty_printer *pp)
  53. {
  54. pp_colon_colon (pp);
  55. pp->padding = pp_none;
  56. }
  57. void
  58. pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
  59. {
  60. pp_cxx_nonconsecutive_character (pp, '<');
  61. }
  62. void
  63. pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
  64. {
  65. pp_cxx_nonconsecutive_character (pp, '>');
  66. }
  67. void
  68. pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
  69. {
  70. pp_separate_with (pp, c);
  71. pp->padding = pp_none;
  72. }
  73. /* Expressions. */
  74. static inline bool
  75. is_destructor_name (tree name)
  76. {
  77. return name == complete_dtor_identifier
  78. || name == base_dtor_identifier
  79. || name == deleting_dtor_identifier;
  80. }
  81. /* conversion-function-id:
  82. operator conversion-type-id
  83. conversion-type-id:
  84. type-specifier-seq conversion-declarator(opt)
  85. conversion-declarator:
  86. ptr-operator conversion-declarator(opt) */
  87. static inline void
  88. pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
  89. {
  90. pp_cxx_ws_string (pp, "operator");
  91. pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
  92. }
  93. static inline void
  94. pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
  95. {
  96. pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
  97. pp_cxx_begin_template_argument_list (pp);
  98. pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
  99. pp_cxx_end_template_argument_list (pp);
  100. }
  101. /* Prints the unqualified part of the id-expression T.
  102. unqualified-id:
  103. identifier
  104. operator-function-id
  105. conversion-function-id
  106. ~ class-name
  107. template-id */
  108. static void
  109. pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
  110. {
  111. enum tree_code code = TREE_CODE (t);
  112. switch (code)
  113. {
  114. case RESULT_DECL:
  115. pp->translate_string ("<return-value>");
  116. break;
  117. case OVERLOAD:
  118. t = OVL_CURRENT (t);
  119. case VAR_DECL:
  120. case PARM_DECL:
  121. case CONST_DECL:
  122. case TYPE_DECL:
  123. case FUNCTION_DECL:
  124. case NAMESPACE_DECL:
  125. case FIELD_DECL:
  126. case LABEL_DECL:
  127. case USING_DECL:
  128. case TEMPLATE_DECL:
  129. t = DECL_NAME (t);
  130. case IDENTIFIER_NODE:
  131. if (t == NULL)
  132. pp->translate_string ("<unnamed>");
  133. else if (IDENTIFIER_TYPENAME_P (t))
  134. pp_cxx_conversion_function_id (pp, t);
  135. else
  136. {
  137. if (is_destructor_name (t))
  138. {
  139. pp_complement (pp);
  140. /* FIXME: Why is this necessary? */
  141. if (TREE_TYPE (t))
  142. t = constructor_name (TREE_TYPE (t));
  143. }
  144. pp_cxx_tree_identifier (pp, t);
  145. }
  146. break;
  147. case TEMPLATE_ID_EXPR:
  148. pp_cxx_template_id (pp, t);
  149. break;
  150. case BASELINK:
  151. pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
  152. break;
  153. case RECORD_TYPE:
  154. case UNION_TYPE:
  155. case ENUMERAL_TYPE:
  156. case TYPENAME_TYPE:
  157. case UNBOUND_CLASS_TEMPLATE:
  158. pp_cxx_unqualified_id (pp, TYPE_NAME (t));
  159. if (CLASS_TYPE_P (t) && CLASSTYPE_USE_TEMPLATE (t))
  160. {
  161. pp_cxx_begin_template_argument_list (pp);
  162. pp_cxx_template_argument_list (pp, INNERMOST_TEMPLATE_ARGS
  163. (CLASSTYPE_TI_ARGS (t)));
  164. pp_cxx_end_template_argument_list (pp);
  165. }
  166. break;
  167. case BIT_NOT_EXPR:
  168. pp_cxx_complement (pp);
  169. pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
  170. break;
  171. case TEMPLATE_TYPE_PARM:
  172. case TEMPLATE_TEMPLATE_PARM:
  173. if (TYPE_IDENTIFIER (t))
  174. pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
  175. else
  176. pp_cxx_canonical_template_parameter (pp, t);
  177. break;
  178. case TEMPLATE_PARM_INDEX:
  179. pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
  180. break;
  181. case BOUND_TEMPLATE_TEMPLATE_PARM:
  182. pp_cxx_cv_qualifier_seq (pp, t);
  183. pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
  184. pp_cxx_begin_template_argument_list (pp);
  185. pp_cxx_template_argument_list (pp, TYPE_TI_ARGS (t));
  186. pp_cxx_end_template_argument_list (pp);
  187. break;
  188. default:
  189. pp_unsupported_tree (pp, t);
  190. break;
  191. }
  192. }
  193. /* Pretty-print out the token sequence ":: template" in template codes
  194. where it is needed to "inline declare" the (following) member as
  195. a template. This situation arises when SCOPE of T is dependent
  196. on template parameters. */
  197. static inline void
  198. pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
  199. {
  200. if (TREE_CODE (t) == TEMPLATE_ID_EXPR
  201. && TYPE_P (scope) && dependent_type_p (scope))
  202. pp_cxx_ws_string (pp, "template");
  203. }
  204. /* nested-name-specifier:
  205. class-or-namespace-name :: nested-name-specifier(opt)
  206. class-or-namespace-name :: template nested-name-specifier */
  207. static void
  208. pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
  209. {
  210. if (!SCOPE_FILE_SCOPE_P (t) && t != pp->enclosing_scope)
  211. {
  212. tree scope = get_containing_scope (t);
  213. pp_cxx_nested_name_specifier (pp, scope);
  214. pp_cxx_template_keyword_if_needed (pp, scope, t);
  215. pp_cxx_unqualified_id (pp, t);
  216. pp_cxx_colon_colon (pp);
  217. }
  218. }
  219. /* qualified-id:
  220. nested-name-specifier template(opt) unqualified-id */
  221. static void
  222. pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
  223. {
  224. switch (TREE_CODE (t))
  225. {
  226. /* A pointer-to-member is always qualified. */
  227. case PTRMEM_CST:
  228. pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
  229. pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
  230. break;
  231. /* In Standard C++, functions cannot possibly be used as
  232. nested-name-specifiers. However, there are situations where
  233. is "makes sense" to output the surrounding function name for the
  234. purpose of emphasizing on the scope kind. Just printing the
  235. function name might not be sufficient as it may be overloaded; so,
  236. we decorate the function with its signature too.
  237. FIXME: This is probably the wrong pretty-printing for conversion
  238. functions and some function templates. */
  239. case OVERLOAD:
  240. t = OVL_CURRENT (t);
  241. case FUNCTION_DECL:
  242. if (DECL_FUNCTION_MEMBER_P (t))
  243. pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
  244. pp_cxx_unqualified_id
  245. (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
  246. pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
  247. break;
  248. case OFFSET_REF:
  249. case SCOPE_REF:
  250. pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
  251. pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
  252. break;
  253. default:
  254. {
  255. tree scope = get_containing_scope (t);
  256. if (scope != pp->enclosing_scope)
  257. {
  258. pp_cxx_nested_name_specifier (pp, scope);
  259. pp_cxx_template_keyword_if_needed (pp, scope, t);
  260. }
  261. pp_cxx_unqualified_id (pp, t);
  262. }
  263. break;
  264. }
  265. }
  266. void
  267. cxx_pretty_printer::constant (tree t)
  268. {
  269. switch (TREE_CODE (t))
  270. {
  271. case STRING_CST:
  272. {
  273. const bool in_parens = PAREN_STRING_LITERAL_P (t);
  274. if (in_parens)
  275. pp_cxx_left_paren (this);
  276. c_pretty_printer::constant (t);
  277. if (in_parens)
  278. pp_cxx_right_paren (this);
  279. }
  280. break;
  281. case INTEGER_CST:
  282. if (NULLPTR_TYPE_P (TREE_TYPE (t)))
  283. {
  284. pp_string (this, "nullptr");
  285. break;
  286. }
  287. /* else fall through. */
  288. default:
  289. c_pretty_printer::constant (t);
  290. break;
  291. }
  292. }
  293. /* id-expression:
  294. unqualified-id
  295. qualified-id */
  296. void
  297. cxx_pretty_printer::id_expression (tree t)
  298. {
  299. if (TREE_CODE (t) == OVERLOAD)
  300. t = OVL_CURRENT (t);
  301. if (DECL_P (t) && DECL_CONTEXT (t))
  302. pp_cxx_qualified_id (this, t);
  303. else
  304. pp_cxx_unqualified_id (this, t);
  305. }
  306. /* user-defined literal:
  307. literal ud-suffix */
  308. void
  309. pp_cxx_userdef_literal (cxx_pretty_printer *pp, tree t)
  310. {
  311. pp->constant (USERDEF_LITERAL_VALUE (t));
  312. pp->id_expression (USERDEF_LITERAL_SUFFIX_ID (t));
  313. }
  314. /* primary-expression:
  315. literal
  316. this
  317. :: identifier
  318. :: operator-function-id
  319. :: qualifier-id
  320. ( expression )
  321. id-expression
  322. GNU Extensions:
  323. __builtin_va_arg ( assignment-expression , type-id )
  324. __builtin_offsetof ( type-id, offsetof-expression )
  325. __has_nothrow_assign ( type-id )
  326. __has_nothrow_constructor ( type-id )
  327. __has_nothrow_copy ( type-id )
  328. __has_trivial_assign ( type-id )
  329. __has_trivial_constructor ( type-id )
  330. __has_trivial_copy ( type-id )
  331. __has_trivial_destructor ( type-id )
  332. __has_virtual_destructor ( type-id )
  333. __is_abstract ( type-id )
  334. __is_base_of ( type-id , type-id )
  335. __is_class ( type-id )
  336. __is_empty ( type-id )
  337. __is_enum ( type-id )
  338. __is_literal_type ( type-id )
  339. __is_pod ( type-id )
  340. __is_polymorphic ( type-id )
  341. __is_std_layout ( type-id )
  342. __is_trivial ( type-id )
  343. __is_union ( type-id ) */
  344. void
  345. cxx_pretty_printer::primary_expression (tree t)
  346. {
  347. switch (TREE_CODE (t))
  348. {
  349. case VOID_CST:
  350. case INTEGER_CST:
  351. case REAL_CST:
  352. case COMPLEX_CST:
  353. case STRING_CST:
  354. constant (t);
  355. break;
  356. case USERDEF_LITERAL:
  357. pp_cxx_userdef_literal (this, t);
  358. break;
  359. case BASELINK:
  360. t = BASELINK_FUNCTIONS (t);
  361. case VAR_DECL:
  362. case PARM_DECL:
  363. case FIELD_DECL:
  364. case FUNCTION_DECL:
  365. case OVERLOAD:
  366. case CONST_DECL:
  367. case TEMPLATE_DECL:
  368. id_expression (t);
  369. break;
  370. case RESULT_DECL:
  371. case TEMPLATE_TYPE_PARM:
  372. case TEMPLATE_TEMPLATE_PARM:
  373. case TEMPLATE_PARM_INDEX:
  374. pp_cxx_unqualified_id (this, t);
  375. break;
  376. case STMT_EXPR:
  377. pp_cxx_left_paren (this);
  378. statement (STMT_EXPR_STMT (t));
  379. pp_cxx_right_paren (this);
  380. break;
  381. case TRAIT_EXPR:
  382. pp_cxx_trait_expression (this, t);
  383. break;
  384. case VA_ARG_EXPR:
  385. pp_cxx_va_arg_expression (this, t);
  386. break;
  387. case OFFSETOF_EXPR:
  388. pp_cxx_offsetof_expression (this, t);
  389. break;
  390. default:
  391. c_pretty_printer::primary_expression (t);
  392. break;
  393. }
  394. }
  395. /* postfix-expression:
  396. primary-expression
  397. postfix-expression [ expression ]
  398. postfix-expression ( expression-list(opt) )
  399. simple-type-specifier ( expression-list(opt) )
  400. typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
  401. typename ::(opt) nested-name-specifier template(opt)
  402. template-id ( expression-list(opt) )
  403. postfix-expression . template(opt) ::(opt) id-expression
  404. postfix-expression -> template(opt) ::(opt) id-expression
  405. postfix-expression . pseudo-destructor-name
  406. postfix-expression -> pseudo-destructor-name
  407. postfix-expression ++
  408. postfix-expression --
  409. dynamic_cast < type-id > ( expression )
  410. static_cast < type-id > ( expression )
  411. reinterpret_cast < type-id > ( expression )
  412. const_cast < type-id > ( expression )
  413. typeid ( expression )
  414. typeid ( type-id ) */
  415. void
  416. cxx_pretty_printer::postfix_expression (tree t)
  417. {
  418. enum tree_code code = TREE_CODE (t);
  419. switch (code)
  420. {
  421. case AGGR_INIT_EXPR:
  422. case CALL_EXPR:
  423. {
  424. tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
  425. : CALL_EXPR_FN (t));
  426. tree saved_scope = enclosing_scope;
  427. bool skipfirst = false;
  428. tree arg;
  429. if (TREE_CODE (fun) == ADDR_EXPR)
  430. fun = TREE_OPERAND (fun, 0);
  431. /* In templates, where there is no way to tell whether a given
  432. call uses an actual member function. So the parser builds
  433. FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
  434. instantiation time. */
  435. if (TREE_CODE (fun) != FUNCTION_DECL)
  436. ;
  437. else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
  438. {
  439. tree object = (code == AGGR_INIT_EXPR
  440. ? (AGGR_INIT_VIA_CTOR_P (t)
  441. ? AGGR_INIT_EXPR_SLOT (t)
  442. : AGGR_INIT_EXPR_ARG (t, 0))
  443. : CALL_EXPR_ARG (t, 0));
  444. while (TREE_CODE (object) == NOP_EXPR)
  445. object = TREE_OPERAND (object, 0);
  446. if (TREE_CODE (object) == ADDR_EXPR)
  447. object = TREE_OPERAND (object, 0);
  448. if (!TYPE_PTR_P (TREE_TYPE (object)))
  449. {
  450. postfix_expression (object);
  451. pp_cxx_dot (this);
  452. }
  453. else
  454. {
  455. postfix_expression (object);
  456. pp_cxx_arrow (this);
  457. }
  458. skipfirst = true;
  459. enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
  460. }
  461. postfix_expression (fun);
  462. enclosing_scope = saved_scope;
  463. pp_cxx_left_paren (this);
  464. if (code == AGGR_INIT_EXPR)
  465. {
  466. aggr_init_expr_arg_iterator iter;
  467. FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
  468. {
  469. if (skipfirst)
  470. skipfirst = false;
  471. else
  472. {
  473. expression (arg);
  474. if (more_aggr_init_expr_args_p (&iter))
  475. pp_cxx_separate_with (this, ',');
  476. }
  477. }
  478. }
  479. else
  480. {
  481. call_expr_arg_iterator iter;
  482. FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
  483. {
  484. if (skipfirst)
  485. skipfirst = false;
  486. else
  487. {
  488. expression (arg);
  489. if (more_call_expr_args_p (&iter))
  490. pp_cxx_separate_with (this, ',');
  491. }
  492. }
  493. }
  494. pp_cxx_right_paren (this);
  495. }
  496. if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
  497. {
  498. pp_cxx_separate_with (this, ',');
  499. postfix_expression (AGGR_INIT_EXPR_SLOT (t));
  500. }
  501. break;
  502. case BASELINK:
  503. case VAR_DECL:
  504. case PARM_DECL:
  505. case FIELD_DECL:
  506. case FUNCTION_DECL:
  507. case OVERLOAD:
  508. case CONST_DECL:
  509. case TEMPLATE_DECL:
  510. case RESULT_DECL:
  511. primary_expression (t);
  512. break;
  513. case DYNAMIC_CAST_EXPR:
  514. case STATIC_CAST_EXPR:
  515. case REINTERPRET_CAST_EXPR:
  516. case CONST_CAST_EXPR:
  517. if (code == DYNAMIC_CAST_EXPR)
  518. pp_cxx_ws_string (this, "dynamic_cast");
  519. else if (code == STATIC_CAST_EXPR)
  520. pp_cxx_ws_string (this, "static_cast");
  521. else if (code == REINTERPRET_CAST_EXPR)
  522. pp_cxx_ws_string (this, "reinterpret_cast");
  523. else
  524. pp_cxx_ws_string (this, "const_cast");
  525. pp_cxx_begin_template_argument_list (this);
  526. type_id (TREE_TYPE (t));
  527. pp_cxx_end_template_argument_list (this);
  528. pp_left_paren (this);
  529. expression (TREE_OPERAND (t, 0));
  530. pp_right_paren (this);
  531. break;
  532. case EMPTY_CLASS_EXPR:
  533. type_id (TREE_TYPE (t));
  534. pp_left_paren (this);
  535. pp_right_paren (this);
  536. break;
  537. case TYPEID_EXPR:
  538. pp_cxx_typeid_expression (this, t);
  539. break;
  540. case PSEUDO_DTOR_EXPR:
  541. postfix_expression (TREE_OPERAND (t, 0));
  542. pp_cxx_dot (this);
  543. if (TREE_OPERAND (t, 1))
  544. {
  545. pp_cxx_qualified_id (this, TREE_OPERAND (t, 1));
  546. pp_cxx_colon_colon (this);
  547. }
  548. pp_complement (this);
  549. pp_cxx_unqualified_id (this, TREE_OPERAND (t, 2));
  550. break;
  551. case ARROW_EXPR:
  552. postfix_expression (TREE_OPERAND (t, 0));
  553. pp_cxx_arrow (this);
  554. break;
  555. default:
  556. c_pretty_printer::postfix_expression (t);
  557. break;
  558. }
  559. }
  560. /* new-expression:
  561. ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
  562. ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
  563. new-placement:
  564. ( expression-list )
  565. new-type-id:
  566. type-specifier-seq new-declarator(opt)
  567. new-declarator:
  568. ptr-operator new-declarator(opt)
  569. direct-new-declarator
  570. direct-new-declarator
  571. [ expression ]
  572. direct-new-declarator [ constant-expression ]
  573. new-initializer:
  574. ( expression-list(opt) ) */
  575. static void
  576. pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
  577. {
  578. enum tree_code code = TREE_CODE (t);
  579. tree type = TREE_OPERAND (t, 1);
  580. tree init = TREE_OPERAND (t, 2);
  581. switch (code)
  582. {
  583. case NEW_EXPR:
  584. case VEC_NEW_EXPR:
  585. if (NEW_EXPR_USE_GLOBAL (t))
  586. pp_cxx_colon_colon (pp);
  587. pp_cxx_ws_string (pp, "new");
  588. if (TREE_OPERAND (t, 0))
  589. {
  590. pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
  591. pp_space (pp);
  592. }
  593. if (TREE_CODE (type) == ARRAY_REF)
  594. type = build_cplus_array_type
  595. (TREE_OPERAND (type, 0),
  596. build_index_type (fold_build2_loc (input_location,
  597. MINUS_EXPR, integer_type_node,
  598. TREE_OPERAND (type, 1),
  599. integer_one_node)));
  600. pp->type_id (type);
  601. if (init)
  602. {
  603. pp_left_paren (pp);
  604. if (TREE_CODE (init) == TREE_LIST)
  605. pp_c_expression_list (pp, init);
  606. else if (init == void_node)
  607. ; /* OK, empty initializer list. */
  608. else
  609. pp->expression (init);
  610. pp_right_paren (pp);
  611. }
  612. break;
  613. default:
  614. pp_unsupported_tree (pp, t);
  615. }
  616. }
  617. /* delete-expression:
  618. ::(opt) delete cast-expression
  619. ::(opt) delete [ ] cast-expression */
  620. static void
  621. pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
  622. {
  623. enum tree_code code = TREE_CODE (t);
  624. switch (code)
  625. {
  626. case DELETE_EXPR:
  627. case VEC_DELETE_EXPR:
  628. if (DELETE_EXPR_USE_GLOBAL (t))
  629. pp_cxx_colon_colon (pp);
  630. pp_cxx_ws_string (pp, "delete");
  631. pp_space (pp);
  632. if (code == VEC_DELETE_EXPR
  633. || DELETE_EXPR_USE_VEC (t))
  634. {
  635. pp_left_bracket (pp);
  636. pp_right_bracket (pp);
  637. pp_space (pp);
  638. }
  639. pp_c_cast_expression (pp, TREE_OPERAND (t, 0));
  640. break;
  641. default:
  642. pp_unsupported_tree (pp, t);
  643. }
  644. }
  645. /* unary-expression:
  646. postfix-expression
  647. ++ cast-expression
  648. -- cast-expression
  649. unary-operator cast-expression
  650. sizeof unary-expression
  651. sizeof ( type-id )
  652. sizeof ... ( identifier )
  653. new-expression
  654. delete-expression
  655. unary-operator: one of
  656. * & + - !
  657. GNU extensions:
  658. __alignof__ unary-expression
  659. __alignof__ ( type-id ) */
  660. void
  661. cxx_pretty_printer::unary_expression (tree t)
  662. {
  663. enum tree_code code = TREE_CODE (t);
  664. switch (code)
  665. {
  666. case NEW_EXPR:
  667. case VEC_NEW_EXPR:
  668. pp_cxx_new_expression (this, t);
  669. break;
  670. case DELETE_EXPR:
  671. case VEC_DELETE_EXPR:
  672. pp_cxx_delete_expression (this, t);
  673. break;
  674. case SIZEOF_EXPR:
  675. if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
  676. {
  677. pp_cxx_ws_string (this, "sizeof");
  678. pp_cxx_ws_string (this, "...");
  679. pp_cxx_whitespace (this);
  680. pp_cxx_left_paren (this);
  681. if (TYPE_P (TREE_OPERAND (t, 0)))
  682. type_id (TREE_OPERAND (t, 0));
  683. else
  684. unary_expression (TREE_OPERAND (t, 0));
  685. pp_cxx_right_paren (this);
  686. break;
  687. }
  688. /* Fall through */
  689. case ALIGNOF_EXPR:
  690. pp_cxx_ws_string (this, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
  691. pp_cxx_whitespace (this);
  692. if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
  693. {
  694. pp_cxx_left_paren (this);
  695. type_id (TREE_TYPE (TREE_OPERAND (t, 0)));
  696. pp_cxx_right_paren (this);
  697. }
  698. else if (TYPE_P (TREE_OPERAND (t, 0)))
  699. {
  700. pp_cxx_left_paren (this);
  701. type_id (TREE_OPERAND (t, 0));
  702. pp_cxx_right_paren (this);
  703. }
  704. else
  705. unary_expression (TREE_OPERAND (t, 0));
  706. break;
  707. case AT_ENCODE_EXPR:
  708. pp_cxx_ws_string (this, "@encode");
  709. pp_cxx_whitespace (this);
  710. pp_cxx_left_paren (this);
  711. type_id (TREE_OPERAND (t, 0));
  712. pp_cxx_right_paren (this);
  713. break;
  714. case NOEXCEPT_EXPR:
  715. pp_cxx_ws_string (this, "noexcept");
  716. pp_cxx_whitespace (this);
  717. pp_cxx_left_paren (this);
  718. expression (TREE_OPERAND (t, 0));
  719. pp_cxx_right_paren (this);
  720. break;
  721. case UNARY_PLUS_EXPR:
  722. pp_plus (this);
  723. pp_cxx_cast_expression (this, TREE_OPERAND (t, 0));
  724. break;
  725. default:
  726. c_pretty_printer::unary_expression (t);
  727. break;
  728. }
  729. }
  730. /* cast-expression:
  731. unary-expression
  732. ( type-id ) cast-expression */
  733. static void
  734. pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
  735. {
  736. switch (TREE_CODE (t))
  737. {
  738. case CAST_EXPR:
  739. case IMPLICIT_CONV_EXPR:
  740. pp->type_id (TREE_TYPE (t));
  741. pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
  742. break;
  743. default:
  744. pp_c_cast_expression (pp, t);
  745. break;
  746. }
  747. }
  748. /* pm-expression:
  749. cast-expression
  750. pm-expression .* cast-expression
  751. pm-expression ->* cast-expression */
  752. static void
  753. pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
  754. {
  755. switch (TREE_CODE (t))
  756. {
  757. /* Handle unfortunate OFFSET_REF overloading here. */
  758. case OFFSET_REF:
  759. if (TYPE_P (TREE_OPERAND (t, 0)))
  760. {
  761. pp_cxx_qualified_id (pp, t);
  762. break;
  763. }
  764. /* Else fall through. */
  765. case MEMBER_REF:
  766. case DOTSTAR_EXPR:
  767. pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
  768. if (TREE_CODE (t) == MEMBER_REF)
  769. pp_cxx_arrow (pp);
  770. else
  771. pp_cxx_dot (pp);
  772. pp_star(pp);
  773. pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
  774. break;
  775. default:
  776. pp_cxx_cast_expression (pp, t);
  777. break;
  778. }
  779. }
  780. /* multiplicative-expression:
  781. pm-expression
  782. multiplicative-expression * pm-expression
  783. multiplicative-expression / pm-expression
  784. multiplicative-expression % pm-expression */
  785. void
  786. cxx_pretty_printer::multiplicative_expression (tree e)
  787. {
  788. enum tree_code code = TREE_CODE (e);
  789. switch (code)
  790. {
  791. case MULT_EXPR:
  792. case TRUNC_DIV_EXPR:
  793. case TRUNC_MOD_EXPR:
  794. multiplicative_expression (TREE_OPERAND (e, 0));
  795. pp_space (this);
  796. if (code == MULT_EXPR)
  797. pp_star (this);
  798. else if (code == TRUNC_DIV_EXPR)
  799. pp_slash (this);
  800. else
  801. pp_modulo (this);
  802. pp_space (this);
  803. pp_cxx_pm_expression (this, TREE_OPERAND (e, 1));
  804. break;
  805. default:
  806. pp_cxx_pm_expression (this, e);
  807. break;
  808. }
  809. }
  810. /* conditional-expression:
  811. logical-or-expression
  812. logical-or-expression ? expression : assignment-expression */
  813. void
  814. cxx_pretty_printer::conditional_expression (tree e)
  815. {
  816. if (TREE_CODE (e) == COND_EXPR)
  817. {
  818. pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
  819. pp_space (this);
  820. pp_question (this);
  821. pp_space (this);
  822. expression (TREE_OPERAND (e, 1));
  823. pp_space (this);
  824. assignment_expression (TREE_OPERAND (e, 2));
  825. }
  826. else
  827. pp_c_logical_or_expression (this, e);
  828. }
  829. /* Pretty-print a compound assignment operator token as indicated by T. */
  830. static void
  831. pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
  832. {
  833. const char *op;
  834. switch (TREE_CODE (t))
  835. {
  836. case NOP_EXPR:
  837. op = "=";
  838. break;
  839. case PLUS_EXPR:
  840. op = "+=";
  841. break;
  842. case MINUS_EXPR:
  843. op = "-=";
  844. break;
  845. case TRUNC_DIV_EXPR:
  846. op = "/=";
  847. break;
  848. case TRUNC_MOD_EXPR:
  849. op = "%=";
  850. break;
  851. default:
  852. op = get_tree_code_name (TREE_CODE (t));
  853. break;
  854. }
  855. pp_cxx_ws_string (pp, op);
  856. }
  857. /* assignment-expression:
  858. conditional-expression
  859. logical-or-expression assignment-operator assignment-expression
  860. throw-expression
  861. throw-expression:
  862. throw assignment-expression(opt)
  863. assignment-operator: one of
  864. = *= /= %= += -= >>= <<= &= ^= |= */
  865. void
  866. cxx_pretty_printer::assignment_expression (tree e)
  867. {
  868. switch (TREE_CODE (e))
  869. {
  870. case MODIFY_EXPR:
  871. case INIT_EXPR:
  872. pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
  873. pp_space (this);
  874. pp_equal (this);
  875. pp_space (this);
  876. assignment_expression (TREE_OPERAND (e, 1));
  877. break;
  878. case THROW_EXPR:
  879. pp_cxx_ws_string (this, "throw");
  880. if (TREE_OPERAND (e, 0))
  881. assignment_expression (TREE_OPERAND (e, 0));
  882. break;
  883. case MODOP_EXPR:
  884. pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
  885. pp_cxx_assignment_operator (this, TREE_OPERAND (e, 1));
  886. assignment_expression (TREE_OPERAND (e, 2));
  887. break;
  888. default:
  889. conditional_expression (e);
  890. break;
  891. }
  892. }
  893. void
  894. cxx_pretty_printer::expression (tree t)
  895. {
  896. switch (TREE_CODE (t))
  897. {
  898. case STRING_CST:
  899. case VOID_CST:
  900. case INTEGER_CST:
  901. case REAL_CST:
  902. case COMPLEX_CST:
  903. constant (t);
  904. break;
  905. case USERDEF_LITERAL:
  906. pp_cxx_userdef_literal (this, t);
  907. break;
  908. case RESULT_DECL:
  909. pp_cxx_unqualified_id (this, t);
  910. break;
  911. #if 0
  912. case OFFSET_REF:
  913. #endif
  914. case SCOPE_REF:
  915. case PTRMEM_CST:
  916. pp_cxx_qualified_id (this, t);
  917. break;
  918. case OVERLOAD:
  919. t = OVL_CURRENT (t);
  920. case VAR_DECL:
  921. case PARM_DECL:
  922. case FIELD_DECL:
  923. case CONST_DECL:
  924. case FUNCTION_DECL:
  925. case BASELINK:
  926. case TEMPLATE_DECL:
  927. case TEMPLATE_TYPE_PARM:
  928. case TEMPLATE_PARM_INDEX:
  929. case TEMPLATE_TEMPLATE_PARM:
  930. case STMT_EXPR:
  931. primary_expression (t);
  932. break;
  933. case CALL_EXPR:
  934. case DYNAMIC_CAST_EXPR:
  935. case STATIC_CAST_EXPR:
  936. case REINTERPRET_CAST_EXPR:
  937. case CONST_CAST_EXPR:
  938. #if 0
  939. case MEMBER_REF:
  940. #endif
  941. case EMPTY_CLASS_EXPR:
  942. case TYPEID_EXPR:
  943. case PSEUDO_DTOR_EXPR:
  944. case AGGR_INIT_EXPR:
  945. case ARROW_EXPR:
  946. postfix_expression (t);
  947. break;
  948. case NEW_EXPR:
  949. case VEC_NEW_EXPR:
  950. pp_cxx_new_expression (this, t);
  951. break;
  952. case DELETE_EXPR:
  953. case VEC_DELETE_EXPR:
  954. pp_cxx_delete_expression (this, t);
  955. break;
  956. case SIZEOF_EXPR:
  957. case ALIGNOF_EXPR:
  958. case NOEXCEPT_EXPR:
  959. unary_expression (t);
  960. break;
  961. case CAST_EXPR:
  962. case IMPLICIT_CONV_EXPR:
  963. pp_cxx_cast_expression (this, t);
  964. break;
  965. case OFFSET_REF:
  966. case MEMBER_REF:
  967. case DOTSTAR_EXPR:
  968. pp_cxx_pm_expression (this, t);
  969. break;
  970. case MULT_EXPR:
  971. case TRUNC_DIV_EXPR:
  972. case TRUNC_MOD_EXPR:
  973. multiplicative_expression (t);
  974. break;
  975. case COND_EXPR:
  976. conditional_expression (t);
  977. break;
  978. case MODIFY_EXPR:
  979. case INIT_EXPR:
  980. case THROW_EXPR:
  981. case MODOP_EXPR:
  982. assignment_expression (t);
  983. break;
  984. case NON_DEPENDENT_EXPR:
  985. case MUST_NOT_THROW_EXPR:
  986. expression (TREE_OPERAND (t, 0));
  987. break;
  988. case EXPR_PACK_EXPANSION:
  989. expression (PACK_EXPANSION_PATTERN (t));
  990. pp_cxx_ws_string (this, "...");
  991. break;
  992. case TEMPLATE_ID_EXPR:
  993. pp_cxx_template_id (this, t);
  994. break;
  995. case NONTYPE_ARGUMENT_PACK:
  996. {
  997. tree args = ARGUMENT_PACK_ARGS (t);
  998. int i, len = TREE_VEC_LENGTH (args);
  999. for (i = 0; i < len; ++i)
  1000. {
  1001. if (i > 0)
  1002. pp_cxx_separate_with (this, ',');
  1003. expression (TREE_VEC_ELT (args, i));
  1004. }
  1005. }
  1006. break;
  1007. case LAMBDA_EXPR:
  1008. pp_cxx_ws_string (this, "<lambda>");
  1009. break;
  1010. case PAREN_EXPR:
  1011. pp_cxx_left_paren (this);
  1012. expression (TREE_OPERAND (t, 0));
  1013. pp_cxx_right_paren (this);
  1014. break;
  1015. default:
  1016. c_pretty_printer::expression (t);
  1017. break;
  1018. }
  1019. }
  1020. /* Declarations. */
  1021. /* function-specifier:
  1022. inline
  1023. virtual
  1024. explicit */
  1025. void
  1026. cxx_pretty_printer::function_specifier (tree t)
  1027. {
  1028. switch (TREE_CODE (t))
  1029. {
  1030. case FUNCTION_DECL:
  1031. if (DECL_VIRTUAL_P (t))
  1032. pp_cxx_ws_string (this, "virtual");
  1033. else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
  1034. pp_cxx_ws_string (this, "explicit");
  1035. else
  1036. c_pretty_printer::function_specifier (t);
  1037. default:
  1038. break;
  1039. }
  1040. }
  1041. /* decl-specifier-seq:
  1042. decl-specifier-seq(opt) decl-specifier
  1043. decl-specifier:
  1044. storage-class-specifier
  1045. type-specifier
  1046. function-specifier
  1047. friend
  1048. typedef */
  1049. void
  1050. cxx_pretty_printer::declaration_specifiers (tree t)
  1051. {
  1052. switch (TREE_CODE (t))
  1053. {
  1054. case VAR_DECL:
  1055. case PARM_DECL:
  1056. case CONST_DECL:
  1057. case FIELD_DECL:
  1058. storage_class_specifier (t);
  1059. declaration_specifiers (TREE_TYPE (t));
  1060. break;
  1061. case TYPE_DECL:
  1062. pp_cxx_ws_string (this, "typedef");
  1063. declaration_specifiers (TREE_TYPE (t));
  1064. break;
  1065. case FUNCTION_DECL:
  1066. /* Constructors don't have return types. And conversion functions
  1067. do not have a type-specifier in their return types. */
  1068. if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
  1069. function_specifier (t);
  1070. else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
  1071. declaration_specifiers (TREE_TYPE (TREE_TYPE (t)));
  1072. else
  1073. default:
  1074. c_pretty_printer::declaration_specifiers (t);
  1075. break;
  1076. }
  1077. }
  1078. /* simple-type-specifier:
  1079. ::(opt) nested-name-specifier(opt) type-name
  1080. ::(opt) nested-name-specifier(opt) template(opt) template-id
  1081. char
  1082. wchar_t
  1083. bool
  1084. short
  1085. int
  1086. long
  1087. signed
  1088. unsigned
  1089. float
  1090. double
  1091. void */
  1092. void
  1093. cxx_pretty_printer::simple_type_specifier (tree t)
  1094. {
  1095. switch (TREE_CODE (t))
  1096. {
  1097. case RECORD_TYPE:
  1098. case UNION_TYPE:
  1099. case ENUMERAL_TYPE:
  1100. pp_cxx_qualified_id (this, t);
  1101. break;
  1102. case TEMPLATE_TYPE_PARM:
  1103. case TEMPLATE_TEMPLATE_PARM:
  1104. case TEMPLATE_PARM_INDEX:
  1105. case BOUND_TEMPLATE_TEMPLATE_PARM:
  1106. pp_cxx_unqualified_id (this, t);
  1107. break;
  1108. case TYPENAME_TYPE:
  1109. pp_cxx_ws_string (this, "typename");
  1110. pp_cxx_nested_name_specifier (this, TYPE_CONTEXT (t));
  1111. pp_cxx_unqualified_id (this, TYPE_NAME (t));
  1112. break;
  1113. default:
  1114. c_pretty_printer::simple_type_specifier (t);
  1115. break;
  1116. }
  1117. }
  1118. /* type-specifier-seq:
  1119. type-specifier type-specifier-seq(opt)
  1120. type-specifier:
  1121. simple-type-specifier
  1122. class-specifier
  1123. enum-specifier
  1124. elaborated-type-specifier
  1125. cv-qualifier */
  1126. static void
  1127. pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
  1128. {
  1129. switch (TREE_CODE (t))
  1130. {
  1131. case TEMPLATE_DECL:
  1132. case TEMPLATE_TYPE_PARM:
  1133. case TEMPLATE_TEMPLATE_PARM:
  1134. case TYPE_DECL:
  1135. case BOUND_TEMPLATE_TEMPLATE_PARM:
  1136. pp_cxx_cv_qualifier_seq (pp, t);
  1137. pp->simple_type_specifier (t);
  1138. break;
  1139. case METHOD_TYPE:
  1140. pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
  1141. pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
  1142. pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
  1143. break;
  1144. case DECLTYPE_TYPE:
  1145. pp_cxx_ws_string (pp, "decltype");
  1146. pp_cxx_left_paren (pp);
  1147. pp->expression (DECLTYPE_TYPE_EXPR (t));
  1148. pp_cxx_right_paren (pp);
  1149. break;
  1150. case RECORD_TYPE:
  1151. if (TYPE_PTRMEMFUNC_P (t))
  1152. {
  1153. tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
  1154. pp->declaration_specifiers (TREE_TYPE (TREE_TYPE (pfm)));
  1155. pp_cxx_whitespace (pp);
  1156. pp_cxx_ptr_operator (pp, t);
  1157. break;
  1158. }
  1159. /* else fall through */
  1160. default:
  1161. if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
  1162. pp_c_specifier_qualifier_list (pp, t);
  1163. }
  1164. }
  1165. /* ptr-operator:
  1166. * cv-qualifier-seq(opt)
  1167. &
  1168. ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
  1169. static void
  1170. pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
  1171. {
  1172. if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
  1173. t = TREE_TYPE (t);
  1174. switch (TREE_CODE (t))
  1175. {
  1176. case REFERENCE_TYPE:
  1177. case POINTER_TYPE:
  1178. if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t)))
  1179. pp_cxx_ptr_operator (pp, TREE_TYPE (t));
  1180. pp_c_attributes_display (pp, TYPE_ATTRIBUTES (TREE_TYPE (t)));
  1181. if (TYPE_PTR_P (t))
  1182. {
  1183. pp_star (pp);
  1184. pp_cxx_cv_qualifier_seq (pp, t);
  1185. }
  1186. else
  1187. pp_ampersand (pp);
  1188. break;
  1189. case RECORD_TYPE:
  1190. if (TYPE_PTRMEMFUNC_P (t))
  1191. {
  1192. pp_cxx_left_paren (pp);
  1193. pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
  1194. pp_star (pp);
  1195. break;
  1196. }
  1197. case OFFSET_TYPE:
  1198. if (TYPE_PTRMEM_P (t))
  1199. {
  1200. if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
  1201. pp_cxx_left_paren (pp);
  1202. pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
  1203. pp_star (pp);
  1204. pp_cxx_cv_qualifier_seq (pp, t);
  1205. break;
  1206. }
  1207. /* else fall through. */
  1208. default:
  1209. pp_unsupported_tree (pp, t);
  1210. break;
  1211. }
  1212. }
  1213. static inline tree
  1214. pp_cxx_implicit_parameter_type (tree mf)
  1215. {
  1216. return class_of_this_parm (TREE_TYPE (mf));
  1217. }
  1218. /*
  1219. parameter-declaration:
  1220. decl-specifier-seq declarator
  1221. decl-specifier-seq declarator = assignment-expression
  1222. decl-specifier-seq abstract-declarator(opt)
  1223. decl-specifier-seq abstract-declarator(opt) assignment-expression */
  1224. static inline void
  1225. pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
  1226. {
  1227. pp->declaration_specifiers (t);
  1228. if (TYPE_P (t))
  1229. pp->abstract_declarator (t);
  1230. else
  1231. pp->declarator (t);
  1232. }
  1233. /* parameter-declaration-clause:
  1234. parameter-declaration-list(opt) ...(opt)
  1235. parameter-declaration-list , ...
  1236. parameter-declaration-list:
  1237. parameter-declaration
  1238. parameter-declaration-list , parameter-declaration */
  1239. static void
  1240. pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
  1241. {
  1242. tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
  1243. tree types =
  1244. TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
  1245. const bool abstract = args == NULL || pp->flags & pp_c_flag_abstract;
  1246. bool first = true;
  1247. /* Skip artificial parameter for nonstatic member functions. */
  1248. if (TREE_CODE (t) == METHOD_TYPE)
  1249. types = TREE_CHAIN (types);
  1250. pp_cxx_left_paren (pp);
  1251. for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
  1252. {
  1253. if (!first)
  1254. pp_cxx_separate_with (pp, ',');
  1255. first = false;
  1256. pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
  1257. if (!abstract && pp->flags & pp_cxx_flag_default_argument)
  1258. {
  1259. pp_cxx_whitespace (pp);
  1260. pp_equal (pp);
  1261. pp_cxx_whitespace (pp);
  1262. pp->assignment_expression (TREE_PURPOSE (types));
  1263. }
  1264. }
  1265. pp_cxx_right_paren (pp);
  1266. }
  1267. /* exception-specification:
  1268. throw ( type-id-list(opt) )
  1269. type-id-list
  1270. type-id
  1271. type-id-list , type-id */
  1272. static void
  1273. pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
  1274. {
  1275. tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
  1276. bool need_comma = false;
  1277. if (ex_spec == NULL)
  1278. return;
  1279. if (TREE_PURPOSE (ex_spec))
  1280. {
  1281. pp_cxx_ws_string (pp, "noexcept");
  1282. pp_cxx_whitespace (pp);
  1283. pp_cxx_left_paren (pp);
  1284. if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec))
  1285. pp_cxx_ws_string (pp, "<uninstantiated>");
  1286. else
  1287. pp->expression (TREE_PURPOSE (ex_spec));
  1288. pp_cxx_right_paren (pp);
  1289. return;
  1290. }
  1291. pp_cxx_ws_string (pp, "throw");
  1292. pp_cxx_left_paren (pp);
  1293. for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
  1294. {
  1295. tree type = TREE_VALUE (ex_spec);
  1296. tree argpack = NULL_TREE;
  1297. int i, len = 1;
  1298. if (ARGUMENT_PACK_P (type))
  1299. {
  1300. argpack = ARGUMENT_PACK_ARGS (type);
  1301. len = TREE_VEC_LENGTH (argpack);
  1302. }
  1303. for (i = 0; i < len; ++i)
  1304. {
  1305. if (argpack)
  1306. type = TREE_VEC_ELT (argpack, i);
  1307. if (need_comma)
  1308. pp_cxx_separate_with (pp, ',');
  1309. else
  1310. need_comma = true;
  1311. pp->type_id (type);
  1312. }
  1313. }
  1314. pp_cxx_right_paren (pp);
  1315. }
  1316. /* direct-declarator:
  1317. declarator-id
  1318. direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
  1319. exception-specification(opt)
  1320. direct-declaration [ constant-expression(opt) ]
  1321. ( declarator ) */
  1322. void
  1323. cxx_pretty_printer::direct_declarator (tree t)
  1324. {
  1325. switch (TREE_CODE (t))
  1326. {
  1327. case VAR_DECL:
  1328. case PARM_DECL:
  1329. case CONST_DECL:
  1330. case FIELD_DECL:
  1331. if (DECL_NAME (t))
  1332. {
  1333. pp_cxx_space_for_pointer_operator (this, TREE_TYPE (t));
  1334. if ((TREE_CODE (t) == PARM_DECL && DECL_PACK_P (t))
  1335. || template_parameter_pack_p (t))
  1336. /* A function parameter pack or non-type template
  1337. parameter pack. */
  1338. pp_cxx_ws_string (this, "...");
  1339. id_expression (DECL_NAME (t));
  1340. }
  1341. abstract_declarator (TREE_TYPE (t));
  1342. break;
  1343. case FUNCTION_DECL:
  1344. pp_cxx_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
  1345. expression (t);
  1346. pp_cxx_parameter_declaration_clause (this, t);
  1347. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
  1348. {
  1349. padding = pp_before;
  1350. pp_cxx_cv_qualifier_seq (this, pp_cxx_implicit_parameter_type (t));
  1351. }
  1352. pp_cxx_exception_specification (this, TREE_TYPE (t));
  1353. break;
  1354. case TYPENAME_TYPE:
  1355. case TEMPLATE_DECL:
  1356. case TEMPLATE_TYPE_PARM:
  1357. case TEMPLATE_PARM_INDEX:
  1358. case TEMPLATE_TEMPLATE_PARM:
  1359. break;
  1360. default:
  1361. c_pretty_printer::direct_declarator (t);
  1362. break;
  1363. }
  1364. }
  1365. /* declarator:
  1366. direct-declarator
  1367. ptr-operator declarator */
  1368. void
  1369. cxx_pretty_printer::declarator (tree t)
  1370. {
  1371. direct_declarator (t);
  1372. }
  1373. /* ctor-initializer:
  1374. : mem-initializer-list
  1375. mem-initializer-list:
  1376. mem-initializer
  1377. mem-initializer , mem-initializer-list
  1378. mem-initializer:
  1379. mem-initializer-id ( expression-list(opt) )
  1380. mem-initializer-id:
  1381. ::(opt) nested-name-specifier(opt) class-name
  1382. identifier */
  1383. static void
  1384. pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
  1385. {
  1386. t = TREE_OPERAND (t, 0);
  1387. pp_cxx_whitespace (pp);
  1388. pp_colon (pp);
  1389. pp_cxx_whitespace (pp);
  1390. for (; t; t = TREE_CHAIN (t))
  1391. {
  1392. tree purpose = TREE_PURPOSE (t);
  1393. bool is_pack = PACK_EXPANSION_P (purpose);
  1394. if (is_pack)
  1395. pp->primary_expression (PACK_EXPANSION_PATTERN (purpose));
  1396. else
  1397. pp->primary_expression (purpose);
  1398. pp_cxx_call_argument_list (pp, TREE_VALUE (t));
  1399. if (is_pack)
  1400. pp_cxx_ws_string (pp, "...");
  1401. if (TREE_CHAIN (t))
  1402. pp_cxx_separate_with (pp, ',');
  1403. }
  1404. }
  1405. /* function-definition:
  1406. decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
  1407. decl-specifier-seq(opt) declarator function-try-block */
  1408. static void
  1409. pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
  1410. {
  1411. tree saved_scope = pp->enclosing_scope;
  1412. pp->declaration_specifiers (t);
  1413. pp->declarator (t);
  1414. pp_needs_newline (pp) = true;
  1415. pp->enclosing_scope = DECL_CONTEXT (t);
  1416. if (DECL_SAVED_TREE (t))
  1417. pp->statement (DECL_SAVED_TREE (t));
  1418. else
  1419. pp_cxx_semicolon (pp);
  1420. pp_newline_and_flush (pp);
  1421. pp->enclosing_scope = saved_scope;
  1422. }
  1423. /* abstract-declarator:
  1424. ptr-operator abstract-declarator(opt)
  1425. direct-abstract-declarator */
  1426. void
  1427. cxx_pretty_printer::abstract_declarator (tree t)
  1428. {
  1429. if (TYPE_PTRMEM_P (t))
  1430. pp_cxx_right_paren (this);
  1431. else if (POINTER_TYPE_P (t))
  1432. {
  1433. if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
  1434. || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
  1435. pp_cxx_right_paren (this);
  1436. t = TREE_TYPE (t);
  1437. }
  1438. direct_abstract_declarator (t);
  1439. }
  1440. /* direct-abstract-declarator:
  1441. direct-abstract-declarator(opt) ( parameter-declaration-clause )
  1442. cv-qualifier-seq(opt) exception-specification(opt)
  1443. direct-abstract-declarator(opt) [ constant-expression(opt) ]
  1444. ( abstract-declarator ) */
  1445. void
  1446. cxx_pretty_printer::direct_abstract_declarator (tree t)
  1447. {
  1448. switch (TREE_CODE (t))
  1449. {
  1450. case REFERENCE_TYPE:
  1451. abstract_declarator (t);
  1452. break;
  1453. case RECORD_TYPE:
  1454. if (TYPE_PTRMEMFUNC_P (t))
  1455. direct_abstract_declarator (TYPE_PTRMEMFUNC_FN_TYPE (t));
  1456. break;
  1457. case METHOD_TYPE:
  1458. case FUNCTION_TYPE:
  1459. pp_cxx_parameter_declaration_clause (this, t);
  1460. direct_abstract_declarator (TREE_TYPE (t));
  1461. if (TREE_CODE (t) == METHOD_TYPE)
  1462. {
  1463. padding = pp_before;
  1464. pp_cxx_cv_qualifier_seq (this, class_of_this_parm (t));
  1465. }
  1466. pp_cxx_exception_specification (this, t);
  1467. break;
  1468. case TYPENAME_TYPE:
  1469. case TEMPLATE_TYPE_PARM:
  1470. case TEMPLATE_TEMPLATE_PARM:
  1471. case BOUND_TEMPLATE_TEMPLATE_PARM:
  1472. case UNBOUND_CLASS_TEMPLATE:
  1473. break;
  1474. default:
  1475. c_pretty_printer::direct_abstract_declarator (t);
  1476. break;
  1477. }
  1478. }
  1479. /* type-id:
  1480. type-specifier-seq abstract-declarator(opt) */
  1481. void
  1482. cxx_pretty_printer::type_id (tree t)
  1483. {
  1484. pp_flags saved_flags = flags;
  1485. flags |= pp_c_flag_abstract;
  1486. switch (TREE_CODE (t))
  1487. {
  1488. case TYPE_DECL:
  1489. case UNION_TYPE:
  1490. case RECORD_TYPE:
  1491. case ENUMERAL_TYPE:
  1492. case TYPENAME_TYPE:
  1493. case BOUND_TEMPLATE_TEMPLATE_PARM:
  1494. case UNBOUND_CLASS_TEMPLATE:
  1495. case TEMPLATE_TEMPLATE_PARM:
  1496. case TEMPLATE_TYPE_PARM:
  1497. case TEMPLATE_PARM_INDEX:
  1498. case TEMPLATE_DECL:
  1499. case TYPEOF_TYPE:
  1500. case UNDERLYING_TYPE:
  1501. case DECLTYPE_TYPE:
  1502. case TEMPLATE_ID_EXPR:
  1503. pp_cxx_type_specifier_seq (this, t);
  1504. break;
  1505. case TYPE_PACK_EXPANSION:
  1506. type_id (PACK_EXPANSION_PATTERN (t));
  1507. pp_cxx_ws_string (this, "...");
  1508. break;
  1509. default:
  1510. c_pretty_printer::type_id (t);
  1511. break;
  1512. }
  1513. flags = saved_flags;
  1514. }
  1515. /* template-argument-list:
  1516. template-argument ...(opt)
  1517. template-argument-list, template-argument ...(opt)
  1518. template-argument:
  1519. assignment-expression
  1520. type-id
  1521. template-name */
  1522. static void
  1523. pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
  1524. {
  1525. int i;
  1526. bool need_comma = false;
  1527. if (t == NULL)
  1528. return;
  1529. for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
  1530. {
  1531. tree arg = TREE_VEC_ELT (t, i);
  1532. tree argpack = NULL_TREE;
  1533. int idx, len = 1;
  1534. if (ARGUMENT_PACK_P (arg))
  1535. {
  1536. argpack = ARGUMENT_PACK_ARGS (arg);
  1537. len = TREE_VEC_LENGTH (argpack);
  1538. }
  1539. for (idx = 0; idx < len; idx++)
  1540. {
  1541. if (argpack)
  1542. arg = TREE_VEC_ELT (argpack, idx);
  1543. if (need_comma)
  1544. pp_cxx_separate_with (pp, ',');
  1545. else
  1546. need_comma = true;
  1547. if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
  1548. && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
  1549. pp->type_id (arg);
  1550. else
  1551. pp->expression (arg);
  1552. }
  1553. }
  1554. }
  1555. static void
  1556. pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
  1557. {
  1558. t = DECL_EXPR_DECL (t);
  1559. pp_cxx_type_specifier_seq (pp, t);
  1560. if (TYPE_P (t))
  1561. pp->abstract_declarator (t);
  1562. else
  1563. pp->declarator (t);
  1564. }
  1565. /* Statements. */
  1566. void
  1567. cxx_pretty_printer::statement (tree t)
  1568. {
  1569. switch (TREE_CODE (t))
  1570. {
  1571. case CTOR_INITIALIZER:
  1572. pp_cxx_ctor_initializer (this, t);
  1573. break;
  1574. case USING_STMT:
  1575. pp_cxx_ws_string (this, "using");
  1576. pp_cxx_ws_string (this, "namespace");
  1577. if (DECL_CONTEXT (t))
  1578. pp_cxx_nested_name_specifier (this, DECL_CONTEXT (t));
  1579. pp_cxx_qualified_id (this, USING_STMT_NAMESPACE (t));
  1580. break;
  1581. case USING_DECL:
  1582. pp_cxx_ws_string (this, "using");
  1583. pp_cxx_nested_name_specifier (this, USING_DECL_SCOPE (t));
  1584. pp_cxx_unqualified_id (this, DECL_NAME (t));
  1585. break;
  1586. case EH_SPEC_BLOCK:
  1587. break;
  1588. /* try-block:
  1589. try compound-statement handler-seq */
  1590. case TRY_BLOCK:
  1591. pp_maybe_newline_and_indent (this, 0);
  1592. pp_cxx_ws_string (this, "try");
  1593. pp_newline_and_indent (this, 3);
  1594. statement (TRY_STMTS (t));
  1595. pp_newline_and_indent (this, -3);
  1596. if (CLEANUP_P (t))
  1597. ;
  1598. else
  1599. statement (TRY_HANDLERS (t));
  1600. break;
  1601. /*
  1602. handler-seq:
  1603. handler handler-seq(opt)
  1604. handler:
  1605. catch ( exception-declaration ) compound-statement
  1606. exception-declaration:
  1607. type-specifier-seq declarator
  1608. type-specifier-seq abstract-declarator
  1609. ... */
  1610. case HANDLER:
  1611. pp_cxx_ws_string (this, "catch");
  1612. pp_cxx_left_paren (this);
  1613. pp_cxx_exception_declaration (this, HANDLER_PARMS (t));
  1614. pp_cxx_right_paren (this);
  1615. pp_indentation (this) += 3;
  1616. pp_needs_newline (this) = true;
  1617. statement (HANDLER_BODY (t));
  1618. pp_indentation (this) -= 3;
  1619. pp_needs_newline (this) = true;
  1620. break;
  1621. /* selection-statement:
  1622. if ( expression ) statement
  1623. if ( expression ) statement else statement */
  1624. case IF_STMT:
  1625. pp_cxx_ws_string (this, "if");
  1626. pp_cxx_whitespace (this);
  1627. pp_cxx_left_paren (this);
  1628. expression (IF_COND (t));
  1629. pp_cxx_right_paren (this);
  1630. pp_newline_and_indent (this, 2);
  1631. statement (THEN_CLAUSE (t));
  1632. pp_newline_and_indent (this, -2);
  1633. if (ELSE_CLAUSE (t))
  1634. {
  1635. tree else_clause = ELSE_CLAUSE (t);
  1636. pp_cxx_ws_string (this, "else");
  1637. if (TREE_CODE (else_clause) == IF_STMT)
  1638. pp_cxx_whitespace (this);
  1639. else
  1640. pp_newline_and_indent (this, 2);
  1641. statement (else_clause);
  1642. if (TREE_CODE (else_clause) != IF_STMT)
  1643. pp_newline_and_indent (this, -2);
  1644. }
  1645. break;
  1646. case SWITCH_STMT:
  1647. pp_cxx_ws_string (this, "switch");
  1648. pp_space (this);
  1649. pp_cxx_left_paren (this);
  1650. expression (SWITCH_STMT_COND (t));
  1651. pp_cxx_right_paren (this);
  1652. pp_indentation (this) += 3;
  1653. pp_needs_newline (this) = true;
  1654. statement (SWITCH_STMT_BODY (t));
  1655. pp_newline_and_indent (this, -3);
  1656. break;
  1657. /* iteration-statement:
  1658. while ( expression ) statement
  1659. do statement while ( expression ) ;
  1660. for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
  1661. for ( declaration expression(opt) ; expression(opt) ) statement */
  1662. case WHILE_STMT:
  1663. pp_cxx_ws_string (this, "while");
  1664. pp_space (this);
  1665. pp_cxx_left_paren (this);
  1666. expression (WHILE_COND (t));
  1667. pp_cxx_right_paren (this);
  1668. pp_newline_and_indent (this, 3);
  1669. statement (WHILE_BODY (t));
  1670. pp_indentation (this) -= 3;
  1671. pp_needs_newline (this) = true;
  1672. break;
  1673. case DO_STMT:
  1674. pp_cxx_ws_string (this, "do");
  1675. pp_newline_and_indent (this, 3);
  1676. statement (DO_BODY (t));
  1677. pp_newline_and_indent (this, -3);
  1678. pp_cxx_ws_string (this, "while");
  1679. pp_space (this);
  1680. pp_cxx_left_paren (this);
  1681. expression (DO_COND (t));
  1682. pp_cxx_right_paren (this);
  1683. pp_cxx_semicolon (this);
  1684. pp_needs_newline (this) = true;
  1685. break;
  1686. case FOR_STMT:
  1687. pp_cxx_ws_string (this, "for");
  1688. pp_space (this);
  1689. pp_cxx_left_paren (this);
  1690. if (FOR_INIT_STMT (t))
  1691. statement (FOR_INIT_STMT (t));
  1692. else
  1693. pp_cxx_semicolon (this);
  1694. pp_needs_newline (this) = false;
  1695. pp_cxx_whitespace (this);
  1696. if (FOR_COND (t))
  1697. expression (FOR_COND (t));
  1698. pp_cxx_semicolon (this);
  1699. pp_needs_newline (this) = false;
  1700. pp_cxx_whitespace (this);
  1701. if (FOR_EXPR (t))
  1702. expression (FOR_EXPR (t));
  1703. pp_cxx_right_paren (this);
  1704. pp_newline_and_indent (this, 3);
  1705. statement (FOR_BODY (t));
  1706. pp_indentation (this) -= 3;
  1707. pp_needs_newline (this) = true;
  1708. break;
  1709. case RANGE_FOR_STMT:
  1710. pp_cxx_ws_string (this, "for");
  1711. pp_space (this);
  1712. pp_cxx_left_paren (this);
  1713. statement (RANGE_FOR_DECL (t));
  1714. pp_space (this);
  1715. pp_needs_newline (this) = false;
  1716. pp_colon (this);
  1717. pp_space (this);
  1718. statement (RANGE_FOR_EXPR (t));
  1719. pp_cxx_right_paren (this);
  1720. pp_newline_and_indent (this, 3);
  1721. statement (FOR_BODY (t));
  1722. pp_indentation (this) -= 3;
  1723. pp_needs_newline (this) = true;
  1724. break;
  1725. /* jump-statement:
  1726. goto identifier;
  1727. continue ;
  1728. return expression(opt) ; */
  1729. case BREAK_STMT:
  1730. case CONTINUE_STMT:
  1731. pp_string (this, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
  1732. pp_cxx_semicolon (this);
  1733. pp_needs_newline (this) = true;
  1734. break;
  1735. /* expression-statement:
  1736. expression(opt) ; */
  1737. case EXPR_STMT:
  1738. expression (EXPR_STMT_EXPR (t));
  1739. pp_cxx_semicolon (this);
  1740. pp_needs_newline (this) = true;
  1741. break;
  1742. case CLEANUP_STMT:
  1743. pp_cxx_ws_string (this, "try");
  1744. pp_newline_and_indent (this, 2);
  1745. statement (CLEANUP_BODY (t));
  1746. pp_newline_and_indent (this, -2);
  1747. pp_cxx_ws_string (this, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
  1748. pp_newline_and_indent (this, 2);
  1749. statement (CLEANUP_EXPR (t));
  1750. pp_newline_and_indent (this, -2);
  1751. break;
  1752. case STATIC_ASSERT:
  1753. declaration (t);
  1754. break;
  1755. default:
  1756. c_pretty_printer::statement (t);
  1757. break;
  1758. }
  1759. }
  1760. /* original-namespace-definition:
  1761. namespace identifier { namespace-body }
  1762. As an edge case, we also handle unnamed namespace definition here. */
  1763. static void
  1764. pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
  1765. {
  1766. pp_cxx_ws_string (pp, "namespace");
  1767. if (DECL_CONTEXT (t))
  1768. pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
  1769. if (DECL_NAME (t))
  1770. pp_cxx_unqualified_id (pp, t);
  1771. pp_cxx_whitespace (pp);
  1772. pp_cxx_left_brace (pp);
  1773. /* We do not print the namespace-body. */
  1774. pp_cxx_whitespace (pp);
  1775. pp_cxx_right_brace (pp);
  1776. }
  1777. /* namespace-alias:
  1778. identifier
  1779. namespace-alias-definition:
  1780. namespace identifier = qualified-namespace-specifier ;
  1781. qualified-namespace-specifier:
  1782. ::(opt) nested-name-specifier(opt) namespace-name */
  1783. static void
  1784. pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
  1785. {
  1786. pp_cxx_ws_string (pp, "namespace");
  1787. if (DECL_CONTEXT (t))
  1788. pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
  1789. pp_cxx_unqualified_id (pp, t);
  1790. pp_cxx_whitespace (pp);
  1791. pp_equal (pp);
  1792. pp_cxx_whitespace (pp);
  1793. if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
  1794. pp_cxx_nested_name_specifier (pp,
  1795. DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
  1796. pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
  1797. pp_cxx_semicolon (pp);
  1798. }
  1799. /* simple-declaration:
  1800. decl-specifier-seq(opt) init-declarator-list(opt) */
  1801. static void
  1802. pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
  1803. {
  1804. pp->declaration_specifiers (t);
  1805. pp_cxx_init_declarator (pp, t);
  1806. pp_cxx_semicolon (pp);
  1807. pp_needs_newline (pp) = true;
  1808. }
  1809. /*
  1810. template-parameter-list:
  1811. template-parameter
  1812. template-parameter-list , template-parameter */
  1813. static inline void
  1814. pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
  1815. {
  1816. const int n = TREE_VEC_LENGTH (t);
  1817. int i;
  1818. for (i = 0; i < n; ++i)
  1819. {
  1820. if (i)
  1821. pp_cxx_separate_with (pp, ',');
  1822. pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
  1823. }
  1824. }
  1825. /* template-parameter:
  1826. type-parameter
  1827. parameter-declaration
  1828. type-parameter:
  1829. class ...(opt) identifier(opt)
  1830. class identifier(opt) = type-id
  1831. typename identifier(opt)
  1832. typename ...(opt) identifier(opt) = type-id
  1833. template < template-parameter-list > class ...(opt) identifier(opt)
  1834. template < template-parameter-list > class identifier(opt) = template-name */
  1835. static void
  1836. pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
  1837. {
  1838. tree parameter = TREE_VALUE (t);
  1839. switch (TREE_CODE (parameter))
  1840. {
  1841. case TYPE_DECL:
  1842. pp_cxx_ws_string (pp, "class");
  1843. if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
  1844. pp_cxx_ws_string (pp, "...");
  1845. if (DECL_NAME (parameter))
  1846. pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
  1847. /* FIXME: Check if we should print also default argument. */
  1848. break;
  1849. case PARM_DECL:
  1850. pp_cxx_parameter_declaration (pp, parameter);
  1851. break;
  1852. case TEMPLATE_DECL:
  1853. break;
  1854. default:
  1855. pp_unsupported_tree (pp, t);
  1856. break;
  1857. }
  1858. }
  1859. /* Pretty-print a template parameter in the canonical form
  1860. "template-parameter-<level>-<position in parameter list>". */
  1861. void
  1862. pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
  1863. {
  1864. const enum tree_code code = TREE_CODE (parm);
  1865. /* Brings type template parameters to the canonical forms. */
  1866. if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
  1867. || code == BOUND_TEMPLATE_TEMPLATE_PARM)
  1868. parm = TEMPLATE_TYPE_PARM_INDEX (parm);
  1869. pp_cxx_begin_template_argument_list (pp);
  1870. pp->translate_string ("template-parameter-");
  1871. pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
  1872. pp_minus (pp);
  1873. pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
  1874. pp_cxx_end_template_argument_list (pp);
  1875. }
  1876. /*
  1877. template-declaration:
  1878. export(opt) template < template-parameter-list > declaration */
  1879. static void
  1880. pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
  1881. {
  1882. tree tmpl = most_general_template (t);
  1883. tree level;
  1884. pp_maybe_newline_and_indent (pp, 0);
  1885. for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
  1886. {
  1887. pp_cxx_ws_string (pp, "template");
  1888. pp_cxx_begin_template_argument_list (pp);
  1889. pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
  1890. pp_cxx_end_template_argument_list (pp);
  1891. pp_newline_and_indent (pp, 3);
  1892. }
  1893. if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
  1894. pp_cxx_function_definition (pp, t);
  1895. else
  1896. pp_cxx_simple_declaration (pp, t);
  1897. }
  1898. static void
  1899. pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
  1900. {
  1901. pp_unsupported_tree (pp, t);
  1902. }
  1903. static void
  1904. pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
  1905. {
  1906. pp_unsupported_tree (pp, t);
  1907. }
  1908. /*
  1909. declaration:
  1910. block-declaration
  1911. function-definition
  1912. template-declaration
  1913. explicit-instantiation
  1914. explicit-specialization
  1915. linkage-specification
  1916. namespace-definition
  1917. block-declaration:
  1918. simple-declaration
  1919. asm-definition
  1920. namespace-alias-definition
  1921. using-declaration
  1922. using-directive
  1923. static_assert-declaration */
  1924. void
  1925. cxx_pretty_printer::declaration (tree t)
  1926. {
  1927. if (TREE_CODE (t) == STATIC_ASSERT)
  1928. {
  1929. pp_cxx_ws_string (this, "static_assert");
  1930. pp_cxx_left_paren (this);
  1931. expression (STATIC_ASSERT_CONDITION (t));
  1932. pp_cxx_separate_with (this, ',');
  1933. expression (STATIC_ASSERT_MESSAGE (t));
  1934. pp_cxx_right_paren (this);
  1935. }
  1936. else if (!DECL_LANG_SPECIFIC (t))
  1937. pp_cxx_simple_declaration (this, t);
  1938. else if (DECL_USE_TEMPLATE (t))
  1939. switch (DECL_USE_TEMPLATE (t))
  1940. {
  1941. case 1:
  1942. pp_cxx_template_declaration (this, t);
  1943. break;
  1944. case 2:
  1945. pp_cxx_explicit_specialization (this, t);
  1946. break;
  1947. case 3:
  1948. pp_cxx_explicit_instantiation (this, t);
  1949. break;
  1950. default:
  1951. break;
  1952. }
  1953. else switch (TREE_CODE (t))
  1954. {
  1955. case VAR_DECL:
  1956. case TYPE_DECL:
  1957. pp_cxx_simple_declaration (this, t);
  1958. break;
  1959. case FUNCTION_DECL:
  1960. if (DECL_SAVED_TREE (t))
  1961. pp_cxx_function_definition (this, t);
  1962. else
  1963. pp_cxx_simple_declaration (this, t);
  1964. break;
  1965. case NAMESPACE_DECL:
  1966. if (DECL_NAMESPACE_ALIAS (t))
  1967. pp_cxx_namespace_alias_definition (this, t);
  1968. else
  1969. pp_cxx_original_namespace_definition (this, t);
  1970. break;
  1971. default:
  1972. pp_unsupported_tree (this, t);
  1973. break;
  1974. }
  1975. }
  1976. static void
  1977. pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t)
  1978. {
  1979. t = TREE_OPERAND (t, 0);
  1980. pp_cxx_ws_string (pp, "typeid");
  1981. pp_cxx_left_paren (pp);
  1982. if (TYPE_P (t))
  1983. pp->type_id (t);
  1984. else
  1985. pp->expression (t);
  1986. pp_cxx_right_paren (pp);
  1987. }
  1988. void
  1989. pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t)
  1990. {
  1991. pp_cxx_ws_string (pp, "va_arg");
  1992. pp_cxx_left_paren (pp);
  1993. pp->assignment_expression (TREE_OPERAND (t, 0));
  1994. pp_cxx_separate_with (pp, ',');
  1995. pp->type_id (TREE_TYPE (t));
  1996. pp_cxx_right_paren (pp);
  1997. }
  1998. static bool
  1999. pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
  2000. {
  2001. switch (TREE_CODE (t))
  2002. {
  2003. case ARROW_EXPR:
  2004. if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
  2005. && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
  2006. {
  2007. pp->type_id (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
  2008. pp_cxx_separate_with (pp, ',');
  2009. return true;
  2010. }
  2011. return false;
  2012. case COMPONENT_REF:
  2013. if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
  2014. return false;
  2015. if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
  2016. pp_cxx_dot (pp);
  2017. pp->expression (TREE_OPERAND (t, 1));
  2018. return true;
  2019. case ARRAY_REF:
  2020. if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
  2021. return false;
  2022. pp_left_bracket (pp);
  2023. pp->expression (TREE_OPERAND (t, 1));
  2024. pp_right_bracket (pp);
  2025. return true;
  2026. default:
  2027. return false;
  2028. }
  2029. }
  2030. void
  2031. pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
  2032. {
  2033. pp_cxx_ws_string (pp, "offsetof");
  2034. pp_cxx_left_paren (pp);
  2035. if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
  2036. pp->expression (TREE_OPERAND (t, 0));
  2037. pp_cxx_right_paren (pp);
  2038. }
  2039. void
  2040. pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t)
  2041. {
  2042. cp_trait_kind kind = TRAIT_EXPR_KIND (t);
  2043. switch (kind)
  2044. {
  2045. case CPTK_HAS_NOTHROW_ASSIGN:
  2046. pp_cxx_ws_string (pp, "__has_nothrow_assign");
  2047. break;
  2048. case CPTK_HAS_TRIVIAL_ASSIGN:
  2049. pp_cxx_ws_string (pp, "__has_trivial_assign");
  2050. break;
  2051. case CPTK_HAS_NOTHROW_CONSTRUCTOR:
  2052. pp_cxx_ws_string (pp, "__has_nothrow_constructor");
  2053. break;
  2054. case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
  2055. pp_cxx_ws_string (pp, "__has_trivial_constructor");
  2056. break;
  2057. case CPTK_HAS_NOTHROW_COPY:
  2058. pp_cxx_ws_string (pp, "__has_nothrow_copy");
  2059. break;
  2060. case CPTK_HAS_TRIVIAL_COPY:
  2061. pp_cxx_ws_string (pp, "__has_trivial_copy");
  2062. break;
  2063. case CPTK_HAS_TRIVIAL_DESTRUCTOR:
  2064. pp_cxx_ws_string (pp, "__has_trivial_destructor");
  2065. break;
  2066. case CPTK_HAS_VIRTUAL_DESTRUCTOR:
  2067. pp_cxx_ws_string (pp, "__has_virtual_destructor");
  2068. break;
  2069. case CPTK_IS_ABSTRACT:
  2070. pp_cxx_ws_string (pp, "__is_abstract");
  2071. break;
  2072. case CPTK_IS_BASE_OF:
  2073. pp_cxx_ws_string (pp, "__is_base_of");
  2074. break;
  2075. case CPTK_IS_CLASS:
  2076. pp_cxx_ws_string (pp, "__is_class");
  2077. break;
  2078. case CPTK_IS_EMPTY:
  2079. pp_cxx_ws_string (pp, "__is_empty");
  2080. break;
  2081. case CPTK_IS_ENUM:
  2082. pp_cxx_ws_string (pp, "__is_enum");
  2083. break;
  2084. case CPTK_IS_FINAL:
  2085. pp_cxx_ws_string (pp, "__is_final");
  2086. break;
  2087. case CPTK_IS_POD:
  2088. pp_cxx_ws_string (pp, "__is_pod");
  2089. break;
  2090. case CPTK_IS_POLYMORPHIC:
  2091. pp_cxx_ws_string (pp, "__is_polymorphic");
  2092. break;
  2093. case CPTK_IS_STD_LAYOUT:
  2094. pp_cxx_ws_string (pp, "__is_std_layout");
  2095. break;
  2096. case CPTK_IS_TRIVIAL:
  2097. pp_cxx_ws_string (pp, "__is_trivial");
  2098. break;
  2099. case CPTK_IS_TRIVIALLY_ASSIGNABLE:
  2100. pp_cxx_ws_string (pp, "__is_trivially_assignable");
  2101. break;
  2102. case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
  2103. pp_cxx_ws_string (pp, "__is_trivially_constructible");
  2104. break;
  2105. case CPTK_IS_TRIVIALLY_COPYABLE:
  2106. pp_cxx_ws_string (pp, "__is_trivially_copyable");
  2107. break;
  2108. case CPTK_IS_UNION:
  2109. pp_cxx_ws_string (pp, "__is_union");
  2110. break;
  2111. case CPTK_IS_LITERAL_TYPE:
  2112. pp_cxx_ws_string (pp, "__is_literal_type");
  2113. break;
  2114. default:
  2115. gcc_unreachable ();
  2116. }
  2117. pp_cxx_left_paren (pp);
  2118. pp->type_id (TRAIT_EXPR_TYPE1 (t));
  2119. if (kind == CPTK_IS_BASE_OF)
  2120. {
  2121. pp_cxx_separate_with (pp, ',');
  2122. pp->type_id (TRAIT_EXPR_TYPE2 (t));
  2123. }
  2124. pp_cxx_right_paren (pp);
  2125. }
  2126. typedef c_pretty_print_fn pp_fun;
  2127. /* Initialization of a C++ pretty-printer object. */
  2128. cxx_pretty_printer::cxx_pretty_printer ()
  2129. : c_pretty_printer (),
  2130. enclosing_scope (global_namespace)
  2131. {
  2132. type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
  2133. parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
  2134. }