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