PR82045: Avoid passing machine modes through "..."
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blobb8b8f665ef35eebd98e4d073451c1e3dc680c3d6
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2017 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 "c-pretty-print.h"
25 #include "diagnostic.h"
26 #include "stor-layout.h"
27 #include "stringpool.h"
28 #include "attribs.h"
29 #include "intl.h"
30 #include "tree-pretty-print.h"
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
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. */
40 #define pp_c_maybe_whitespace(PP) \
41 do { \
42 if ((PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
44 } while (0)
46 /* literal */
47 static void pp_c_char (c_pretty_printer *, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer *, tree);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
53 static void pp_c_additive_expression (c_pretty_printer *, tree);
54 static void pp_c_shift_expression (c_pretty_printer *, tree);
55 static void pp_c_relational_expression (c_pretty_printer *, tree);
56 static void pp_c_equality_expression (c_pretty_printer *, tree);
57 static void pp_c_and_expression (c_pretty_printer *, tree);
58 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
59 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62 /* declarations. */
65 /* Helper functions. */
67 void
68 pp_c_whitespace (c_pretty_printer *pp)
70 pp_space (pp);
71 pp->padding = pp_none;
74 void
75 pp_c_left_paren (c_pretty_printer *pp)
77 pp_left_paren (pp);
78 pp->padding = pp_none;
81 void
82 pp_c_right_paren (c_pretty_printer *pp)
84 pp_right_paren (pp);
85 pp->padding = pp_none;
88 void
89 pp_c_left_brace (c_pretty_printer *pp)
91 pp_left_brace (pp);
92 pp->padding = pp_none;
95 void
96 pp_c_right_brace (c_pretty_printer *pp)
98 pp_right_brace (pp);
99 pp->padding = pp_none;
102 void
103 pp_c_left_bracket (c_pretty_printer *pp)
105 pp_left_bracket (pp);
106 pp->padding = pp_none;
109 void
110 pp_c_right_bracket (c_pretty_printer *pp)
112 pp_right_bracket (pp);
113 pp->padding = pp_none;
116 void
117 pp_c_dot (c_pretty_printer *pp)
119 pp_dot (pp);
120 pp->padding = pp_none;
123 void
124 pp_c_ampersand (c_pretty_printer *pp)
126 pp_ampersand (pp);
127 pp->padding = pp_none;
130 void
131 pp_c_star (c_pretty_printer *pp)
133 pp_star (pp);
134 pp->padding = pp_none;
137 void
138 pp_c_arrow (c_pretty_printer *pp)
140 pp_arrow (pp);
141 pp->padding = pp_none;
144 void
145 pp_c_semicolon (c_pretty_printer *pp)
147 pp_semicolon (pp);
148 pp->padding = pp_none;
151 void
152 pp_c_complement (c_pretty_printer *pp)
154 pp_complement (pp);
155 pp->padding = pp_none;
158 void
159 pp_c_exclamation (c_pretty_printer *pp)
161 pp_exclamation (pp);
162 pp->padding = pp_none;
165 /* Print out the external representation of QUALIFIERS. */
167 void
168 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
170 const char *p = pp_last_position_in_text (pp);
172 if (!qualifiers)
173 return;
175 /* The C programming language does not have references, but it is much
176 simpler to handle those here rather than going through the same
177 logic in the C++ pretty-printer. */
178 if (p != NULL && (*p == '*' || *p == '&'))
179 pp_c_whitespace (pp);
181 if (qualifiers & TYPE_QUAL_ATOMIC)
182 pp_c_ws_string (pp, "_Atomic");
183 if (qualifiers & TYPE_QUAL_CONST)
184 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
185 if (qualifiers & TYPE_QUAL_VOLATILE)
186 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
187 if (qualifiers & TYPE_QUAL_RESTRICT)
188 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
189 ? "restrict" : "__restrict__"));
192 /* Pretty-print T using the type-cast notation '( type-name )'. */
194 static void
195 pp_c_type_cast (c_pretty_printer *pp, tree t)
197 pp_c_left_paren (pp);
198 pp->type_id (t);
199 pp_c_right_paren (pp);
202 /* We're about to pretty-print a pointer type as indicated by T.
203 Output a whitespace, if needed, preparing for subsequent output. */
205 void
206 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
208 if (POINTER_TYPE_P (t))
210 tree pointee = strip_pointer_operator (TREE_TYPE (t));
211 if (TREE_CODE (pointee) != ARRAY_TYPE
212 && TREE_CODE (pointee) != FUNCTION_TYPE)
213 pp_c_whitespace (pp);
218 /* Declarations. */
220 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
221 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
222 of its type. Take care of possible extensions.
224 type-qualifier-list:
225 type-qualifier
226 type-qualifier-list type-qualifier
228 type-qualifier:
229 const
230 restrict -- C99
231 __restrict__ -- GNU C
232 address-space-qualifier -- GNU C
233 volatile
234 _Atomic -- C11
236 address-space-qualifier:
237 identifier -- GNU C */
239 void
240 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
242 int qualifiers;
244 if (!t || t == error_mark_node)
245 return;
247 if (!TYPE_P (t))
248 t = TREE_TYPE (t);
250 qualifiers = TYPE_QUALS (t);
251 pp_c_cv_qualifiers (pp, qualifiers,
252 TREE_CODE (t) == FUNCTION_TYPE);
254 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
256 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
257 pp_c_identifier (pp, as);
261 /* pointer:
262 * type-qualifier-list(opt)
263 * type-qualifier-list(opt) pointer */
265 static void
266 pp_c_pointer (c_pretty_printer *pp, tree t)
268 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
269 t = TREE_TYPE (t);
270 switch (TREE_CODE (t))
272 case POINTER_TYPE:
273 /* It is easier to handle C++ reference types here. */
274 case REFERENCE_TYPE:
275 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
276 pp_c_pointer (pp, TREE_TYPE (t));
277 if (TREE_CODE (t) == POINTER_TYPE)
278 pp_c_star (pp);
279 else
280 pp_c_ampersand (pp);
281 pp_c_type_qualifier_list (pp, t);
282 break;
284 /* ??? This node is now in GENERIC and so shouldn't be here. But
285 we'll fix that later. */
286 case DECL_EXPR:
287 pp->declaration (DECL_EXPR_DECL (t));
288 pp_needs_newline (pp) = true;
289 break;
291 default:
292 pp_unsupported_tree (pp, t);
296 /* simple-type-specifier:
297 type-specifier
299 type-specifier:
300 void
301 char
302 short
304 long
305 float
306 double
307 signed
308 unsigned
309 _Bool -- C99
310 _Complex -- C99
311 _Imaginary -- C99
312 struct-or-union-specifier
313 enum-specifier
314 typedef-name.
316 GNU extensions.
317 simple-type-specifier:
318 __complex__
319 __vector__ */
321 void
322 c_pretty_printer::simple_type_specifier (tree t)
324 const enum tree_code code = TREE_CODE (t);
325 switch (code)
327 case ERROR_MARK:
328 translate_string ("<type-error>");
329 break;
331 case IDENTIFIER_NODE:
332 pp_c_identifier (this, IDENTIFIER_POINTER (t));
333 break;
335 case VOID_TYPE:
336 case BOOLEAN_TYPE:
337 case INTEGER_TYPE:
338 case REAL_TYPE:
339 case FIXED_POINT_TYPE:
340 if (TYPE_NAME (t))
342 t = TYPE_NAME (t);
343 simple_type_specifier (t);
345 else
347 int prec = TYPE_PRECISION (t);
348 tree common_t;
349 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
350 common_t = c_common_type_for_mode (TYPE_MODE (t),
351 TYPE_SATURATING (t));
352 else
353 common_t = c_common_type_for_mode (TYPE_MODE (t),
354 TYPE_UNSIGNED (t));
355 if (common_t && TYPE_NAME (common_t))
357 simple_type_specifier (common_t);
358 if (TYPE_PRECISION (common_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 if (tree_fits_shwi_p (i))
909 pp_wide_integer (pp, tree_to_shwi (i));
910 else if (tree_fits_uhwi_p (i))
911 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
912 else
914 wide_int wi = i;
916 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
918 pp_minus (pp);
919 wi = -wi;
921 print_hex (wi, pp_buffer (pp)->digit_buffer);
922 pp_string (pp, pp_buffer (pp)->digit_buffer);
926 /* Print out a CHARACTER literal. */
928 static void
929 pp_c_character_constant (c_pretty_printer *pp, tree c)
931 pp_quote (pp);
932 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
933 pp_quote (pp);
936 /* Print out a BOOLEAN literal. */
938 static void
939 pp_c_bool_constant (c_pretty_printer *pp, tree b)
941 if (b == boolean_false_node)
943 if (c_dialect_cxx ())
944 pp_c_ws_string (pp, "false");
945 else if (flag_isoc99)
946 pp_c_ws_string (pp, "_False");
947 else
948 pp_unsupported_tree (pp, b);
950 else if (b == boolean_true_node)
952 if (c_dialect_cxx ())
953 pp_c_ws_string (pp, "true");
954 else if (flag_isoc99)
955 pp_c_ws_string (pp, "_True");
956 else
957 pp_unsupported_tree (pp, b);
959 else if (TREE_CODE (b) == INTEGER_CST)
960 pp_c_integer_constant (pp, b);
961 else
962 pp_unsupported_tree (pp, b);
965 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
966 false; that means the value was obtained by a cast, in which case
967 print out the type-id part of the cast-expression -- the casted value
968 is then printed by pp_c_integer_literal. */
970 static bool
971 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
973 bool value_is_named = true;
974 tree type = TREE_TYPE (e);
975 tree value;
977 /* Find the name of this constant. */
978 for (value = TYPE_VALUES (type);
979 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
980 value = TREE_CHAIN (value))
983 if (value != NULL_TREE)
984 pp->id_expression (TREE_PURPOSE (value));
985 else
987 /* Value must have been cast. */
988 pp_c_type_cast (pp, type);
989 value_is_named = false;
992 return value_is_named;
995 /* Print out a REAL value as a decimal-floating-constant. */
997 static void
998 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1000 const struct real_format *fmt
1001 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1003 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1004 bool is_decimal = floating_cst.decimal;
1006 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1007 log10(2) to 7 significant digits. */
1008 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1010 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1011 sizeof (pp_buffer (pp)->digit_buffer),
1012 max_digits10, 1);
1014 pp_string (pp, pp_buffer(pp)->digit_buffer);
1015 if (TREE_TYPE (r) == float_type_node)
1016 pp_character (pp, 'f');
1017 else if (TREE_TYPE (r) == long_double_type_node)
1018 pp_character (pp, 'l');
1019 else if (TREE_TYPE (r) == dfloat128_type_node)
1020 pp_string (pp, "dl");
1021 else if (TREE_TYPE (r) == dfloat64_type_node)
1022 pp_string (pp, "dd");
1023 else if (TREE_TYPE (r) == dfloat32_type_node)
1024 pp_string (pp, "df");
1025 else if (TREE_TYPE (r) != double_type_node)
1026 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1027 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i))
1029 pp_character (pp, 'f');
1030 pp_decimal_int (pp, floatn_nx_types[i].n);
1031 if (floatn_nx_types[i].extended)
1032 pp_character (pp, 'x');
1033 break;
1037 /* Print out a FIXED value as a decimal-floating-constant. */
1039 static void
1040 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1042 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1043 sizeof (pp_buffer (pp)->digit_buffer));
1044 pp_string (pp, pp_buffer(pp)->digit_buffer);
1047 /* Pretty-print a compound literal expression. GNU extensions include
1048 vector constants. */
1050 static void
1051 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1053 tree type = TREE_TYPE (e);
1054 pp_c_type_cast (pp, type);
1056 switch (TREE_CODE (type))
1058 case RECORD_TYPE:
1059 case UNION_TYPE:
1060 case ARRAY_TYPE:
1061 case VECTOR_TYPE:
1062 case COMPLEX_TYPE:
1063 pp_c_brace_enclosed_initializer_list (pp, e);
1064 break;
1066 default:
1067 pp_unsupported_tree (pp, e);
1068 break;
1072 /* Pretty-print a COMPLEX_EXPR expression. */
1074 static void
1075 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1077 /* Handle a few common special cases, otherwise fallback
1078 to printing it as compound literal. */
1079 tree type = TREE_TYPE (e);
1080 tree realexpr = TREE_OPERAND (e, 0);
1081 tree imagexpr = TREE_OPERAND (e, 1);
1083 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1084 if (TREE_CODE (realexpr) == NOP_EXPR
1085 && TREE_CODE (imagexpr) == NOP_EXPR
1086 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1087 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1088 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1089 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1090 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1091 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1093 pp_c_type_cast (pp, type);
1094 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1095 return;
1098 /* Cast of an scalar expression to COMPLEX_TYPE. */
1099 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1100 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1102 pp_c_type_cast (pp, type);
1103 if (TREE_CODE (realexpr) == NOP_EXPR)
1104 realexpr = TREE_OPERAND (realexpr, 0);
1105 pp->expression (realexpr);
1106 return;
1109 pp_c_compound_literal (pp, e);
1112 /* constant:
1113 integer-constant
1114 floating-constant
1115 fixed-point-constant
1116 enumeration-constant
1117 character-constant */
1119 void
1120 c_pretty_printer::constant (tree e)
1122 const enum tree_code code = TREE_CODE (e);
1124 switch (code)
1126 case VOID_CST:
1127 pp_c_void_constant (this);
1128 break;
1130 case INTEGER_CST:
1132 tree type = TREE_TYPE (e);
1133 if (type == boolean_type_node)
1134 pp_c_bool_constant (this, e);
1135 else if (type == char_type_node)
1136 pp_c_character_constant (this, e);
1137 else if (TREE_CODE (type) == ENUMERAL_TYPE
1138 && pp_c_enumeration_constant (this, e))
1140 else
1141 pp_c_integer_constant (this, e);
1143 break;
1145 case REAL_CST:
1146 pp_c_floating_constant (this, e);
1147 break;
1149 case FIXED_CST:
1150 pp_c_fixed_constant (this, e);
1151 break;
1153 case STRING_CST:
1154 pp_c_string_literal (this, e);
1155 break;
1157 case COMPLEX_CST:
1158 /* Sometimes, we are confused and we think a complex literal
1159 is a constant. Such thing is a compound literal which
1160 grammatically belongs to postfix-expr production. */
1161 pp_c_compound_literal (this, e);
1162 break;
1164 default:
1165 pp_unsupported_tree (this, e);
1166 break;
1170 /* Pretty-print a string such as an identifier, without changing its
1171 encoding, preceded by whitespace is necessary. */
1173 void
1174 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1176 pp_c_maybe_whitespace (pp);
1177 pp_string (pp, str);
1178 pp->padding = pp_before;
1181 void
1182 c_pretty_printer::translate_string (const char *gmsgid)
1184 if (pp_translate_identifiers (this))
1185 pp_c_ws_string (this, _(gmsgid));
1186 else
1187 pp_c_ws_string (this, gmsgid);
1190 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1191 that need converting to the locale encoding, preceded by whitespace
1192 is necessary. */
1194 void
1195 pp_c_identifier (c_pretty_printer *pp, const char *id)
1197 pp_c_maybe_whitespace (pp);
1198 pp_identifier (pp, id);
1199 pp->padding = pp_before;
1202 /* Pretty-print a C primary-expression.
1203 primary-expression:
1204 identifier
1205 constant
1206 string-literal
1207 ( expression ) */
1209 void
1210 c_pretty_printer::primary_expression (tree e)
1212 switch (TREE_CODE (e))
1214 case VAR_DECL:
1215 case PARM_DECL:
1216 case FIELD_DECL:
1217 case CONST_DECL:
1218 case FUNCTION_DECL:
1219 case LABEL_DECL:
1220 pp_c_tree_decl_identifier (this, e);
1221 break;
1223 case IDENTIFIER_NODE:
1224 pp_c_tree_identifier (this, e);
1225 break;
1227 case ERROR_MARK:
1228 translate_string ("<erroneous-expression>");
1229 break;
1231 case RESULT_DECL:
1232 translate_string ("<return-value>");
1233 break;
1235 case VOID_CST:
1236 case INTEGER_CST:
1237 case REAL_CST:
1238 case FIXED_CST:
1239 case STRING_CST:
1240 constant (e);
1241 break;
1243 case TARGET_EXPR:
1244 pp_c_ws_string (this, "__builtin_memcpy");
1245 pp_c_left_paren (this);
1246 pp_ampersand (this);
1247 primary_expression (TREE_OPERAND (e, 0));
1248 pp_separate_with (this, ',');
1249 pp_ampersand (this);
1250 initializer (TREE_OPERAND (e, 1));
1251 if (TREE_OPERAND (e, 2))
1253 pp_separate_with (this, ',');
1254 expression (TREE_OPERAND (e, 2));
1256 pp_c_right_paren (this);
1257 break;
1259 default:
1260 /* FIXME: Make sure we won't get into an infinite loop. */
1261 pp_c_left_paren (this);
1262 expression (e);
1263 pp_c_right_paren (this);
1264 break;
1268 /* Print out a C initializer -- also support C compound-literals.
1269 initializer:
1270 assignment-expression:
1271 { initializer-list }
1272 { initializer-list , } */
1274 void
1275 c_pretty_printer::initializer (tree e)
1277 if (TREE_CODE (e) == CONSTRUCTOR)
1278 pp_c_brace_enclosed_initializer_list (this, e);
1279 else
1280 expression (e);
1283 /* init-declarator:
1284 declarator:
1285 declarator = initializer */
1287 void
1288 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1290 pp->declarator (t);
1291 /* We don't want to output function definitions here. There are handled
1292 elsewhere (and the syntactic form is bogus anyway). */
1293 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1295 tree init = DECL_INITIAL (t);
1296 /* This C++ bit is handled here because it is easier to do so.
1297 In templates, the C++ parser builds a TREE_LIST for a
1298 direct-initialization; the TREE_PURPOSE is the variable to
1299 initialize and the TREE_VALUE is the initializer. */
1300 if (TREE_CODE (init) == TREE_LIST)
1302 pp_c_left_paren (pp);
1303 pp->expression (TREE_VALUE (init));
1304 pp_right_paren (pp);
1306 else
1308 pp_space (pp);
1309 pp_equal (pp);
1310 pp_space (pp);
1311 pp->initializer (init);
1316 /* initializer-list:
1317 designation(opt) initializer
1318 initializer-list , designation(opt) initializer
1320 designation:
1321 designator-list =
1323 designator-list:
1324 designator
1325 designator-list designator
1327 designator:
1328 [ constant-expression ]
1329 identifier */
1331 static void
1332 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1334 tree type = TREE_TYPE (e);
1335 const enum tree_code code = TREE_CODE (type);
1337 if (TREE_CODE (e) == CONSTRUCTOR)
1339 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1340 return;
1343 switch (code)
1345 case RECORD_TYPE:
1346 case UNION_TYPE:
1347 case ARRAY_TYPE:
1349 tree init = TREE_OPERAND (e, 0);
1350 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1352 if (code == RECORD_TYPE || code == UNION_TYPE)
1354 pp_c_dot (pp);
1355 pp->primary_expression (TREE_PURPOSE (init));
1357 else
1359 pp_c_left_bracket (pp);
1360 if (TREE_PURPOSE (init))
1361 pp->constant (TREE_PURPOSE (init));
1362 pp_c_right_bracket (pp);
1364 pp_c_whitespace (pp);
1365 pp_equal (pp);
1366 pp_c_whitespace (pp);
1367 pp->initializer (TREE_VALUE (init));
1368 if (TREE_CHAIN (init))
1369 pp_separate_with (pp, ',');
1372 return;
1374 case VECTOR_TYPE:
1375 if (TREE_CODE (e) == VECTOR_CST)
1377 unsigned i;
1378 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1380 if (i > 0)
1381 pp_separate_with (pp, ',');
1382 pp->expression (VECTOR_CST_ELT (e, i));
1385 else
1386 break;
1387 return;
1389 case COMPLEX_TYPE:
1390 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1392 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1393 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1394 pp_separate_with (pp, ',');
1395 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1397 else
1398 break;
1399 return;
1401 default:
1402 break;
1405 pp_unsupported_tree (pp, type);
1408 /* Pretty-print a brace-enclosed initializer-list. */
1410 static void
1411 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1413 pp_c_left_brace (pp);
1414 pp_c_initializer_list (pp, l);
1415 pp_c_right_brace (pp);
1419 /* This is a convenient function, used to bridge gap between C and C++
1420 grammars.
1422 id-expression:
1423 identifier */
1425 void
1426 c_pretty_printer::id_expression (tree t)
1428 switch (TREE_CODE (t))
1430 case VAR_DECL:
1431 case PARM_DECL:
1432 case CONST_DECL:
1433 case TYPE_DECL:
1434 case FUNCTION_DECL:
1435 case FIELD_DECL:
1436 case LABEL_DECL:
1437 pp_c_tree_decl_identifier (this, t);
1438 break;
1440 case IDENTIFIER_NODE:
1441 pp_c_tree_identifier (this, t);
1442 break;
1444 default:
1445 pp_unsupported_tree (this, t);
1446 break;
1450 /* postfix-expression:
1451 primary-expression
1452 postfix-expression [ expression ]
1453 postfix-expression ( argument-expression-list(opt) )
1454 postfix-expression . identifier
1455 postfix-expression -> identifier
1456 postfix-expression ++
1457 postfix-expression --
1458 ( type-name ) { initializer-list }
1459 ( type-name ) { initializer-list , } */
1461 void
1462 c_pretty_printer::postfix_expression (tree e)
1464 enum tree_code code = TREE_CODE (e);
1465 switch (code)
1467 case POSTINCREMENT_EXPR:
1468 case POSTDECREMENT_EXPR:
1469 postfix_expression (TREE_OPERAND (e, 0));
1470 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1471 break;
1473 case ARRAY_REF:
1474 postfix_expression (TREE_OPERAND (e, 0));
1475 pp_c_left_bracket (this);
1476 expression (TREE_OPERAND (e, 1));
1477 pp_c_right_bracket (this);
1478 break;
1480 case ARRAY_NOTATION_REF:
1481 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1482 pp_c_left_bracket (this);
1483 expression (ARRAY_NOTATION_START (e));
1484 pp_colon (this);
1485 expression (ARRAY_NOTATION_LENGTH (e));
1486 pp_colon (this);
1487 expression (ARRAY_NOTATION_STRIDE (e));
1488 pp_c_right_bracket (this);
1489 break;
1491 case CALL_EXPR:
1493 call_expr_arg_iterator iter;
1494 tree arg;
1495 postfix_expression (CALL_EXPR_FN (e));
1496 pp_c_left_paren (this);
1497 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1499 expression (arg);
1500 if (more_call_expr_args_p (&iter))
1501 pp_separate_with (this, ',');
1503 pp_c_right_paren (this);
1504 break;
1507 case UNORDERED_EXPR:
1508 pp_c_ws_string (this, flag_isoc99
1509 ? "isunordered"
1510 : "__builtin_isunordered");
1511 goto two_args_fun;
1513 case ORDERED_EXPR:
1514 pp_c_ws_string (this, flag_isoc99
1515 ? "!isunordered"
1516 : "!__builtin_isunordered");
1517 goto two_args_fun;
1519 case UNLT_EXPR:
1520 pp_c_ws_string (this, flag_isoc99
1521 ? "!isgreaterequal"
1522 : "!__builtin_isgreaterequal");
1523 goto two_args_fun;
1525 case UNLE_EXPR:
1526 pp_c_ws_string (this, flag_isoc99
1527 ? "!isgreater"
1528 : "!__builtin_isgreater");
1529 goto two_args_fun;
1531 case UNGT_EXPR:
1532 pp_c_ws_string (this, flag_isoc99
1533 ? "!islessequal"
1534 : "!__builtin_islessequal");
1535 goto two_args_fun;
1537 case UNGE_EXPR:
1538 pp_c_ws_string (this, flag_isoc99
1539 ? "!isless"
1540 : "!__builtin_isless");
1541 goto two_args_fun;
1543 case UNEQ_EXPR:
1544 pp_c_ws_string (this, flag_isoc99
1545 ? "!islessgreater"
1546 : "!__builtin_islessgreater");
1547 goto two_args_fun;
1549 case LTGT_EXPR:
1550 pp_c_ws_string (this, flag_isoc99
1551 ? "islessgreater"
1552 : "__builtin_islessgreater");
1553 goto two_args_fun;
1555 case MAX_EXPR:
1556 pp_c_ws_string (this, "max");
1557 goto two_args_fun;
1559 case MIN_EXPR:
1560 pp_c_ws_string (this, "min");
1561 goto two_args_fun;
1563 two_args_fun:
1564 pp_c_left_paren (this);
1565 expression (TREE_OPERAND (e, 0));
1566 pp_separate_with (this, ',');
1567 expression (TREE_OPERAND (e, 1));
1568 pp_c_right_paren (this);
1569 break;
1571 case ABS_EXPR:
1572 pp_c_ws_string (this, "__builtin_abs");
1573 pp_c_left_paren (this);
1574 expression (TREE_OPERAND (e, 0));
1575 pp_c_right_paren (this);
1576 break;
1578 case COMPONENT_REF:
1580 tree object = TREE_OPERAND (e, 0);
1581 if (INDIRECT_REF_P (object))
1583 postfix_expression (TREE_OPERAND (object, 0));
1584 pp_c_arrow (this);
1586 else
1588 postfix_expression (object);
1589 pp_c_dot (this);
1591 expression (TREE_OPERAND (e, 1));
1593 break;
1595 case BIT_FIELD_REF:
1597 tree type = TREE_TYPE (e);
1599 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1600 if (type
1601 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1603 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1604 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1605 if ((bitpos % size) == 0)
1607 pp_c_left_paren (this);
1608 pp_c_left_paren (this);
1609 type_id (type);
1610 pp_c_star (this);
1611 pp_c_right_paren (this);
1612 pp_c_ampersand (this);
1613 expression (TREE_OPERAND (e, 0));
1614 pp_c_right_paren (this);
1615 pp_c_left_bracket (this);
1616 pp_wide_integer (this, bitpos / size);
1617 pp_c_right_bracket (this);
1618 break;
1621 pp_unsupported_tree (this, e);
1623 break;
1625 case MEM_REF:
1626 expression (e);
1627 break;
1629 case COMPLEX_CST:
1630 case VECTOR_CST:
1631 pp_c_compound_literal (this, e);
1632 break;
1634 case COMPLEX_EXPR:
1635 pp_c_complex_expr (this, e);
1636 break;
1638 case COMPOUND_LITERAL_EXPR:
1639 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1640 /* Fall through. */
1641 case CONSTRUCTOR:
1642 initializer (e);
1643 break;
1645 case VA_ARG_EXPR:
1646 pp_c_ws_string (this, "__builtin_va_arg");
1647 pp_c_left_paren (this);
1648 assignment_expression (TREE_OPERAND (e, 0));
1649 pp_separate_with (this, ',');
1650 type_id (TREE_TYPE (e));
1651 pp_c_right_paren (this);
1652 break;
1654 case ADDR_EXPR:
1655 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1657 id_expression (TREE_OPERAND (e, 0));
1658 break;
1660 /* fall through. */
1662 default:
1663 primary_expression (e);
1664 break;
1668 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1670 void
1671 pp_c_expression_list (c_pretty_printer *pp, tree e)
1673 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1675 pp->expression (TREE_VALUE (e));
1676 if (TREE_CHAIN (e))
1677 pp_separate_with (pp, ',');
1681 /* Print out V, which contains the elements of a constructor. */
1683 void
1684 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1686 unsigned HOST_WIDE_INT ix;
1687 tree value;
1689 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1691 pp->expression (value);
1692 if (ix != vec_safe_length (v) - 1)
1693 pp_separate_with (pp, ',');
1697 /* Print out an expression-list in parens, as if it were the argument
1698 list to a function. */
1700 void
1701 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1703 pp_c_left_paren (pp);
1704 if (t && TREE_CODE (t) == TREE_LIST)
1705 pp_c_expression_list (pp, t);
1706 pp_c_right_paren (pp);
1709 /* unary-expression:
1710 postfix-expression
1711 ++ cast-expression
1712 -- cast-expression
1713 unary-operator cast-expression
1714 sizeof unary-expression
1715 sizeof ( type-id )
1717 unary-operator: one of
1718 * & + - ! ~
1720 GNU extensions.
1721 unary-expression:
1722 __alignof__ unary-expression
1723 __alignof__ ( type-id )
1724 __real__ unary-expression
1725 __imag__ unary-expression */
1727 void
1728 c_pretty_printer::unary_expression (tree e)
1730 enum tree_code code = TREE_CODE (e);
1731 switch (code)
1733 case PREINCREMENT_EXPR:
1734 case PREDECREMENT_EXPR:
1735 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1736 unary_expression (TREE_OPERAND (e, 0));
1737 break;
1739 case ADDR_EXPR:
1740 case INDIRECT_REF:
1741 case NEGATE_EXPR:
1742 case BIT_NOT_EXPR:
1743 case TRUTH_NOT_EXPR:
1744 case CONJ_EXPR:
1745 /* String literal are used by address. */
1746 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1747 pp_ampersand (this);
1748 else if (code == INDIRECT_REF)
1750 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
1751 if (type && TREE_CODE (type) == REFERENCE_TYPE)
1752 /* Reference decay is implicit, don't print anything. */;
1753 else
1754 pp_c_star (this);
1756 else if (code == NEGATE_EXPR)
1757 pp_minus (this);
1758 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1759 pp_complement (this);
1760 else if (code == TRUTH_NOT_EXPR)
1761 pp_exclamation (this);
1762 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1763 break;
1765 case MEM_REF:
1766 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1767 && integer_zerop (TREE_OPERAND (e, 1)))
1768 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1769 else
1771 pp_c_star (this);
1772 if (!integer_zerop (TREE_OPERAND (e, 1)))
1774 pp_c_left_paren (this);
1775 if (!integer_onep (TYPE_SIZE_UNIT
1776 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1777 pp_c_type_cast (this, ptr_type_node);
1779 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1780 if (!integer_zerop (TREE_OPERAND (e, 1)))
1782 pp_plus (this);
1783 pp_c_integer_constant (this,
1784 fold_convert (ssizetype,
1785 TREE_OPERAND (e, 1)));
1786 pp_c_right_paren (this);
1789 break;
1791 case REALPART_EXPR:
1792 case IMAGPART_EXPR:
1793 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1794 pp_c_whitespace (this);
1795 unary_expression (TREE_OPERAND (e, 0));
1796 break;
1798 default:
1799 postfix_expression (e);
1800 break;
1804 /* cast-expression:
1805 unary-expression
1806 ( type-name ) cast-expression */
1808 void
1809 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1811 switch (TREE_CODE (e))
1813 case FLOAT_EXPR:
1814 case FIX_TRUNC_EXPR:
1815 CASE_CONVERT:
1816 case VIEW_CONVERT_EXPR:
1817 pp_c_type_cast (pp, TREE_TYPE (e));
1818 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1819 break;
1821 default:
1822 pp->unary_expression (e);
1826 /* multiplicative-expression:
1827 cast-expression
1828 multiplicative-expression * cast-expression
1829 multiplicative-expression / cast-expression
1830 multiplicative-expression % cast-expression */
1832 void
1833 c_pretty_printer::multiplicative_expression (tree e)
1835 enum tree_code code = TREE_CODE (e);
1836 switch (code)
1838 case MULT_EXPR:
1839 case TRUNC_DIV_EXPR:
1840 case TRUNC_MOD_EXPR:
1841 case EXACT_DIV_EXPR:
1842 case RDIV_EXPR:
1843 multiplicative_expression (TREE_OPERAND (e, 0));
1844 pp_c_whitespace (this);
1845 if (code == MULT_EXPR)
1846 pp_c_star (this);
1847 else if (code == TRUNC_DIV_EXPR)
1848 pp_slash (this);
1849 else
1850 pp_modulo (this);
1851 pp_c_whitespace (this);
1852 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1853 break;
1855 default:
1856 pp_c_cast_expression (this, e);
1857 break;
1861 /* additive-expression:
1862 multiplicative-expression
1863 additive-expression + multiplicative-expression
1864 additive-expression - multiplicative-expression */
1866 static void
1867 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1869 enum tree_code code = TREE_CODE (e);
1870 switch (code)
1872 case POINTER_PLUS_EXPR:
1873 case PLUS_EXPR:
1874 case MINUS_EXPR:
1875 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1876 pp_c_whitespace (pp);
1877 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1878 pp_plus (pp);
1879 else
1880 pp_minus (pp);
1881 pp_c_whitespace (pp);
1882 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1883 break;
1885 default:
1886 pp->multiplicative_expression (e);
1887 break;
1891 /* additive-expression:
1892 additive-expression
1893 shift-expression << additive-expression
1894 shift-expression >> additive-expression */
1896 static void
1897 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1899 enum tree_code code = TREE_CODE (e);
1900 switch (code)
1902 case LSHIFT_EXPR:
1903 case RSHIFT_EXPR:
1904 case LROTATE_EXPR:
1905 case RROTATE_EXPR:
1906 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1907 pp_c_whitespace (pp);
1908 pp_string (pp, code == LSHIFT_EXPR ? "<<" :
1909 code == RSHIFT_EXPR ? ">>" :
1910 code == LROTATE_EXPR ? "<<<" : ">>>");
1911 pp_c_whitespace (pp);
1912 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1913 break;
1915 default:
1916 pp_c_additive_expression (pp, e);
1920 /* relational-expression:
1921 shift-expression
1922 relational-expression < shift-expression
1923 relational-expression > shift-expression
1924 relational-expression <= shift-expression
1925 relational-expression >= shift-expression */
1927 static void
1928 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1930 enum tree_code code = TREE_CODE (e);
1931 switch (code)
1933 case LT_EXPR:
1934 case GT_EXPR:
1935 case LE_EXPR:
1936 case GE_EXPR:
1937 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1938 pp_c_whitespace (pp);
1939 if (code == LT_EXPR)
1940 pp_less (pp);
1941 else if (code == GT_EXPR)
1942 pp_greater (pp);
1943 else if (code == LE_EXPR)
1944 pp_less_equal (pp);
1945 else if (code == GE_EXPR)
1946 pp_greater_equal (pp);
1947 pp_c_whitespace (pp);
1948 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1949 break;
1951 default:
1952 pp_c_shift_expression (pp, e);
1953 break;
1957 /* equality-expression:
1958 relational-expression
1959 equality-expression == relational-expression
1960 equality-equality != relational-expression */
1962 static void
1963 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1965 enum tree_code code = TREE_CODE (e);
1966 switch (code)
1968 case EQ_EXPR:
1969 case NE_EXPR:
1970 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1971 pp_c_whitespace (pp);
1972 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1973 pp_c_whitespace (pp);
1974 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1975 break;
1977 default:
1978 pp_c_relational_expression (pp, e);
1979 break;
1983 /* AND-expression:
1984 equality-expression
1985 AND-expression & equality-equality */
1987 static void
1988 pp_c_and_expression (c_pretty_printer *pp, tree e)
1990 if (TREE_CODE (e) == BIT_AND_EXPR)
1992 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1993 pp_c_whitespace (pp);
1994 pp_ampersand (pp);
1995 pp_c_whitespace (pp);
1996 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1998 else
1999 pp_c_equality_expression (pp, e);
2002 /* exclusive-OR-expression:
2003 AND-expression
2004 exclusive-OR-expression ^ AND-expression */
2006 static void
2007 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2009 if (TREE_CODE (e) == BIT_XOR_EXPR
2010 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2012 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2013 if (TREE_CODE (e) == BIT_XOR_EXPR)
2014 pp_c_maybe_whitespace (pp);
2015 else
2016 pp_c_whitespace (pp);
2017 pp_carret (pp);
2018 pp_c_whitespace (pp);
2019 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2021 else
2022 pp_c_and_expression (pp, e);
2025 /* inclusive-OR-expression:
2026 exclusive-OR-expression
2027 inclusive-OR-expression | exclusive-OR-expression */
2029 static void
2030 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2032 if (TREE_CODE (e) == BIT_IOR_EXPR)
2034 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2035 pp_c_whitespace (pp);
2036 pp_bar (pp);
2037 pp_c_whitespace (pp);
2038 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2040 else
2041 pp_c_exclusive_or_expression (pp, e);
2044 /* logical-AND-expression:
2045 inclusive-OR-expression
2046 logical-AND-expression && inclusive-OR-expression */
2048 static void
2049 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2051 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2052 || TREE_CODE (e) == TRUTH_AND_EXPR)
2054 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2055 pp_c_whitespace (pp);
2056 pp_ampersand_ampersand (pp);
2057 pp_c_whitespace (pp);
2058 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2060 else
2061 pp_c_inclusive_or_expression (pp, e);
2064 /* logical-OR-expression:
2065 logical-AND-expression
2066 logical-OR-expression || logical-AND-expression */
2068 void
2069 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2071 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2072 || TREE_CODE (e) == TRUTH_OR_EXPR)
2074 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2075 pp_c_whitespace (pp);
2076 pp_bar_bar (pp);
2077 pp_c_whitespace (pp);
2078 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2080 else
2081 pp_c_logical_and_expression (pp, e);
2084 /* conditional-expression:
2085 logical-OR-expression
2086 logical-OR-expression ? expression : conditional-expression */
2088 void
2089 c_pretty_printer::conditional_expression (tree e)
2091 if (TREE_CODE (e) == COND_EXPR)
2093 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2094 pp_c_whitespace (this);
2095 pp_question (this);
2096 pp_c_whitespace (this);
2097 expression (TREE_OPERAND (e, 1));
2098 pp_c_whitespace (this);
2099 pp_colon (this);
2100 pp_c_whitespace (this);
2101 conditional_expression (TREE_OPERAND (e, 2));
2103 else
2104 pp_c_logical_or_expression (this, e);
2108 /* assignment-expression:
2109 conditional-expression
2110 unary-expression assignment-operator assignment-expression
2112 assignment-expression: one of
2113 = *= /= %= += -= >>= <<= &= ^= |= */
2115 void
2116 c_pretty_printer::assignment_expression (tree e)
2118 if (TREE_CODE (e) == MODIFY_EXPR
2119 || TREE_CODE (e) == INIT_EXPR)
2121 unary_expression (TREE_OPERAND (e, 0));
2122 pp_c_whitespace (this);
2123 pp_equal (this);
2124 pp_space (this);
2125 expression (TREE_OPERAND (e, 1));
2127 else
2128 conditional_expression (e);
2131 /* expression:
2132 assignment-expression
2133 expression , assignment-expression
2135 Implementation note: instead of going through the usual recursion
2136 chain, I take the liberty of dispatching nodes to the appropriate
2137 functions. This makes some redundancy, but it worths it. That also
2138 prevents a possible infinite recursion between primary_expression ()
2139 and expression (). */
2141 void
2142 c_pretty_printer::expression (tree e)
2144 switch (TREE_CODE (e))
2146 case VOID_CST:
2147 pp_c_void_constant (this);
2148 break;
2150 case INTEGER_CST:
2151 pp_c_integer_constant (this, e);
2152 break;
2154 case REAL_CST:
2155 pp_c_floating_constant (this, e);
2156 break;
2158 case FIXED_CST:
2159 pp_c_fixed_constant (this, e);
2160 break;
2162 case STRING_CST:
2163 pp_c_string_literal (this, e);
2164 break;
2166 case IDENTIFIER_NODE:
2167 case FUNCTION_DECL:
2168 case VAR_DECL:
2169 case CONST_DECL:
2170 case PARM_DECL:
2171 case RESULT_DECL:
2172 case FIELD_DECL:
2173 case LABEL_DECL:
2174 case ERROR_MARK:
2175 primary_expression (e);
2176 break;
2178 case SSA_NAME:
2179 if (SSA_NAME_VAR (e)
2180 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2181 expression (SSA_NAME_VAR (e));
2182 else
2183 translate_string ("<unknown>");
2184 break;
2186 case POSTINCREMENT_EXPR:
2187 case POSTDECREMENT_EXPR:
2188 case ARRAY_REF:
2189 case ARRAY_NOTATION_REF:
2190 case CALL_EXPR:
2191 case COMPONENT_REF:
2192 case BIT_FIELD_REF:
2193 case COMPLEX_CST:
2194 case COMPLEX_EXPR:
2195 case VECTOR_CST:
2196 case ORDERED_EXPR:
2197 case UNORDERED_EXPR:
2198 case LTGT_EXPR:
2199 case UNEQ_EXPR:
2200 case UNLE_EXPR:
2201 case UNLT_EXPR:
2202 case UNGE_EXPR:
2203 case UNGT_EXPR:
2204 case MAX_EXPR:
2205 case MIN_EXPR:
2206 case ABS_EXPR:
2207 case CONSTRUCTOR:
2208 case COMPOUND_LITERAL_EXPR:
2209 case VA_ARG_EXPR:
2210 postfix_expression (e);
2211 break;
2213 case CONJ_EXPR:
2214 case ADDR_EXPR:
2215 case INDIRECT_REF:
2216 case MEM_REF:
2217 case NEGATE_EXPR:
2218 case BIT_NOT_EXPR:
2219 case TRUTH_NOT_EXPR:
2220 case PREINCREMENT_EXPR:
2221 case PREDECREMENT_EXPR:
2222 case REALPART_EXPR:
2223 case IMAGPART_EXPR:
2224 unary_expression (e);
2225 break;
2227 case FLOAT_EXPR:
2228 case FIX_TRUNC_EXPR:
2229 CASE_CONVERT:
2230 case VIEW_CONVERT_EXPR:
2231 pp_c_cast_expression (this, e);
2232 break;
2234 case MULT_EXPR:
2235 case TRUNC_MOD_EXPR:
2236 case TRUNC_DIV_EXPR:
2237 case EXACT_DIV_EXPR:
2238 case RDIV_EXPR:
2239 multiplicative_expression (e);
2240 break;
2242 case LSHIFT_EXPR:
2243 case RSHIFT_EXPR:
2244 case LROTATE_EXPR:
2245 case RROTATE_EXPR:
2246 pp_c_shift_expression (this, e);
2247 break;
2249 case LT_EXPR:
2250 case GT_EXPR:
2251 case LE_EXPR:
2252 case GE_EXPR:
2253 pp_c_relational_expression (this, e);
2254 break;
2256 case BIT_AND_EXPR:
2257 pp_c_and_expression (this, e);
2258 break;
2260 case BIT_XOR_EXPR:
2261 case TRUTH_XOR_EXPR:
2262 pp_c_exclusive_or_expression (this, e);
2263 break;
2265 case BIT_IOR_EXPR:
2266 pp_c_inclusive_or_expression (this, e);
2267 break;
2269 case TRUTH_ANDIF_EXPR:
2270 case TRUTH_AND_EXPR:
2271 pp_c_logical_and_expression (this, e);
2272 break;
2274 case TRUTH_ORIF_EXPR:
2275 case TRUTH_OR_EXPR:
2276 pp_c_logical_or_expression (this, e);
2277 break;
2279 case EQ_EXPR:
2280 case NE_EXPR:
2281 pp_c_equality_expression (this, e);
2282 break;
2284 case COND_EXPR:
2285 conditional_expression (e);
2286 break;
2288 case POINTER_PLUS_EXPR:
2289 case PLUS_EXPR:
2290 case MINUS_EXPR:
2291 pp_c_additive_expression (this, e);
2292 break;
2294 case MODIFY_EXPR:
2295 case INIT_EXPR:
2296 assignment_expression (e);
2297 break;
2299 case COMPOUND_EXPR:
2300 pp_c_left_paren (this);
2301 expression (TREE_OPERAND (e, 0));
2302 pp_separate_with (this, ',');
2303 assignment_expression (TREE_OPERAND (e, 1));
2304 pp_c_right_paren (this);
2305 break;
2307 case NON_LVALUE_EXPR:
2308 case SAVE_EXPR:
2309 expression (TREE_OPERAND (e, 0));
2310 break;
2312 case TARGET_EXPR:
2313 postfix_expression (TREE_OPERAND (e, 1));
2314 break;
2316 case BIND_EXPR:
2317 case GOTO_EXPR:
2318 /* We don't yet have a way of dumping statements in a
2319 human-readable format. */
2320 pp_string (this, "({...})");
2321 break;
2323 case C_MAYBE_CONST_EXPR:
2324 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2325 break;
2327 default:
2328 pp_unsupported_tree (this, e);
2329 break;
2335 /* Statements. */
2337 void
2338 c_pretty_printer::statement (tree stmt)
2340 if (stmt == NULL)
2341 return;
2343 if (pp_needs_newline (this))
2344 pp_newline_and_indent (this, 0);
2346 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2350 /* Initialize the PRETTY-PRINTER for handling C codes. */
2352 c_pretty_printer::c_pretty_printer ()
2353 : pretty_printer (),
2354 offset_list (),
2355 flags ()
2357 type_specifier_seq = pp_c_specifier_qualifier_list;
2358 ptr_operator = pp_c_pointer;
2359 parameter_list = pp_c_parameter_type_list;
2363 /* Print the tree T in full, on file FILE. */
2365 void
2366 print_c_tree (FILE *file, tree t)
2368 c_pretty_printer pp;
2370 pp_needs_newline (&pp) = true;
2371 pp.buffer->stream = file;
2372 pp.statement (t);
2373 pp_newline_and_flush (&pp);
2376 /* Print the tree T in full, on stderr. */
2378 DEBUG_FUNCTION void
2379 debug_c_tree (tree t)
2381 print_c_tree (stderr, t);
2382 fputc ('\n', stderr);
2385 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2386 up of T's memory address. */
2388 void
2389 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2391 const char *name;
2393 gcc_assert (DECL_P (t));
2395 if (DECL_NAME (t))
2396 name = IDENTIFIER_POINTER (DECL_NAME (t));
2397 else
2399 static char xname[8];
2400 sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t)
2401 & 0xffff)));
2402 name = xname;
2405 pp_c_identifier (pp, name);