[AArch64] PR target/68129: Define TARGET_SUPPORTS_WIDE_INT
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob6b439ec4d3bbe1c6163ca3a0b538ea8adf8be40a
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "c-pretty-print.h"
27 #include "diagnostic.h"
28 #include "alias.h"
29 #include "stor-layout.h"
30 #include "attribs.h"
31 #include "intl.h"
32 #include "tree-pretty-print.h"
33 #include "tree-iterator.h"
35 /* The pretty-printer code is primarily designed to closely follow
36 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
37 codes we used to have in the past. Following a structured
38 approach (preferably the official grammars) is believed to make it
39 much easier to add extensions and nifty pretty-printing effects that
40 takes expression or declaration contexts into account. */
43 #define pp_c_maybe_whitespace(PP) \
44 do { \
45 if ((PP)->padding == pp_before) \
46 pp_c_whitespace (PP); \
47 } while (0)
49 /* literal */
50 static void pp_c_char (c_pretty_printer *, int);
52 /* postfix-expression */
53 static void pp_c_initializer_list (c_pretty_printer *, tree);
54 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
56 static void pp_c_additive_expression (c_pretty_printer *, tree);
57 static void pp_c_shift_expression (c_pretty_printer *, tree);
58 static void pp_c_relational_expression (c_pretty_printer *, tree);
59 static void pp_c_equality_expression (c_pretty_printer *, tree);
60 static void pp_c_and_expression (c_pretty_printer *, tree);
61 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
62 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
63 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
65 /* declarations. */
68 /* Helper functions. */
70 void
71 pp_c_whitespace (c_pretty_printer *pp)
73 pp_space (pp);
74 pp->padding = pp_none;
77 void
78 pp_c_left_paren (c_pretty_printer *pp)
80 pp_left_paren (pp);
81 pp->padding = pp_none;
84 void
85 pp_c_right_paren (c_pretty_printer *pp)
87 pp_right_paren (pp);
88 pp->padding = pp_none;
91 void
92 pp_c_left_brace (c_pretty_printer *pp)
94 pp_left_brace (pp);
95 pp->padding = pp_none;
98 void
99 pp_c_right_brace (c_pretty_printer *pp)
101 pp_right_brace (pp);
102 pp->padding = pp_none;
105 void
106 pp_c_left_bracket (c_pretty_printer *pp)
108 pp_left_bracket (pp);
109 pp->padding = pp_none;
112 void
113 pp_c_right_bracket (c_pretty_printer *pp)
115 pp_right_bracket (pp);
116 pp->padding = pp_none;
119 void
120 pp_c_dot (c_pretty_printer *pp)
122 pp_dot (pp);
123 pp->padding = pp_none;
126 void
127 pp_c_ampersand (c_pretty_printer *pp)
129 pp_ampersand (pp);
130 pp->padding = pp_none;
133 void
134 pp_c_star (c_pretty_printer *pp)
136 pp_star (pp);
137 pp->padding = pp_none;
140 void
141 pp_c_arrow (c_pretty_printer *pp)
143 pp_arrow (pp);
144 pp->padding = pp_none;
147 void
148 pp_c_semicolon (c_pretty_printer *pp)
150 pp_semicolon (pp);
151 pp->padding = pp_none;
154 void
155 pp_c_complement (c_pretty_printer *pp)
157 pp_complement (pp);
158 pp->padding = pp_none;
161 void
162 pp_c_exclamation (c_pretty_printer *pp)
164 pp_exclamation (pp);
165 pp->padding = pp_none;
168 /* Print out the external representation of QUALIFIERS. */
170 void
171 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
173 const char *p = pp_last_position_in_text (pp);
175 if (!qualifiers)
176 return;
178 /* The C programming language does not have references, but it is much
179 simpler to handle those here rather than going through the same
180 logic in the C++ pretty-printer. */
181 if (p != NULL && (*p == '*' || *p == '&'))
182 pp_c_whitespace (pp);
184 if (qualifiers & TYPE_QUAL_ATOMIC)
185 pp_c_ws_string (pp, "_Atomic");
186 if (qualifiers & TYPE_QUAL_CONST)
187 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
188 if (qualifiers & TYPE_QUAL_VOLATILE)
189 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
190 if (qualifiers & TYPE_QUAL_RESTRICT)
191 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
192 ? "restrict" : "__restrict__"));
195 /* Pretty-print T using the type-cast notation '( type-name )'. */
197 static void
198 pp_c_type_cast (c_pretty_printer *pp, tree t)
200 pp_c_left_paren (pp);
201 pp->type_id (t);
202 pp_c_right_paren (pp);
205 /* We're about to pretty-print a pointer type as indicated by T.
206 Output a whitespace, if needed, preparing for subsequent output. */
208 void
209 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
211 if (POINTER_TYPE_P (t))
213 tree pointee = strip_pointer_operator (TREE_TYPE (t));
214 if (TREE_CODE (pointee) != ARRAY_TYPE
215 && TREE_CODE (pointee) != FUNCTION_TYPE)
216 pp_c_whitespace (pp);
221 /* Declarations. */
223 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
224 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
225 of its type. Take care of possible extensions.
227 type-qualifier-list:
228 type-qualifier
229 type-qualifier-list type-qualifier
231 type-qualifier:
232 const
233 restrict -- C99
234 __restrict__ -- GNU C
235 address-space-qualifier -- GNU C
236 volatile
237 _Atomic -- C11
239 address-space-qualifier:
240 identifier -- GNU C */
242 void
243 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
245 int qualifiers;
247 if (!t || t == error_mark_node)
248 return;
250 if (!TYPE_P (t))
251 t = TREE_TYPE (t);
253 qualifiers = TYPE_QUALS (t);
254 pp_c_cv_qualifiers (pp, qualifiers,
255 TREE_CODE (t) == FUNCTION_TYPE);
257 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
259 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
260 pp_c_identifier (pp, as);
264 /* pointer:
265 * type-qualifier-list(opt)
266 * type-qualifier-list(opt) pointer */
268 static void
269 pp_c_pointer (c_pretty_printer *pp, tree t)
271 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
272 t = TREE_TYPE (t);
273 switch (TREE_CODE (t))
275 case POINTER_TYPE:
276 /* It is easier to handle C++ reference types here. */
277 case REFERENCE_TYPE:
278 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
279 pp_c_pointer (pp, TREE_TYPE (t));
280 if (TREE_CODE (t) == POINTER_TYPE)
281 pp_c_star (pp);
282 else
283 pp_c_ampersand (pp);
284 pp_c_type_qualifier_list (pp, t);
285 break;
287 /* ??? This node is now in GENERIC and so shouldn't be here. But
288 we'll fix that later. */
289 case DECL_EXPR:
290 pp->declaration (DECL_EXPR_DECL (t));
291 pp_needs_newline (pp) = true;
292 break;
294 default:
295 pp_unsupported_tree (pp, t);
299 /* simple-type-specifier:
300 type-specifier
302 type-specifier:
303 void
304 char
305 short
307 long
308 float
309 double
310 signed
311 unsigned
312 _Bool -- C99
313 _Complex -- C99
314 _Imaginary -- C99
315 struct-or-union-specifier
316 enum-specifier
317 typedef-name.
319 GNU extensions.
320 simple-type-specifier:
321 __complex__
322 __vector__ */
324 void
325 c_pretty_printer::simple_type_specifier (tree t)
327 const enum tree_code code = TREE_CODE (t);
328 switch (code)
330 case ERROR_MARK:
331 translate_string ("<type-error>");
332 break;
334 case IDENTIFIER_NODE:
335 pp_c_identifier (this, IDENTIFIER_POINTER (t));
336 break;
338 case VOID_TYPE:
339 case BOOLEAN_TYPE:
340 case INTEGER_TYPE:
341 case REAL_TYPE:
342 case FIXED_POINT_TYPE:
343 if (TYPE_NAME (t))
345 t = TYPE_NAME (t);
346 simple_type_specifier (t);
348 else
350 int prec = TYPE_PRECISION (t);
351 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
352 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
353 else
354 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
355 if (TYPE_NAME (t))
357 simple_type_specifier (t);
358 if (TYPE_PRECISION (t) != prec)
360 pp_colon (this);
361 pp_decimal_int (this, prec);
364 else
366 switch (code)
368 case INTEGER_TYPE:
369 translate_string (TYPE_UNSIGNED (t)
370 ? "<unnamed-unsigned:"
371 : "<unnamed-signed:");
372 break;
373 case REAL_TYPE:
374 translate_string ("<unnamed-float:");
375 break;
376 case FIXED_POINT_TYPE:
377 translate_string ("<unnamed-fixed:");
378 break;
379 default:
380 gcc_unreachable ();
382 pp_decimal_int (this, prec);
383 pp_greater (this);
386 break;
388 case TYPE_DECL:
389 if (DECL_NAME (t))
390 id_expression (t);
391 else
392 translate_string ("<typedef-error>");
393 break;
395 case UNION_TYPE:
396 case RECORD_TYPE:
397 case ENUMERAL_TYPE:
398 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
399 /* Don't decorate the type if this is a typedef name. */;
400 else if (code == UNION_TYPE)
401 pp_c_ws_string (this, "union");
402 else if (code == RECORD_TYPE)
403 pp_c_ws_string (this, "struct");
404 else if (code == ENUMERAL_TYPE)
405 pp_c_ws_string (this, "enum");
406 else
407 translate_string ("<tag-error>");
409 if (TYPE_NAME (t))
410 id_expression (TYPE_NAME (t));
411 else
412 translate_string ("<anonymous>");
413 break;
415 default:
416 pp_unsupported_tree (this, t);
417 break;
421 /* specifier-qualifier-list:
422 type-specifier specifier-qualifier-list-opt
423 type-qualifier specifier-qualifier-list-opt
426 Implementation note: Because of the non-linearities in array or
427 function declarations, this routine prints not just the
428 specifier-qualifier-list of such entities or types of such entities,
429 but also the 'pointer' production part of their declarators. The
430 remaining part is done by declarator() or abstract_declarator(). */
432 void
433 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
435 const enum tree_code code = TREE_CODE (t);
437 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
438 pp_c_type_qualifier_list (pp, t);
439 switch (code)
441 case REFERENCE_TYPE:
442 case POINTER_TYPE:
444 /* Get the types-specifier of this type. */
445 tree pointee = strip_pointer_operator (TREE_TYPE (t));
446 pp_c_specifier_qualifier_list (pp, pointee);
447 if (TREE_CODE (pointee) == ARRAY_TYPE
448 || TREE_CODE (pointee) == FUNCTION_TYPE)
450 pp_c_whitespace (pp);
451 pp_c_left_paren (pp);
452 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
454 else if (!c_dialect_cxx ())
455 pp_c_whitespace (pp);
456 pp_ptr_operator (pp, t);
458 break;
460 case FUNCTION_TYPE:
461 case ARRAY_TYPE:
462 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
463 break;
465 case VECTOR_TYPE:
466 case COMPLEX_TYPE:
467 if (code == COMPLEX_TYPE)
468 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
469 ? "_Complex" : "__complex__"));
470 else if (code == VECTOR_TYPE)
472 pp_c_ws_string (pp, "__vector");
473 pp_c_left_paren (pp);
474 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
475 pp_c_right_paren (pp);
476 pp_c_whitespace (pp);
478 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
479 break;
481 default:
482 pp->simple_type_specifier (t);
483 break;
485 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
486 pp_c_type_qualifier_list (pp, t);
489 /* parameter-type-list:
490 parameter-list
491 parameter-list , ...
493 parameter-list:
494 parameter-declaration
495 parameter-list , parameter-declaration
497 parameter-declaration:
498 declaration-specifiers declarator
499 declaration-specifiers abstract-declarator(opt) */
501 void
502 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
504 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
505 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
506 pp_c_left_paren (pp);
507 if (parms == void_list_node)
508 pp_c_ws_string (pp, "void");
509 else
511 bool first = true;
512 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
514 if (!first)
515 pp_separate_with (pp, ',');
516 first = false;
517 pp->declaration_specifiers
518 (want_parm_decl ? parms : TREE_VALUE (parms));
519 if (want_parm_decl)
520 pp->declarator (parms);
521 else
522 pp->abstract_declarator (TREE_VALUE (parms));
525 pp_c_right_paren (pp);
528 /* abstract-declarator:
529 pointer
530 pointer(opt) direct-abstract-declarator */
532 void
533 c_pretty_printer::abstract_declarator (tree t)
535 if (TREE_CODE (t) == POINTER_TYPE)
537 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
538 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
539 pp_c_right_paren (this);
540 t = TREE_TYPE (t);
543 direct_abstract_declarator (t);
546 /* direct-abstract-declarator:
547 ( abstract-declarator )
548 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
549 direct-abstract-declarator(opt) [ * ]
550 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
552 void
553 c_pretty_printer::direct_abstract_declarator (tree t)
555 switch (TREE_CODE (t))
557 case POINTER_TYPE:
558 abstract_declarator (t);
559 break;
561 case FUNCTION_TYPE:
562 pp_c_parameter_type_list (this, t);
563 direct_abstract_declarator (TREE_TYPE (t));
564 break;
566 case ARRAY_TYPE:
567 pp_c_left_bracket (this);
568 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
570 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
571 tree type = TREE_TYPE (maxval);
573 if (tree_fits_shwi_p (maxval))
574 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
575 else
576 expression (fold_build2 (PLUS_EXPR, type, maxval,
577 build_int_cst (type, 1)));
579 pp_c_right_bracket (this);
580 direct_abstract_declarator (TREE_TYPE (t));
581 break;
583 case IDENTIFIER_NODE:
584 case VOID_TYPE:
585 case BOOLEAN_TYPE:
586 case INTEGER_TYPE:
587 case REAL_TYPE:
588 case FIXED_POINT_TYPE:
589 case ENUMERAL_TYPE:
590 case RECORD_TYPE:
591 case UNION_TYPE:
592 case VECTOR_TYPE:
593 case COMPLEX_TYPE:
594 case TYPE_DECL:
595 break;
597 default:
598 pp_unsupported_tree (this, t);
599 break;
603 /* type-name:
604 specifier-qualifier-list abstract-declarator(opt) */
606 void
607 c_pretty_printer::type_id (tree t)
609 pp_c_specifier_qualifier_list (this, t);
610 abstract_declarator (t);
613 /* storage-class-specifier:
614 typedef
615 extern
616 static
617 auto
618 register */
620 void
621 c_pretty_printer::storage_class_specifier (tree t)
623 if (TREE_CODE (t) == TYPE_DECL)
624 pp_c_ws_string (this, "typedef");
625 else if (DECL_P (t))
627 if (DECL_REGISTER (t))
628 pp_c_ws_string (this, "register");
629 else if (TREE_STATIC (t) && VAR_P (t))
630 pp_c_ws_string (this, "static");
634 /* function-specifier:
635 inline */
637 void
638 c_pretty_printer::function_specifier (tree t)
640 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
641 pp_c_ws_string (this, "inline");
644 /* declaration-specifiers:
645 storage-class-specifier declaration-specifiers(opt)
646 type-specifier declaration-specifiers(opt)
647 type-qualifier declaration-specifiers(opt)
648 function-specifier declaration-specifiers(opt) */
650 void
651 c_pretty_printer::declaration_specifiers (tree t)
653 storage_class_specifier (t);
654 function_specifier (t);
655 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
658 /* direct-declarator
659 identifier
660 ( declarator )
661 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
662 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
663 direct-declarator [ type-qualifier-list static assignment-expression ]
664 direct-declarator [ type-qualifier-list * ]
665 direct-declarator ( parameter-type-list )
666 direct-declarator ( identifier-list(opt) ) */
668 void
669 c_pretty_printer::direct_declarator (tree t)
671 switch (TREE_CODE (t))
673 case VAR_DECL:
674 case PARM_DECL:
675 case TYPE_DECL:
676 case FIELD_DECL:
677 case LABEL_DECL:
678 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
679 pp_c_tree_decl_identifier (this, t);
680 break;
682 case ARRAY_TYPE:
683 case POINTER_TYPE:
684 abstract_declarator (TREE_TYPE (t));
685 break;
687 case FUNCTION_TYPE:
688 pp_parameter_list (this, t);
689 abstract_declarator (TREE_TYPE (t));
690 break;
692 case FUNCTION_DECL:
693 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
694 pp_c_tree_decl_identifier (this, t);
695 if (flags & pp_c_flag_abstract)
696 abstract_declarator (TREE_TYPE (t));
697 else
699 pp_parameter_list (this, t);
700 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
702 break;
704 case INTEGER_TYPE:
705 case REAL_TYPE:
706 case FIXED_POINT_TYPE:
707 case ENUMERAL_TYPE:
708 case UNION_TYPE:
709 case RECORD_TYPE:
710 break;
712 default:
713 pp_unsupported_tree (this, t);
714 break;
719 /* declarator:
720 pointer(opt) direct-declarator */
722 void
723 c_pretty_printer::declarator (tree t)
725 switch (TREE_CODE (t))
727 case INTEGER_TYPE:
728 case REAL_TYPE:
729 case FIXED_POINT_TYPE:
730 case ENUMERAL_TYPE:
731 case UNION_TYPE:
732 case RECORD_TYPE:
733 break;
735 case VAR_DECL:
736 case PARM_DECL:
737 case FIELD_DECL:
738 case ARRAY_TYPE:
739 case FUNCTION_TYPE:
740 case FUNCTION_DECL:
741 case TYPE_DECL:
742 direct_declarator (t);
743 break;
746 default:
747 pp_unsupported_tree (this, t);
748 break;
752 /* declaration:
753 declaration-specifiers init-declarator-list(opt) ; */
755 void
756 c_pretty_printer::declaration (tree t)
758 declaration_specifiers (t);
759 pp_c_init_declarator (this, t);
762 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
764 void
765 pp_c_attributes (c_pretty_printer *pp, tree attributes)
767 if (attributes == NULL_TREE)
768 return;
770 pp_c_ws_string (pp, "__attribute__");
771 pp_c_left_paren (pp);
772 pp_c_left_paren (pp);
773 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
775 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
776 if (TREE_VALUE (attributes))
777 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
779 if (TREE_CHAIN (attributes))
780 pp_separate_with (pp, ',');
782 pp_c_right_paren (pp);
783 pp_c_right_paren (pp);
786 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
787 marked to be displayed on disgnostic. */
789 void
790 pp_c_attributes_display (c_pretty_printer *pp, tree a)
792 bool is_first = true;
794 if (a == NULL_TREE)
795 return;
797 for (; a != NULL_TREE; a = TREE_CHAIN (a))
799 const struct attribute_spec *as;
800 as = lookup_attribute_spec (TREE_PURPOSE (a));
801 if (!as || as->affects_type_identity == false)
802 continue;
803 if (c_dialect_cxx ()
804 && !strcmp ("transaction_safe", as->name))
805 /* In C++ transaction_safe is printed at the end of the declarator. */
806 continue;
807 if (is_first)
809 pp_c_ws_string (pp, "__attribute__");
810 pp_c_left_paren (pp);
811 pp_c_left_paren (pp);
812 is_first = false;
814 else
816 pp_separate_with (pp, ',');
818 pp_tree_identifier (pp, TREE_PURPOSE (a));
819 if (TREE_VALUE (a))
820 pp_c_call_argument_list (pp, TREE_VALUE (a));
823 if (!is_first)
825 pp_c_right_paren (pp);
826 pp_c_right_paren (pp);
827 pp_c_whitespace (pp);
831 /* function-definition:
832 declaration-specifiers declarator compound-statement */
834 void
835 pp_c_function_definition (c_pretty_printer *pp, tree t)
837 pp->declaration_specifiers (t);
838 pp->declarator (t);
839 pp_needs_newline (pp) = true;
840 pp->statement (DECL_SAVED_TREE (t));
841 pp_newline_and_flush (pp);
845 /* Expressions. */
847 /* Print out a c-char. This is called solely for characters which are
848 in the *target* execution character set. We ought to convert them
849 back to the *host* execution character set before printing, but we
850 have no way to do this at present. A decent compromise is to print
851 all characters as if they were in the host execution character set,
852 and not attempt to recover any named escape characters, but render
853 all unprintables as octal escapes. If the host and target character
854 sets are the same, this produces relatively readable output. If they
855 are not the same, strings may appear as gibberish, but that's okay
856 (in fact, it may well be what the reader wants, e.g. if they are looking
857 to see if conversion to the target character set happened correctly).
859 A special case: we need to prefix \, ", and ' with backslashes. It is
860 correct to do so for the *host*'s \, ", and ', because the rest of the
861 file appears in the host character set. */
863 static void
864 pp_c_char (c_pretty_printer *pp, int c)
866 if (ISPRINT (c))
868 switch (c)
870 case '\\': pp_string (pp, "\\\\"); break;
871 case '\'': pp_string (pp, "\\\'"); break;
872 case '\"': pp_string (pp, "\\\""); break;
873 default: pp_character (pp, c);
876 else
877 pp_scalar (pp, "\\%03o", (unsigned) c);
880 /* Print out a STRING literal. */
882 void
883 pp_c_string_literal (c_pretty_printer *pp, tree s)
885 const char *p = TREE_STRING_POINTER (s);
886 int n = TREE_STRING_LENGTH (s) - 1;
887 int i;
888 pp_doublequote (pp);
889 for (i = 0; i < n; ++i)
890 pp_c_char (pp, p[i]);
891 pp_doublequote (pp);
894 /* Pretty-print a VOID_CST (void_node). */
896 static void
897 pp_c_void_constant (c_pretty_printer *pp)
899 pp_c_type_cast (pp, void_type_node);
900 pp_string (pp, "0");
903 /* Pretty-print an INTEGER literal. */
905 static void
906 pp_c_integer_constant (c_pretty_printer *pp, tree i)
908 int idx;
910 /* We are going to compare the type of I to other types using
911 pointer comparison so we need to use its canonical type. */
912 tree type =
913 TYPE_CANONICAL (TREE_TYPE (i))
914 ? TYPE_CANONICAL (TREE_TYPE (i))
915 : TREE_TYPE (i);
917 if (tree_fits_shwi_p (i))
918 pp_wide_integer (pp, tree_to_shwi (i));
919 else if (tree_fits_uhwi_p (i))
920 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
921 else
923 wide_int wi = i;
925 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
927 pp_minus (pp);
928 wi = -wi;
930 print_hex (wi, pp_buffer (pp)->digit_buffer);
931 pp_string (pp, pp_buffer (pp)->digit_buffer);
933 if (TYPE_UNSIGNED (type))
934 pp_character (pp, 'u');
935 if (type == long_integer_type_node || type == long_unsigned_type_node)
936 pp_character (pp, 'l');
937 else if (type == long_long_integer_type_node
938 || type == long_long_unsigned_type_node)
939 pp_string (pp, "ll");
940 else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++)
941 if (int_n_enabled_p[idx])
943 char buf[2+20];
944 if (type == int_n_trees[idx].signed_type
945 || type == int_n_trees[idx].unsigned_type)
947 sprintf (buf, "I%d", int_n_data[idx].bitsize);
948 pp_string (pp, buf);
953 /* Print out a CHARACTER literal. */
955 static void
956 pp_c_character_constant (c_pretty_printer *pp, tree c)
958 pp_quote (pp);
959 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
960 pp_quote (pp);
963 /* Print out a BOOLEAN literal. */
965 static void
966 pp_c_bool_constant (c_pretty_printer *pp, tree b)
968 if (b == boolean_false_node)
970 if (c_dialect_cxx ())
971 pp_c_ws_string (pp, "false");
972 else if (flag_isoc99)
973 pp_c_ws_string (pp, "_False");
974 else
975 pp_unsupported_tree (pp, b);
977 else if (b == boolean_true_node)
979 if (c_dialect_cxx ())
980 pp_c_ws_string (pp, "true");
981 else if (flag_isoc99)
982 pp_c_ws_string (pp, "_True");
983 else
984 pp_unsupported_tree (pp, b);
986 else if (TREE_CODE (b) == INTEGER_CST)
987 pp_c_integer_constant (pp, b);
988 else
989 pp_unsupported_tree (pp, b);
992 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
993 false; that means the value was obtained by a cast, in which case
994 print out the type-id part of the cast-expression -- the casted value
995 is then printed by pp_c_integer_literal. */
997 static bool
998 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1000 bool value_is_named = true;
1001 tree type = TREE_TYPE (e);
1002 tree value;
1004 /* Find the name of this constant. */
1005 for (value = TYPE_VALUES (type);
1006 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1007 value = TREE_CHAIN (value))
1010 if (value != NULL_TREE)
1011 pp->id_expression (TREE_PURPOSE (value));
1012 else
1014 /* Value must have been cast. */
1015 pp_c_type_cast (pp, type);
1016 value_is_named = false;
1019 return value_is_named;
1022 /* Print out a REAL value as a decimal-floating-constant. */
1024 static void
1025 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1027 const struct real_format *fmt
1028 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1030 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1031 bool is_decimal = floating_cst.decimal;
1033 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1034 log10(2) to 7 significant digits. */
1035 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1037 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1038 sizeof (pp_buffer (pp)->digit_buffer),
1039 max_digits10, 1);
1041 pp_string (pp, pp_buffer(pp)->digit_buffer);
1042 if (TREE_TYPE (r) == float_type_node)
1043 pp_character (pp, 'f');
1044 else if (TREE_TYPE (r) == long_double_type_node)
1045 pp_character (pp, 'l');
1046 else if (TREE_TYPE (r) == dfloat128_type_node)
1047 pp_string (pp, "dl");
1048 else if (TREE_TYPE (r) == dfloat64_type_node)
1049 pp_string (pp, "dd");
1050 else if (TREE_TYPE (r) == dfloat32_type_node)
1051 pp_string (pp, "df");
1054 /* Print out a FIXED value as a decimal-floating-constant. */
1056 static void
1057 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1059 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1060 sizeof (pp_buffer (pp)->digit_buffer));
1061 pp_string (pp, pp_buffer(pp)->digit_buffer);
1064 /* Pretty-print a compound literal expression. GNU extensions include
1065 vector constants. */
1067 static void
1068 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1070 tree type = TREE_TYPE (e);
1071 pp_c_type_cast (pp, type);
1073 switch (TREE_CODE (type))
1075 case RECORD_TYPE:
1076 case UNION_TYPE:
1077 case ARRAY_TYPE:
1078 case VECTOR_TYPE:
1079 case COMPLEX_TYPE:
1080 pp_c_brace_enclosed_initializer_list (pp, e);
1081 break;
1083 default:
1084 pp_unsupported_tree (pp, e);
1085 break;
1089 /* Pretty-print a COMPLEX_EXPR expression. */
1091 static void
1092 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1094 /* Handle a few common special cases, otherwise fallback
1095 to printing it as compound literal. */
1096 tree type = TREE_TYPE (e);
1097 tree realexpr = TREE_OPERAND (e, 0);
1098 tree imagexpr = TREE_OPERAND (e, 1);
1100 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1101 if (TREE_CODE (realexpr) == NOP_EXPR
1102 && TREE_CODE (imagexpr) == NOP_EXPR
1103 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1104 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1105 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1106 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1107 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1108 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1110 pp_c_type_cast (pp, type);
1111 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1112 return;
1115 /* Cast of an scalar expression to COMPLEX_TYPE. */
1116 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1117 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1119 pp_c_type_cast (pp, type);
1120 if (TREE_CODE (realexpr) == NOP_EXPR)
1121 realexpr = TREE_OPERAND (realexpr, 0);
1122 pp->expression (realexpr);
1123 return;
1126 pp_c_compound_literal (pp, e);
1129 /* constant:
1130 integer-constant
1131 floating-constant
1132 fixed-point-constant
1133 enumeration-constant
1134 character-constant */
1136 void
1137 c_pretty_printer::constant (tree e)
1139 const enum tree_code code = TREE_CODE (e);
1141 switch (code)
1143 case VOID_CST:
1144 pp_c_void_constant (this);
1145 break;
1147 case INTEGER_CST:
1149 tree type = TREE_TYPE (e);
1150 if (type == boolean_type_node)
1151 pp_c_bool_constant (this, e);
1152 else if (type == char_type_node)
1153 pp_c_character_constant (this, e);
1154 else if (TREE_CODE (type) == ENUMERAL_TYPE
1155 && pp_c_enumeration_constant (this, e))
1157 else
1158 pp_c_integer_constant (this, e);
1160 break;
1162 case REAL_CST:
1163 pp_c_floating_constant (this, e);
1164 break;
1166 case FIXED_CST:
1167 pp_c_fixed_constant (this, e);
1168 break;
1170 case STRING_CST:
1171 pp_c_string_literal (this, e);
1172 break;
1174 case COMPLEX_CST:
1175 /* Sometimes, we are confused and we think a complex literal
1176 is a constant. Such thing is a compound literal which
1177 grammatically belongs to postfix-expr production. */
1178 pp_c_compound_literal (this, e);
1179 break;
1181 default:
1182 pp_unsupported_tree (this, e);
1183 break;
1187 /* Pretty-print a string such as an identifier, without changing its
1188 encoding, preceded by whitespace is necessary. */
1190 void
1191 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1193 pp_c_maybe_whitespace (pp);
1194 pp_string (pp, str);
1195 pp->padding = pp_before;
1198 void
1199 c_pretty_printer::translate_string (const char *gmsgid)
1201 if (pp_translate_identifiers (this))
1202 pp_c_ws_string (this, _(gmsgid));
1203 else
1204 pp_c_ws_string (this, gmsgid);
1207 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1208 that need converting to the locale encoding, preceded by whitespace
1209 is necessary. */
1211 void
1212 pp_c_identifier (c_pretty_printer *pp, const char *id)
1214 pp_c_maybe_whitespace (pp);
1215 pp_identifier (pp, id);
1216 pp->padding = pp_before;
1219 /* Pretty-print a C primary-expression.
1220 primary-expression:
1221 identifier
1222 constant
1223 string-literal
1224 ( expression ) */
1226 void
1227 c_pretty_printer::primary_expression (tree e)
1229 switch (TREE_CODE (e))
1231 case VAR_DECL:
1232 case PARM_DECL:
1233 case FIELD_DECL:
1234 case CONST_DECL:
1235 case FUNCTION_DECL:
1236 case LABEL_DECL:
1237 pp_c_tree_decl_identifier (this, e);
1238 break;
1240 case IDENTIFIER_NODE:
1241 pp_c_tree_identifier (this, e);
1242 break;
1244 case ERROR_MARK:
1245 translate_string ("<erroneous-expression>");
1246 break;
1248 case RESULT_DECL:
1249 translate_string ("<return-value>");
1250 break;
1252 case VOID_CST:
1253 case INTEGER_CST:
1254 case REAL_CST:
1255 case FIXED_CST:
1256 case STRING_CST:
1257 constant (e);
1258 break;
1260 case TARGET_EXPR:
1261 pp_c_ws_string (this, "__builtin_memcpy");
1262 pp_c_left_paren (this);
1263 pp_ampersand (this);
1264 primary_expression (TREE_OPERAND (e, 0));
1265 pp_separate_with (this, ',');
1266 pp_ampersand (this);
1267 initializer (TREE_OPERAND (e, 1));
1268 if (TREE_OPERAND (e, 2))
1270 pp_separate_with (this, ',');
1271 expression (TREE_OPERAND (e, 2));
1273 pp_c_right_paren (this);
1274 break;
1276 default:
1277 /* FIXME: Make sure we won't get into an infinite loop. */
1278 pp_c_left_paren (this);
1279 expression (e);
1280 pp_c_right_paren (this);
1281 break;
1285 /* Print out a C initializer -- also support C compound-literals.
1286 initializer:
1287 assignment-expression:
1288 { initializer-list }
1289 { initializer-list , } */
1291 void
1292 c_pretty_printer::initializer (tree e)
1294 if (TREE_CODE (e) == CONSTRUCTOR)
1295 pp_c_brace_enclosed_initializer_list (this, e);
1296 else
1297 expression (e);
1300 /* init-declarator:
1301 declarator:
1302 declarator = initializer */
1304 void
1305 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1307 pp->declarator (t);
1308 /* We don't want to output function definitions here. There are handled
1309 elsewhere (and the syntactic form is bogus anyway). */
1310 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1312 tree init = DECL_INITIAL (t);
1313 /* This C++ bit is handled here because it is easier to do so.
1314 In templates, the C++ parser builds a TREE_LIST for a
1315 direct-initialization; the TREE_PURPOSE is the variable to
1316 initialize and the TREE_VALUE is the initializer. */
1317 if (TREE_CODE (init) == TREE_LIST)
1319 pp_c_left_paren (pp);
1320 pp->expression (TREE_VALUE (init));
1321 pp_right_paren (pp);
1323 else
1325 pp_space (pp);
1326 pp_equal (pp);
1327 pp_space (pp);
1328 pp->initializer (init);
1333 /* initializer-list:
1334 designation(opt) initializer
1335 initializer-list , designation(opt) initializer
1337 designation:
1338 designator-list =
1340 designator-list:
1341 designator
1342 designator-list designator
1344 designator:
1345 [ constant-expression ]
1346 identifier */
1348 static void
1349 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1351 tree type = TREE_TYPE (e);
1352 const enum tree_code code = TREE_CODE (type);
1354 if (TREE_CODE (e) == CONSTRUCTOR)
1356 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1357 return;
1360 switch (code)
1362 case RECORD_TYPE:
1363 case UNION_TYPE:
1364 case ARRAY_TYPE:
1366 tree init = TREE_OPERAND (e, 0);
1367 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1369 if (code == RECORD_TYPE || code == UNION_TYPE)
1371 pp_c_dot (pp);
1372 pp->primary_expression (TREE_PURPOSE (init));
1374 else
1376 pp_c_left_bracket (pp);
1377 if (TREE_PURPOSE (init))
1378 pp->constant (TREE_PURPOSE (init));
1379 pp_c_right_bracket (pp);
1381 pp_c_whitespace (pp);
1382 pp_equal (pp);
1383 pp_c_whitespace (pp);
1384 pp->initializer (TREE_VALUE (init));
1385 if (TREE_CHAIN (init))
1386 pp_separate_with (pp, ',');
1389 return;
1391 case VECTOR_TYPE:
1392 if (TREE_CODE (e) == VECTOR_CST)
1394 unsigned i;
1395 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1397 if (i > 0)
1398 pp_separate_with (pp, ',');
1399 pp->expression (VECTOR_CST_ELT (e, i));
1402 else
1403 break;
1404 return;
1406 case COMPLEX_TYPE:
1407 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1409 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1410 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1411 pp_separate_with (pp, ',');
1412 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1414 else
1415 break;
1416 return;
1418 default:
1419 break;
1422 pp_unsupported_tree (pp, type);
1425 /* Pretty-print a brace-enclosed initializer-list. */
1427 static void
1428 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1430 pp_c_left_brace (pp);
1431 pp_c_initializer_list (pp, l);
1432 pp_c_right_brace (pp);
1436 /* This is a convenient function, used to bridge gap between C and C++
1437 grammars.
1439 id-expression:
1440 identifier */
1442 void
1443 c_pretty_printer::id_expression (tree t)
1445 switch (TREE_CODE (t))
1447 case VAR_DECL:
1448 case PARM_DECL:
1449 case CONST_DECL:
1450 case TYPE_DECL:
1451 case FUNCTION_DECL:
1452 case FIELD_DECL:
1453 case LABEL_DECL:
1454 pp_c_tree_decl_identifier (this, t);
1455 break;
1457 case IDENTIFIER_NODE:
1458 pp_c_tree_identifier (this, t);
1459 break;
1461 default:
1462 pp_unsupported_tree (this, t);
1463 break;
1467 /* postfix-expression:
1468 primary-expression
1469 postfix-expression [ expression ]
1470 postfix-expression ( argument-expression-list(opt) )
1471 postfix-expression . identifier
1472 postfix-expression -> identifier
1473 postfix-expression ++
1474 postfix-expression --
1475 ( type-name ) { initializer-list }
1476 ( type-name ) { initializer-list , } */
1478 void
1479 c_pretty_printer::postfix_expression (tree e)
1481 enum tree_code code = TREE_CODE (e);
1482 switch (code)
1484 case POSTINCREMENT_EXPR:
1485 case POSTDECREMENT_EXPR:
1486 postfix_expression (TREE_OPERAND (e, 0));
1487 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1488 break;
1490 case ARRAY_REF:
1491 postfix_expression (TREE_OPERAND (e, 0));
1492 pp_c_left_bracket (this);
1493 expression (TREE_OPERAND (e, 1));
1494 pp_c_right_bracket (this);
1495 break;
1497 case ARRAY_NOTATION_REF:
1498 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1499 pp_c_left_bracket (this);
1500 expression (ARRAY_NOTATION_START (e));
1501 pp_colon (this);
1502 expression (ARRAY_NOTATION_LENGTH (e));
1503 pp_colon (this);
1504 expression (ARRAY_NOTATION_STRIDE (e));
1505 pp_c_right_bracket (this);
1506 break;
1508 case CALL_EXPR:
1510 call_expr_arg_iterator iter;
1511 tree arg;
1512 postfix_expression (CALL_EXPR_FN (e));
1513 pp_c_left_paren (this);
1514 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1516 expression (arg);
1517 if (more_call_expr_args_p (&iter))
1518 pp_separate_with (this, ',');
1520 pp_c_right_paren (this);
1521 break;
1524 case UNORDERED_EXPR:
1525 pp_c_ws_string (this, flag_isoc99
1526 ? "isunordered"
1527 : "__builtin_isunordered");
1528 goto two_args_fun;
1530 case ORDERED_EXPR:
1531 pp_c_ws_string (this, flag_isoc99
1532 ? "!isunordered"
1533 : "!__builtin_isunordered");
1534 goto two_args_fun;
1536 case UNLT_EXPR:
1537 pp_c_ws_string (this, flag_isoc99
1538 ? "!isgreaterequal"
1539 : "!__builtin_isgreaterequal");
1540 goto two_args_fun;
1542 case UNLE_EXPR:
1543 pp_c_ws_string (this, flag_isoc99
1544 ? "!isgreater"
1545 : "!__builtin_isgreater");
1546 goto two_args_fun;
1548 case UNGT_EXPR:
1549 pp_c_ws_string (this, flag_isoc99
1550 ? "!islessequal"
1551 : "!__builtin_islessequal");
1552 goto two_args_fun;
1554 case UNGE_EXPR:
1555 pp_c_ws_string (this, flag_isoc99
1556 ? "!isless"
1557 : "!__builtin_isless");
1558 goto two_args_fun;
1560 case UNEQ_EXPR:
1561 pp_c_ws_string (this, flag_isoc99
1562 ? "!islessgreater"
1563 : "!__builtin_islessgreater");
1564 goto two_args_fun;
1566 case LTGT_EXPR:
1567 pp_c_ws_string (this, flag_isoc99
1568 ? "islessgreater"
1569 : "__builtin_islessgreater");
1570 goto two_args_fun;
1572 two_args_fun:
1573 pp_c_left_paren (this);
1574 expression (TREE_OPERAND (e, 0));
1575 pp_separate_with (this, ',');
1576 expression (TREE_OPERAND (e, 1));
1577 pp_c_right_paren (this);
1578 break;
1580 case ABS_EXPR:
1581 pp_c_ws_string (this, "__builtin_abs");
1582 pp_c_left_paren (this);
1583 expression (TREE_OPERAND (e, 0));
1584 pp_c_right_paren (this);
1585 break;
1587 case COMPONENT_REF:
1589 tree object = TREE_OPERAND (e, 0);
1590 if (INDIRECT_REF_P (object))
1592 postfix_expression (TREE_OPERAND (object, 0));
1593 pp_c_arrow (this);
1595 else
1597 postfix_expression (object);
1598 pp_c_dot (this);
1600 expression (TREE_OPERAND (e, 1));
1602 break;
1604 case BIT_FIELD_REF:
1606 tree type = TREE_TYPE (e);
1608 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1609 if (type
1610 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1612 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1613 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1614 if ((bitpos % size) == 0)
1616 pp_c_left_paren (this);
1617 pp_c_left_paren (this);
1618 type_id (type);
1619 pp_c_star (this);
1620 pp_c_right_paren (this);
1621 pp_c_ampersand (this);
1622 expression (TREE_OPERAND (e, 0));
1623 pp_c_right_paren (this);
1624 pp_c_left_bracket (this);
1625 pp_wide_integer (this, bitpos / size);
1626 pp_c_right_bracket (this);
1627 break;
1630 pp_unsupported_tree (this, e);
1632 break;
1634 case MEM_REF:
1635 expression (e);
1636 break;
1638 case COMPLEX_CST:
1639 case VECTOR_CST:
1640 pp_c_compound_literal (this, e);
1641 break;
1643 case COMPLEX_EXPR:
1644 pp_c_complex_expr (this, e);
1645 break;
1647 case COMPOUND_LITERAL_EXPR:
1648 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1649 /* Fall through. */
1650 case CONSTRUCTOR:
1651 initializer (e);
1652 break;
1654 case VA_ARG_EXPR:
1655 pp_c_ws_string (this, "__builtin_va_arg");
1656 pp_c_left_paren (this);
1657 assignment_expression (TREE_OPERAND (e, 0));
1658 pp_separate_with (this, ',');
1659 type_id (TREE_TYPE (e));
1660 pp_c_right_paren (this);
1661 break;
1663 case ADDR_EXPR:
1664 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1666 id_expression (TREE_OPERAND (e, 0));
1667 break;
1669 /* else fall through. */
1671 default:
1672 primary_expression (e);
1673 break;
1677 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1679 void
1680 pp_c_expression_list (c_pretty_printer *pp, tree e)
1682 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1684 pp->expression (TREE_VALUE (e));
1685 if (TREE_CHAIN (e))
1686 pp_separate_with (pp, ',');
1690 /* Print out V, which contains the elements of a constructor. */
1692 void
1693 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1695 unsigned HOST_WIDE_INT ix;
1696 tree value;
1698 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1700 pp->expression (value);
1701 if (ix != vec_safe_length (v) - 1)
1702 pp_separate_with (pp, ',');
1706 /* Print out an expression-list in parens, as if it were the argument
1707 list to a function. */
1709 void
1710 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1712 pp_c_left_paren (pp);
1713 if (t && TREE_CODE (t) == TREE_LIST)
1714 pp_c_expression_list (pp, t);
1715 pp_c_right_paren (pp);
1718 /* unary-expression:
1719 postfix-expression
1720 ++ cast-expression
1721 -- cast-expression
1722 unary-operator cast-expression
1723 sizeof unary-expression
1724 sizeof ( type-id )
1726 unary-operator: one of
1727 * & + - ! ~
1729 GNU extensions.
1730 unary-expression:
1731 __alignof__ unary-expression
1732 __alignof__ ( type-id )
1733 __real__ unary-expression
1734 __imag__ unary-expression */
1736 void
1737 c_pretty_printer::unary_expression (tree e)
1739 enum tree_code code = TREE_CODE (e);
1740 switch (code)
1742 case PREINCREMENT_EXPR:
1743 case PREDECREMENT_EXPR:
1744 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1745 unary_expression (TREE_OPERAND (e, 0));
1746 break;
1748 case ADDR_EXPR:
1749 case INDIRECT_REF:
1750 case NEGATE_EXPR:
1751 case BIT_NOT_EXPR:
1752 case TRUTH_NOT_EXPR:
1753 case CONJ_EXPR:
1754 /* String literal are used by address. */
1755 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1756 pp_ampersand (this);
1757 else if (code == INDIRECT_REF)
1759 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
1760 if (type && TREE_CODE (type) == REFERENCE_TYPE)
1761 /* Reference decay is implicit, don't print anything. */;
1762 else
1763 pp_c_star (this);
1765 else if (code == NEGATE_EXPR)
1766 pp_minus (this);
1767 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1768 pp_complement (this);
1769 else if (code == TRUTH_NOT_EXPR)
1770 pp_exclamation (this);
1771 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1772 break;
1774 case MEM_REF:
1775 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1776 && integer_zerop (TREE_OPERAND (e, 1)))
1777 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1778 else
1780 pp_c_star (this);
1781 if (!integer_zerop (TREE_OPERAND (e, 1)))
1783 pp_c_left_paren (this);
1784 if (!integer_onep (TYPE_SIZE_UNIT
1785 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1786 pp_c_type_cast (this, ptr_type_node);
1788 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1789 if (!integer_zerop (TREE_OPERAND (e, 1)))
1791 pp_plus (this);
1792 pp_c_integer_constant (this,
1793 fold_convert (ssizetype,
1794 TREE_OPERAND (e, 1)));
1795 pp_c_right_paren (this);
1798 break;
1800 case REALPART_EXPR:
1801 case IMAGPART_EXPR:
1802 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1803 pp_c_whitespace (this);
1804 unary_expression (TREE_OPERAND (e, 0));
1805 break;
1807 default:
1808 postfix_expression (e);
1809 break;
1813 /* cast-expression:
1814 unary-expression
1815 ( type-name ) cast-expression */
1817 void
1818 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1820 switch (TREE_CODE (e))
1822 case FLOAT_EXPR:
1823 case FIX_TRUNC_EXPR:
1824 CASE_CONVERT:
1825 case VIEW_CONVERT_EXPR:
1826 pp_c_type_cast (pp, TREE_TYPE (e));
1827 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1828 break;
1830 default:
1831 pp->unary_expression (e);
1835 /* multiplicative-expression:
1836 cast-expression
1837 multiplicative-expression * cast-expression
1838 multiplicative-expression / cast-expression
1839 multiplicative-expression % cast-expression */
1841 void
1842 c_pretty_printer::multiplicative_expression (tree e)
1844 enum tree_code code = TREE_CODE (e);
1845 switch (code)
1847 case MULT_EXPR:
1848 case TRUNC_DIV_EXPR:
1849 case TRUNC_MOD_EXPR:
1850 multiplicative_expression (TREE_OPERAND (e, 0));
1851 pp_c_whitespace (this);
1852 if (code == MULT_EXPR)
1853 pp_c_star (this);
1854 else if (code == TRUNC_DIV_EXPR)
1855 pp_slash (this);
1856 else
1857 pp_modulo (this);
1858 pp_c_whitespace (this);
1859 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1860 break;
1862 default:
1863 pp_c_cast_expression (this, e);
1864 break;
1868 /* additive-expression:
1869 multiplicative-expression
1870 additive-expression + multiplicative-expression
1871 additive-expression - multiplicative-expression */
1873 static void
1874 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1876 enum tree_code code = TREE_CODE (e);
1877 switch (code)
1879 case POINTER_PLUS_EXPR:
1880 case PLUS_EXPR:
1881 case MINUS_EXPR:
1882 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1883 pp_c_whitespace (pp);
1884 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1885 pp_plus (pp);
1886 else
1887 pp_minus (pp);
1888 pp_c_whitespace (pp);
1889 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1890 break;
1892 default:
1893 pp->multiplicative_expression (e);
1894 break;
1898 /* additive-expression:
1899 additive-expression
1900 shift-expression << additive-expression
1901 shift-expression >> additive-expression */
1903 static void
1904 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1906 enum tree_code code = TREE_CODE (e);
1907 switch (code)
1909 case LSHIFT_EXPR:
1910 case RSHIFT_EXPR:
1911 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1912 pp_c_whitespace (pp);
1913 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1914 pp_c_whitespace (pp);
1915 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1916 break;
1918 default:
1919 pp_c_additive_expression (pp, e);
1923 /* relational-expression:
1924 shift-expression
1925 relational-expression < shift-expression
1926 relational-expression > shift-expression
1927 relational-expression <= shift-expression
1928 relational-expression >= shift-expression */
1930 static void
1931 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1933 enum tree_code code = TREE_CODE (e);
1934 switch (code)
1936 case LT_EXPR:
1937 case GT_EXPR:
1938 case LE_EXPR:
1939 case GE_EXPR:
1940 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1941 pp_c_whitespace (pp);
1942 if (code == LT_EXPR)
1943 pp_less (pp);
1944 else if (code == GT_EXPR)
1945 pp_greater (pp);
1946 else if (code == LE_EXPR)
1947 pp_less_equal (pp);
1948 else if (code == GE_EXPR)
1949 pp_greater_equal (pp);
1950 pp_c_whitespace (pp);
1951 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1952 break;
1954 default:
1955 pp_c_shift_expression (pp, e);
1956 break;
1960 /* equality-expression:
1961 relational-expression
1962 equality-expression == relational-expression
1963 equality-equality != relational-expression */
1965 static void
1966 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1968 enum tree_code code = TREE_CODE (e);
1969 switch (code)
1971 case EQ_EXPR:
1972 case NE_EXPR:
1973 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1974 pp_c_whitespace (pp);
1975 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1976 pp_c_whitespace (pp);
1977 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1978 break;
1980 default:
1981 pp_c_relational_expression (pp, e);
1982 break;
1986 /* AND-expression:
1987 equality-expression
1988 AND-expression & equality-equality */
1990 static void
1991 pp_c_and_expression (c_pretty_printer *pp, tree e)
1993 if (TREE_CODE (e) == BIT_AND_EXPR)
1995 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1996 pp_c_whitespace (pp);
1997 pp_ampersand (pp);
1998 pp_c_whitespace (pp);
1999 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2001 else
2002 pp_c_equality_expression (pp, e);
2005 /* exclusive-OR-expression:
2006 AND-expression
2007 exclusive-OR-expression ^ AND-expression */
2009 static void
2010 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2012 if (TREE_CODE (e) == BIT_XOR_EXPR
2013 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2015 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2016 if (TREE_CODE (e) == BIT_XOR_EXPR)
2017 pp_c_maybe_whitespace (pp);
2018 else
2019 pp_c_whitespace (pp);
2020 pp_carret (pp);
2021 pp_c_whitespace (pp);
2022 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2024 else
2025 pp_c_and_expression (pp, e);
2028 /* inclusive-OR-expression:
2029 exclusive-OR-expression
2030 inclusive-OR-expression | exclusive-OR-expression */
2032 static void
2033 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2035 if (TREE_CODE (e) == BIT_IOR_EXPR)
2037 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2038 pp_c_whitespace (pp);
2039 pp_bar (pp);
2040 pp_c_whitespace (pp);
2041 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2043 else
2044 pp_c_exclusive_or_expression (pp, e);
2047 /* logical-AND-expression:
2048 inclusive-OR-expression
2049 logical-AND-expression && inclusive-OR-expression */
2051 static void
2052 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2054 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2055 || TREE_CODE (e) == TRUTH_AND_EXPR)
2057 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2058 pp_c_whitespace (pp);
2059 pp_ampersand_ampersand (pp);
2060 pp_c_whitespace (pp);
2061 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2063 else
2064 pp_c_inclusive_or_expression (pp, e);
2067 /* logical-OR-expression:
2068 logical-AND-expression
2069 logical-OR-expression || logical-AND-expression */
2071 void
2072 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2074 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2075 || TREE_CODE (e) == TRUTH_OR_EXPR)
2077 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2078 pp_c_whitespace (pp);
2079 pp_bar_bar (pp);
2080 pp_c_whitespace (pp);
2081 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2083 else
2084 pp_c_logical_and_expression (pp, e);
2087 /* conditional-expression:
2088 logical-OR-expression
2089 logical-OR-expression ? expression : conditional-expression */
2091 void
2092 c_pretty_printer::conditional_expression (tree e)
2094 if (TREE_CODE (e) == COND_EXPR)
2096 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2097 pp_c_whitespace (this);
2098 pp_question (this);
2099 pp_c_whitespace (this);
2100 expression (TREE_OPERAND (e, 1));
2101 pp_c_whitespace (this);
2102 pp_colon (this);
2103 pp_c_whitespace (this);
2104 conditional_expression (TREE_OPERAND (e, 2));
2106 else
2107 pp_c_logical_or_expression (this, e);
2111 /* assignment-expression:
2112 conditional-expression
2113 unary-expression assignment-operator assignment-expression
2115 assignment-expression: one of
2116 = *= /= %= += -= >>= <<= &= ^= |= */
2118 void
2119 c_pretty_printer::assignment_expression (tree e)
2121 if (TREE_CODE (e) == MODIFY_EXPR
2122 || TREE_CODE (e) == INIT_EXPR)
2124 unary_expression (TREE_OPERAND (e, 0));
2125 pp_c_whitespace (this);
2126 pp_equal (this);
2127 pp_space (this);
2128 expression (TREE_OPERAND (e, 1));
2130 else
2131 conditional_expression (e);
2134 /* expression:
2135 assignment-expression
2136 expression , assignment-expression
2138 Implementation note: instead of going through the usual recursion
2139 chain, I take the liberty of dispatching nodes to the appropriate
2140 functions. This makes some redundancy, but it worths it. That also
2141 prevents a possible infinite recursion between primary_expression ()
2142 and expression (). */
2144 void
2145 c_pretty_printer::expression (tree e)
2147 switch (TREE_CODE (e))
2149 case VOID_CST:
2150 pp_c_void_constant (this);
2151 break;
2153 case INTEGER_CST:
2154 pp_c_integer_constant (this, e);
2155 break;
2157 case REAL_CST:
2158 pp_c_floating_constant (this, e);
2159 break;
2161 case FIXED_CST:
2162 pp_c_fixed_constant (this, e);
2163 break;
2165 case STRING_CST:
2166 pp_c_string_literal (this, e);
2167 break;
2169 case IDENTIFIER_NODE:
2170 case FUNCTION_DECL:
2171 case VAR_DECL:
2172 case CONST_DECL:
2173 case PARM_DECL:
2174 case RESULT_DECL:
2175 case FIELD_DECL:
2176 case LABEL_DECL:
2177 case ERROR_MARK:
2178 primary_expression (e);
2179 break;
2181 case SSA_NAME:
2182 if (SSA_NAME_VAR (e)
2183 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2184 expression (SSA_NAME_VAR (e));
2185 else
2186 translate_string ("<unknown>");
2187 break;
2189 case POSTINCREMENT_EXPR:
2190 case POSTDECREMENT_EXPR:
2191 case ARRAY_REF:
2192 case ARRAY_NOTATION_REF:
2193 case CALL_EXPR:
2194 case COMPONENT_REF:
2195 case BIT_FIELD_REF:
2196 case COMPLEX_CST:
2197 case COMPLEX_EXPR:
2198 case VECTOR_CST:
2199 case ORDERED_EXPR:
2200 case UNORDERED_EXPR:
2201 case LTGT_EXPR:
2202 case UNEQ_EXPR:
2203 case UNLE_EXPR:
2204 case UNLT_EXPR:
2205 case UNGE_EXPR:
2206 case UNGT_EXPR:
2207 case ABS_EXPR:
2208 case CONSTRUCTOR:
2209 case COMPOUND_LITERAL_EXPR:
2210 case VA_ARG_EXPR:
2211 postfix_expression (e);
2212 break;
2214 case CONJ_EXPR:
2215 case ADDR_EXPR:
2216 case INDIRECT_REF:
2217 case MEM_REF:
2218 case NEGATE_EXPR:
2219 case BIT_NOT_EXPR:
2220 case TRUTH_NOT_EXPR:
2221 case PREINCREMENT_EXPR:
2222 case PREDECREMENT_EXPR:
2223 case REALPART_EXPR:
2224 case IMAGPART_EXPR:
2225 unary_expression (e);
2226 break;
2228 case FLOAT_EXPR:
2229 case FIX_TRUNC_EXPR:
2230 CASE_CONVERT:
2231 case VIEW_CONVERT_EXPR:
2232 pp_c_cast_expression (this, e);
2233 break;
2235 case MULT_EXPR:
2236 case TRUNC_MOD_EXPR:
2237 case TRUNC_DIV_EXPR:
2238 multiplicative_expression (e);
2239 break;
2241 case LSHIFT_EXPR:
2242 case RSHIFT_EXPR:
2243 pp_c_shift_expression (this, e);
2244 break;
2246 case LT_EXPR:
2247 case GT_EXPR:
2248 case LE_EXPR:
2249 case GE_EXPR:
2250 pp_c_relational_expression (this, e);
2251 break;
2253 case BIT_AND_EXPR:
2254 pp_c_and_expression (this, e);
2255 break;
2257 case BIT_XOR_EXPR:
2258 case TRUTH_XOR_EXPR:
2259 pp_c_exclusive_or_expression (this, e);
2260 break;
2262 case BIT_IOR_EXPR:
2263 pp_c_inclusive_or_expression (this, e);
2264 break;
2266 case TRUTH_ANDIF_EXPR:
2267 case TRUTH_AND_EXPR:
2268 pp_c_logical_and_expression (this, e);
2269 break;
2271 case TRUTH_ORIF_EXPR:
2272 case TRUTH_OR_EXPR:
2273 pp_c_logical_or_expression (this, e);
2274 break;
2276 case EQ_EXPR:
2277 case NE_EXPR:
2278 pp_c_equality_expression (this, e);
2279 break;
2281 case COND_EXPR:
2282 conditional_expression (e);
2283 break;
2285 case POINTER_PLUS_EXPR:
2286 case PLUS_EXPR:
2287 case MINUS_EXPR:
2288 pp_c_additive_expression (this, e);
2289 break;
2291 case MODIFY_EXPR:
2292 case INIT_EXPR:
2293 assignment_expression (e);
2294 break;
2296 case COMPOUND_EXPR:
2297 pp_c_left_paren (this);
2298 expression (TREE_OPERAND (e, 0));
2299 pp_separate_with (this, ',');
2300 assignment_expression (TREE_OPERAND (e, 1));
2301 pp_c_right_paren (this);
2302 break;
2304 case NON_LVALUE_EXPR:
2305 case SAVE_EXPR:
2306 expression (TREE_OPERAND (e, 0));
2307 break;
2309 case TARGET_EXPR:
2310 postfix_expression (TREE_OPERAND (e, 1));
2311 break;
2313 case BIND_EXPR:
2314 case GOTO_EXPR:
2315 /* We don't yet have a way of dumping statements in a
2316 human-readable format. */
2317 pp_string (this, "({...})");
2318 break;
2320 case C_MAYBE_CONST_EXPR:
2321 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2322 break;
2324 default:
2325 pp_unsupported_tree (this, e);
2326 break;
2332 /* Statements. */
2334 void
2335 c_pretty_printer::statement (tree stmt)
2337 if (stmt == NULL)
2338 return;
2340 if (pp_needs_newline (this))
2341 pp_newline_and_indent (this, 0);
2343 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2347 /* Initialize the PRETTY-PRINTER for handling C codes. */
2349 c_pretty_printer::c_pretty_printer ()
2350 : pretty_printer (),
2351 offset_list (),
2352 flags ()
2354 type_specifier_seq = pp_c_specifier_qualifier_list;
2355 ptr_operator = pp_c_pointer;
2356 parameter_list = pp_c_parameter_type_list;
2360 /* Print the tree T in full, on file FILE. */
2362 void
2363 print_c_tree (FILE *file, tree t)
2365 c_pretty_printer pp;
2367 pp_needs_newline (&pp) = true;
2368 pp.buffer->stream = file;
2369 pp.statement (t);
2370 pp_newline_and_flush (&pp);
2373 /* Print the tree T in full, on stderr. */
2375 DEBUG_FUNCTION void
2376 debug_c_tree (tree t)
2378 print_c_tree (stderr, t);
2379 fputc ('\n', stderr);
2382 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2383 up of T's memory address. */
2385 void
2386 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2388 const char *name;
2390 gcc_assert (DECL_P (t));
2392 if (DECL_NAME (t))
2393 name = IDENTIFIER_POINTER (DECL_NAME (t));
2394 else
2396 static char xname[8];
2397 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2398 name = xname;
2401 pp_c_identifier (pp, name);