* gimple-ssa-evrp-analyze.c (evrp_range_analyzer::try_find_new_range):
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob15e13ea5a2f207e75bed635440d566dd97ad8e6a
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));
524 if (!first && !parms)
526 pp_separate_with (pp, ',');
527 pp_c_ws_string (pp, "...");
530 pp_c_right_paren (pp);
533 /* abstract-declarator:
534 pointer
535 pointer(opt) direct-abstract-declarator */
537 void
538 c_pretty_printer::abstract_declarator (tree t)
540 if (TREE_CODE (t) == POINTER_TYPE)
542 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
543 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
544 pp_c_right_paren (this);
545 t = TREE_TYPE (t);
548 direct_abstract_declarator (t);
551 /* direct-abstract-declarator:
552 ( abstract-declarator )
553 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
554 direct-abstract-declarator(opt) [ * ]
555 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
557 void
558 c_pretty_printer::direct_abstract_declarator (tree t)
560 switch (TREE_CODE (t))
562 case POINTER_TYPE:
563 abstract_declarator (t);
564 break;
566 case FUNCTION_TYPE:
567 pp_c_parameter_type_list (this, t);
568 direct_abstract_declarator (TREE_TYPE (t));
569 break;
571 case ARRAY_TYPE:
572 pp_c_left_bracket (this);
573 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
575 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
576 tree type = TREE_TYPE (maxval);
578 if (tree_fits_shwi_p (maxval))
579 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
580 else
581 expression (fold_build2 (PLUS_EXPR, type, maxval,
582 build_int_cst (type, 1)));
584 pp_c_right_bracket (this);
585 direct_abstract_declarator (TREE_TYPE (t));
586 break;
588 case IDENTIFIER_NODE:
589 case VOID_TYPE:
590 case BOOLEAN_TYPE:
591 case INTEGER_TYPE:
592 case REAL_TYPE:
593 case FIXED_POINT_TYPE:
594 case ENUMERAL_TYPE:
595 case RECORD_TYPE:
596 case UNION_TYPE:
597 case VECTOR_TYPE:
598 case COMPLEX_TYPE:
599 case TYPE_DECL:
600 break;
602 default:
603 pp_unsupported_tree (this, t);
604 break;
608 /* type-name:
609 specifier-qualifier-list abstract-declarator(opt) */
611 void
612 c_pretty_printer::type_id (tree t)
614 pp_c_specifier_qualifier_list (this, t);
615 abstract_declarator (t);
618 /* storage-class-specifier:
619 typedef
620 extern
621 static
622 auto
623 register */
625 void
626 c_pretty_printer::storage_class_specifier (tree t)
628 if (TREE_CODE (t) == TYPE_DECL)
629 pp_c_ws_string (this, "typedef");
630 else if (DECL_P (t))
632 if (DECL_REGISTER (t))
633 pp_c_ws_string (this, "register");
634 else if (TREE_STATIC (t) && VAR_P (t))
635 pp_c_ws_string (this, "static");
639 /* function-specifier:
640 inline */
642 void
643 c_pretty_printer::function_specifier (tree t)
645 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
646 pp_c_ws_string (this, "inline");
649 /* declaration-specifiers:
650 storage-class-specifier declaration-specifiers(opt)
651 type-specifier declaration-specifiers(opt)
652 type-qualifier declaration-specifiers(opt)
653 function-specifier declaration-specifiers(opt) */
655 void
656 c_pretty_printer::declaration_specifiers (tree t)
658 storage_class_specifier (t);
659 function_specifier (t);
660 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
663 /* direct-declarator
664 identifier
665 ( declarator )
666 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
667 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
668 direct-declarator [ type-qualifier-list static assignment-expression ]
669 direct-declarator [ type-qualifier-list * ]
670 direct-declarator ( parameter-type-list )
671 direct-declarator ( identifier-list(opt) ) */
673 void
674 c_pretty_printer::direct_declarator (tree t)
676 switch (TREE_CODE (t))
678 case VAR_DECL:
679 case PARM_DECL:
680 case TYPE_DECL:
681 case FIELD_DECL:
682 case LABEL_DECL:
683 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
684 pp_c_tree_decl_identifier (this, t);
685 break;
687 case ARRAY_TYPE:
688 case POINTER_TYPE:
689 abstract_declarator (TREE_TYPE (t));
690 break;
692 case FUNCTION_TYPE:
693 pp_parameter_list (this, t);
694 abstract_declarator (TREE_TYPE (t));
695 break;
697 case FUNCTION_DECL:
698 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
699 pp_c_tree_decl_identifier (this, t);
700 if (flags & pp_c_flag_abstract)
701 abstract_declarator (TREE_TYPE (t));
702 else
704 pp_parameter_list (this, t);
705 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
707 break;
709 case INTEGER_TYPE:
710 case REAL_TYPE:
711 case FIXED_POINT_TYPE:
712 case ENUMERAL_TYPE:
713 case UNION_TYPE:
714 case RECORD_TYPE:
715 break;
717 default:
718 pp_unsupported_tree (this, t);
719 break;
724 /* declarator:
725 pointer(opt) direct-declarator */
727 void
728 c_pretty_printer::declarator (tree t)
730 switch (TREE_CODE (t))
732 case INTEGER_TYPE:
733 case REAL_TYPE:
734 case FIXED_POINT_TYPE:
735 case ENUMERAL_TYPE:
736 case UNION_TYPE:
737 case RECORD_TYPE:
738 break;
740 case VAR_DECL:
741 case PARM_DECL:
742 case FIELD_DECL:
743 case ARRAY_TYPE:
744 case FUNCTION_TYPE:
745 case FUNCTION_DECL:
746 case TYPE_DECL:
747 direct_declarator (t);
748 break;
751 default:
752 pp_unsupported_tree (this, t);
753 break;
757 /* declaration:
758 declaration-specifiers init-declarator-list(opt) ; */
760 void
761 c_pretty_printer::declaration (tree t)
763 declaration_specifiers (t);
764 pp_c_init_declarator (this, t);
767 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
769 void
770 pp_c_attributes (c_pretty_printer *pp, tree attributes)
772 if (attributes == NULL_TREE)
773 return;
775 pp_c_ws_string (pp, "__attribute__");
776 pp_c_left_paren (pp);
777 pp_c_left_paren (pp);
778 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
780 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
781 if (TREE_VALUE (attributes))
782 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
784 if (TREE_CHAIN (attributes))
785 pp_separate_with (pp, ',');
787 pp_c_right_paren (pp);
788 pp_c_right_paren (pp);
791 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
792 marked to be displayed on disgnostic. */
794 void
795 pp_c_attributes_display (c_pretty_printer *pp, tree a)
797 bool is_first = true;
799 if (a == NULL_TREE)
800 return;
802 for (; a != NULL_TREE; a = TREE_CHAIN (a))
804 const struct attribute_spec *as;
805 as = lookup_attribute_spec (TREE_PURPOSE (a));
806 if (!as || as->affects_type_identity == false)
807 continue;
808 if (c_dialect_cxx ()
809 && !strcmp ("transaction_safe", as->name))
810 /* In C++ transaction_safe is printed at the end of the declarator. */
811 continue;
812 if (is_first)
814 pp_c_ws_string (pp, "__attribute__");
815 pp_c_left_paren (pp);
816 pp_c_left_paren (pp);
817 is_first = false;
819 else
821 pp_separate_with (pp, ',');
823 pp_tree_identifier (pp, TREE_PURPOSE (a));
824 if (TREE_VALUE (a))
825 pp_c_call_argument_list (pp, TREE_VALUE (a));
828 if (!is_first)
830 pp_c_right_paren (pp);
831 pp_c_right_paren (pp);
832 pp_c_whitespace (pp);
836 /* function-definition:
837 declaration-specifiers declarator compound-statement */
839 void
840 pp_c_function_definition (c_pretty_printer *pp, tree t)
842 pp->declaration_specifiers (t);
843 pp->declarator (t);
844 pp_needs_newline (pp) = true;
845 pp->statement (DECL_SAVED_TREE (t));
846 pp_newline_and_flush (pp);
850 /* Expressions. */
852 /* Print out a c-char. This is called solely for characters which are
853 in the *target* execution character set. We ought to convert them
854 back to the *host* execution character set before printing, but we
855 have no way to do this at present. A decent compromise is to print
856 all characters as if they were in the host execution character set,
857 and not attempt to recover any named escape characters, but render
858 all unprintables as octal escapes. If the host and target character
859 sets are the same, this produces relatively readable output. If they
860 are not the same, strings may appear as gibberish, but that's okay
861 (in fact, it may well be what the reader wants, e.g. if they are looking
862 to see if conversion to the target character set happened correctly).
864 A special case: we need to prefix \, ", and ' with backslashes. It is
865 correct to do so for the *host*'s \, ", and ', because the rest of the
866 file appears in the host character set. */
868 static void
869 pp_c_char (c_pretty_printer *pp, int c)
871 if (ISPRINT (c))
873 switch (c)
875 case '\\': pp_string (pp, "\\\\"); break;
876 case '\'': pp_string (pp, "\\\'"); break;
877 case '\"': pp_string (pp, "\\\""); break;
878 default: pp_character (pp, c);
881 else
882 pp_scalar (pp, "\\%03o", (unsigned) c);
885 /* Print out a STRING literal. */
887 void
888 pp_c_string_literal (c_pretty_printer *pp, tree s)
890 const char *p = TREE_STRING_POINTER (s);
891 int n = TREE_STRING_LENGTH (s) - 1;
892 int i;
893 pp_doublequote (pp);
894 for (i = 0; i < n; ++i)
895 pp_c_char (pp, p[i]);
896 pp_doublequote (pp);
899 /* Pretty-print a VOID_CST (void_node). */
901 static void
902 pp_c_void_constant (c_pretty_printer *pp)
904 pp_c_type_cast (pp, void_type_node);
905 pp_string (pp, "0");
908 /* Pretty-print an INTEGER literal. */
910 static void
911 pp_c_integer_constant (c_pretty_printer *pp, tree i)
913 if (tree_fits_shwi_p (i))
914 pp_wide_integer (pp, tree_to_shwi (i));
915 else if (tree_fits_uhwi_p (i))
916 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
917 else
919 wide_int wi = wi::to_wide (i);
921 if (wi::lt_p (wi::to_wide (i), 0, TYPE_SIGN (TREE_TYPE (i))))
923 pp_minus (pp);
924 wi = -wi;
926 print_hex (wi, pp_buffer (pp)->digit_buffer);
927 pp_string (pp, pp_buffer (pp)->digit_buffer);
931 /* Print out a CHARACTER literal. */
933 static void
934 pp_c_character_constant (c_pretty_printer *pp, tree c)
936 pp_quote (pp);
937 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
938 pp_quote (pp);
941 /* Print out a BOOLEAN literal. */
943 static void
944 pp_c_bool_constant (c_pretty_printer *pp, tree b)
946 if (b == boolean_false_node)
948 if (c_dialect_cxx ())
949 pp_c_ws_string (pp, "false");
950 else if (flag_isoc99)
951 pp_c_ws_string (pp, "_False");
952 else
953 pp_unsupported_tree (pp, b);
955 else if (b == boolean_true_node)
957 if (c_dialect_cxx ())
958 pp_c_ws_string (pp, "true");
959 else if (flag_isoc99)
960 pp_c_ws_string (pp, "_True");
961 else
962 pp_unsupported_tree (pp, b);
964 else if (TREE_CODE (b) == INTEGER_CST)
965 pp_c_integer_constant (pp, b);
966 else
967 pp_unsupported_tree (pp, b);
970 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
971 false; that means the value was obtained by a cast, in which case
972 print out the type-id part of the cast-expression -- the casted value
973 is then printed by pp_c_integer_literal. */
975 static bool
976 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
978 bool value_is_named = true;
979 tree type = TREE_TYPE (e);
980 tree value;
982 /* Find the name of this constant. */
983 for (value = TYPE_VALUES (type);
984 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
985 value = TREE_CHAIN (value))
988 if (value != NULL_TREE)
989 pp->id_expression (TREE_PURPOSE (value));
990 else
992 /* Value must have been cast. */
993 pp_c_type_cast (pp, type);
994 value_is_named = false;
997 return value_is_named;
1000 /* Print out a REAL value as a decimal-floating-constant. */
1002 static void
1003 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1005 const struct real_format *fmt
1006 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1008 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1009 bool is_decimal = floating_cst.decimal;
1011 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1012 log10(2) to 7 significant digits. */
1013 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1015 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1016 sizeof (pp_buffer (pp)->digit_buffer),
1017 max_digits10, 1);
1019 pp_string (pp, pp_buffer(pp)->digit_buffer);
1020 if (TREE_TYPE (r) == float_type_node)
1021 pp_character (pp, 'f');
1022 else if (TREE_TYPE (r) == long_double_type_node)
1023 pp_character (pp, 'l');
1024 else if (TREE_TYPE (r) == dfloat128_type_node)
1025 pp_string (pp, "dl");
1026 else if (TREE_TYPE (r) == dfloat64_type_node)
1027 pp_string (pp, "dd");
1028 else if (TREE_TYPE (r) == dfloat32_type_node)
1029 pp_string (pp, "df");
1030 else if (TREE_TYPE (r) != double_type_node)
1031 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1032 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i))
1034 pp_character (pp, 'f');
1035 pp_decimal_int (pp, floatn_nx_types[i].n);
1036 if (floatn_nx_types[i].extended)
1037 pp_character (pp, 'x');
1038 break;
1042 /* Print out a FIXED value as a decimal-floating-constant. */
1044 static void
1045 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1047 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1048 sizeof (pp_buffer (pp)->digit_buffer));
1049 pp_string (pp, pp_buffer(pp)->digit_buffer);
1052 /* Pretty-print a compound literal expression. GNU extensions include
1053 vector constants. */
1055 static void
1056 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1058 tree type = TREE_TYPE (e);
1059 pp_c_type_cast (pp, type);
1061 switch (TREE_CODE (type))
1063 case RECORD_TYPE:
1064 case UNION_TYPE:
1065 case ARRAY_TYPE:
1066 case VECTOR_TYPE:
1067 case COMPLEX_TYPE:
1068 pp_c_brace_enclosed_initializer_list (pp, e);
1069 break;
1071 default:
1072 pp_unsupported_tree (pp, e);
1073 break;
1077 /* Pretty-print a COMPLEX_EXPR expression. */
1079 static void
1080 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1082 /* Handle a few common special cases, otherwise fallback
1083 to printing it as compound literal. */
1084 tree type = TREE_TYPE (e);
1085 tree realexpr = TREE_OPERAND (e, 0);
1086 tree imagexpr = TREE_OPERAND (e, 1);
1088 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1089 if (TREE_CODE (realexpr) == NOP_EXPR
1090 && TREE_CODE (imagexpr) == NOP_EXPR
1091 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1092 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1093 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1094 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1095 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1096 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1098 pp_c_type_cast (pp, type);
1099 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1100 return;
1103 /* Cast of an scalar expression to COMPLEX_TYPE. */
1104 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1105 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1107 pp_c_type_cast (pp, type);
1108 if (TREE_CODE (realexpr) == NOP_EXPR)
1109 realexpr = TREE_OPERAND (realexpr, 0);
1110 pp->expression (realexpr);
1111 return;
1114 pp_c_compound_literal (pp, e);
1117 /* constant:
1118 integer-constant
1119 floating-constant
1120 fixed-point-constant
1121 enumeration-constant
1122 character-constant */
1124 void
1125 c_pretty_printer::constant (tree e)
1127 const enum tree_code code = TREE_CODE (e);
1129 switch (code)
1131 case VOID_CST:
1132 pp_c_void_constant (this);
1133 break;
1135 case INTEGER_CST:
1137 tree type = TREE_TYPE (e);
1138 if (type == boolean_type_node)
1139 pp_c_bool_constant (this, e);
1140 else if (type == char_type_node)
1141 pp_c_character_constant (this, e);
1142 else if (TREE_CODE (type) == ENUMERAL_TYPE
1143 && pp_c_enumeration_constant (this, e))
1145 else
1146 pp_c_integer_constant (this, e);
1148 break;
1150 case REAL_CST:
1151 pp_c_floating_constant (this, e);
1152 break;
1154 case FIXED_CST:
1155 pp_c_fixed_constant (this, e);
1156 break;
1158 case STRING_CST:
1159 pp_c_string_literal (this, e);
1160 break;
1162 case COMPLEX_CST:
1163 /* Sometimes, we are confused and we think a complex literal
1164 is a constant. Such thing is a compound literal which
1165 grammatically belongs to postfix-expr production. */
1166 pp_c_compound_literal (this, e);
1167 break;
1169 default:
1170 pp_unsupported_tree (this, e);
1171 break;
1175 /* Pretty-print a string such as an identifier, without changing its
1176 encoding, preceded by whitespace is necessary. */
1178 void
1179 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1181 pp_c_maybe_whitespace (pp);
1182 pp_string (pp, str);
1183 pp->padding = pp_before;
1186 void
1187 c_pretty_printer::translate_string (const char *gmsgid)
1189 if (pp_translate_identifiers (this))
1190 pp_c_ws_string (this, _(gmsgid));
1191 else
1192 pp_c_ws_string (this, gmsgid);
1195 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1196 that need converting to the locale encoding, preceded by whitespace
1197 is necessary. */
1199 void
1200 pp_c_identifier (c_pretty_printer *pp, const char *id)
1202 pp_c_maybe_whitespace (pp);
1203 pp_identifier (pp, id);
1204 pp->padding = pp_before;
1207 /* Pretty-print a C primary-expression.
1208 primary-expression:
1209 identifier
1210 constant
1211 string-literal
1212 ( expression ) */
1214 void
1215 c_pretty_printer::primary_expression (tree e)
1217 switch (TREE_CODE (e))
1219 case VAR_DECL:
1220 case PARM_DECL:
1221 case FIELD_DECL:
1222 case CONST_DECL:
1223 case FUNCTION_DECL:
1224 case LABEL_DECL:
1225 pp_c_tree_decl_identifier (this, e);
1226 break;
1228 case IDENTIFIER_NODE:
1229 pp_c_tree_identifier (this, e);
1230 break;
1232 case ERROR_MARK:
1233 translate_string ("<erroneous-expression>");
1234 break;
1236 case RESULT_DECL:
1237 translate_string ("<return-value>");
1238 break;
1240 case VOID_CST:
1241 case INTEGER_CST:
1242 case REAL_CST:
1243 case FIXED_CST:
1244 case STRING_CST:
1245 constant (e);
1246 break;
1248 case TARGET_EXPR:
1249 pp_c_ws_string (this, "__builtin_memcpy");
1250 pp_c_left_paren (this);
1251 pp_ampersand (this);
1252 primary_expression (TREE_OPERAND (e, 0));
1253 pp_separate_with (this, ',');
1254 pp_ampersand (this);
1255 initializer (TREE_OPERAND (e, 1));
1256 if (TREE_OPERAND (e, 2))
1258 pp_separate_with (this, ',');
1259 expression (TREE_OPERAND (e, 2));
1261 pp_c_right_paren (this);
1262 break;
1264 default:
1265 /* FIXME: Make sure we won't get into an infinite loop. */
1266 pp_c_left_paren (this);
1267 expression (e);
1268 pp_c_right_paren (this);
1269 break;
1273 /* Print out a C initializer -- also support C compound-literals.
1274 initializer:
1275 assignment-expression:
1276 { initializer-list }
1277 { initializer-list , } */
1279 void
1280 c_pretty_printer::initializer (tree e)
1282 if (TREE_CODE (e) == CONSTRUCTOR)
1283 pp_c_brace_enclosed_initializer_list (this, e);
1284 else
1285 expression (e);
1288 /* init-declarator:
1289 declarator:
1290 declarator = initializer */
1292 void
1293 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1295 pp->declarator (t);
1296 /* We don't want to output function definitions here. There are handled
1297 elsewhere (and the syntactic form is bogus anyway). */
1298 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1300 tree init = DECL_INITIAL (t);
1301 /* This C++ bit is handled here because it is easier to do so.
1302 In templates, the C++ parser builds a TREE_LIST for a
1303 direct-initialization; the TREE_PURPOSE is the variable to
1304 initialize and the TREE_VALUE is the initializer. */
1305 if (TREE_CODE (init) == TREE_LIST)
1307 pp_c_left_paren (pp);
1308 pp->expression (TREE_VALUE (init));
1309 pp_right_paren (pp);
1311 else
1313 pp_space (pp);
1314 pp_equal (pp);
1315 pp_space (pp);
1316 pp->initializer (init);
1321 /* initializer-list:
1322 designation(opt) initializer
1323 initializer-list , designation(opt) initializer
1325 designation:
1326 designator-list =
1328 designator-list:
1329 designator
1330 designator-list designator
1332 designator:
1333 [ constant-expression ]
1334 identifier */
1336 static void
1337 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1339 tree type = TREE_TYPE (e);
1340 const enum tree_code code = TREE_CODE (type);
1342 if (TREE_CODE (e) == CONSTRUCTOR)
1344 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1345 return;
1348 switch (code)
1350 case RECORD_TYPE:
1351 case UNION_TYPE:
1352 case ARRAY_TYPE:
1354 tree init = TREE_OPERAND (e, 0);
1355 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1357 if (code == RECORD_TYPE || code == UNION_TYPE)
1359 pp_c_dot (pp);
1360 pp->primary_expression (TREE_PURPOSE (init));
1362 else
1364 pp_c_left_bracket (pp);
1365 if (TREE_PURPOSE (init))
1366 pp->constant (TREE_PURPOSE (init));
1367 pp_c_right_bracket (pp);
1369 pp_c_whitespace (pp);
1370 pp_equal (pp);
1371 pp_c_whitespace (pp);
1372 pp->initializer (TREE_VALUE (init));
1373 if (TREE_CHAIN (init))
1374 pp_separate_with (pp, ',');
1377 return;
1379 case VECTOR_TYPE:
1380 if (TREE_CODE (e) == VECTOR_CST)
1382 unsigned i;
1383 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1385 if (i > 0)
1386 pp_separate_with (pp, ',');
1387 pp->expression (VECTOR_CST_ELT (e, i));
1390 else
1391 break;
1392 return;
1394 case COMPLEX_TYPE:
1395 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1397 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1398 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1399 pp_separate_with (pp, ',');
1400 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1402 else
1403 break;
1404 return;
1406 default:
1407 break;
1410 pp_unsupported_tree (pp, type);
1413 /* Pretty-print a brace-enclosed initializer-list. */
1415 static void
1416 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1418 pp_c_left_brace (pp);
1419 pp_c_initializer_list (pp, l);
1420 pp_c_right_brace (pp);
1424 /* This is a convenient function, used to bridge gap between C and C++
1425 grammars.
1427 id-expression:
1428 identifier */
1430 void
1431 c_pretty_printer::id_expression (tree t)
1433 switch (TREE_CODE (t))
1435 case VAR_DECL:
1436 case PARM_DECL:
1437 case CONST_DECL:
1438 case TYPE_DECL:
1439 case FUNCTION_DECL:
1440 case FIELD_DECL:
1441 case LABEL_DECL:
1442 pp_c_tree_decl_identifier (this, t);
1443 break;
1445 case IDENTIFIER_NODE:
1446 pp_c_tree_identifier (this, t);
1447 break;
1449 default:
1450 pp_unsupported_tree (this, t);
1451 break;
1455 /* postfix-expression:
1456 primary-expression
1457 postfix-expression [ expression ]
1458 postfix-expression ( argument-expression-list(opt) )
1459 postfix-expression . identifier
1460 postfix-expression -> identifier
1461 postfix-expression ++
1462 postfix-expression --
1463 ( type-name ) { initializer-list }
1464 ( type-name ) { initializer-list , } */
1466 void
1467 c_pretty_printer::postfix_expression (tree e)
1469 enum tree_code code = TREE_CODE (e);
1470 switch (code)
1472 case POSTINCREMENT_EXPR:
1473 case POSTDECREMENT_EXPR:
1474 postfix_expression (TREE_OPERAND (e, 0));
1475 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1476 break;
1478 case ARRAY_REF:
1479 postfix_expression (TREE_OPERAND (e, 0));
1480 pp_c_left_bracket (this);
1481 expression (TREE_OPERAND (e, 1));
1482 pp_c_right_bracket (this);
1483 break;
1485 case ARRAY_NOTATION_REF:
1486 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1487 pp_c_left_bracket (this);
1488 expression (ARRAY_NOTATION_START (e));
1489 pp_colon (this);
1490 expression (ARRAY_NOTATION_LENGTH (e));
1491 pp_colon (this);
1492 expression (ARRAY_NOTATION_STRIDE (e));
1493 pp_c_right_bracket (this);
1494 break;
1496 case CALL_EXPR:
1498 call_expr_arg_iterator iter;
1499 tree arg;
1500 postfix_expression (CALL_EXPR_FN (e));
1501 pp_c_left_paren (this);
1502 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1504 expression (arg);
1505 if (more_call_expr_args_p (&iter))
1506 pp_separate_with (this, ',');
1508 pp_c_right_paren (this);
1509 break;
1512 case UNORDERED_EXPR:
1513 pp_c_ws_string (this, flag_isoc99
1514 ? "isunordered"
1515 : "__builtin_isunordered");
1516 goto two_args_fun;
1518 case ORDERED_EXPR:
1519 pp_c_ws_string (this, flag_isoc99
1520 ? "!isunordered"
1521 : "!__builtin_isunordered");
1522 goto two_args_fun;
1524 case UNLT_EXPR:
1525 pp_c_ws_string (this, flag_isoc99
1526 ? "!isgreaterequal"
1527 : "!__builtin_isgreaterequal");
1528 goto two_args_fun;
1530 case UNLE_EXPR:
1531 pp_c_ws_string (this, flag_isoc99
1532 ? "!isgreater"
1533 : "!__builtin_isgreater");
1534 goto two_args_fun;
1536 case UNGT_EXPR:
1537 pp_c_ws_string (this, flag_isoc99
1538 ? "!islessequal"
1539 : "!__builtin_islessequal");
1540 goto two_args_fun;
1542 case UNGE_EXPR:
1543 pp_c_ws_string (this, flag_isoc99
1544 ? "!isless"
1545 : "!__builtin_isless");
1546 goto two_args_fun;
1548 case UNEQ_EXPR:
1549 pp_c_ws_string (this, flag_isoc99
1550 ? "!islessgreater"
1551 : "!__builtin_islessgreater");
1552 goto two_args_fun;
1554 case LTGT_EXPR:
1555 pp_c_ws_string (this, flag_isoc99
1556 ? "islessgreater"
1557 : "__builtin_islessgreater");
1558 goto two_args_fun;
1560 case MAX_EXPR:
1561 pp_c_ws_string (this, "max");
1562 goto two_args_fun;
1564 case MIN_EXPR:
1565 pp_c_ws_string (this, "min");
1566 goto two_args_fun;
1568 two_args_fun:
1569 pp_c_left_paren (this);
1570 expression (TREE_OPERAND (e, 0));
1571 pp_separate_with (this, ',');
1572 expression (TREE_OPERAND (e, 1));
1573 pp_c_right_paren (this);
1574 break;
1576 case ABS_EXPR:
1577 pp_c_ws_string (this, "__builtin_abs");
1578 pp_c_left_paren (this);
1579 expression (TREE_OPERAND (e, 0));
1580 pp_c_right_paren (this);
1581 break;
1583 case COMPONENT_REF:
1585 tree object = TREE_OPERAND (e, 0);
1586 if (INDIRECT_REF_P (object))
1588 postfix_expression (TREE_OPERAND (object, 0));
1589 pp_c_arrow (this);
1591 else
1593 postfix_expression (object);
1594 pp_c_dot (this);
1596 expression (TREE_OPERAND (e, 1));
1598 break;
1600 case BIT_FIELD_REF:
1602 tree type = TREE_TYPE (e);
1604 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1605 if (type
1606 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1608 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1609 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1610 if ((bitpos % size) == 0)
1612 pp_c_left_paren (this);
1613 pp_c_left_paren (this);
1614 type_id (type);
1615 pp_c_star (this);
1616 pp_c_right_paren (this);
1617 pp_c_ampersand (this);
1618 expression (TREE_OPERAND (e, 0));
1619 pp_c_right_paren (this);
1620 pp_c_left_bracket (this);
1621 pp_wide_integer (this, bitpos / size);
1622 pp_c_right_bracket (this);
1623 break;
1626 pp_unsupported_tree (this, e);
1628 break;
1630 case MEM_REF:
1631 expression (e);
1632 break;
1634 case COMPLEX_CST:
1635 case VECTOR_CST:
1636 pp_c_compound_literal (this, e);
1637 break;
1639 case COMPLEX_EXPR:
1640 pp_c_complex_expr (this, e);
1641 break;
1643 case COMPOUND_LITERAL_EXPR:
1644 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1645 /* Fall through. */
1646 case CONSTRUCTOR:
1647 initializer (e);
1648 break;
1650 case VA_ARG_EXPR:
1651 pp_c_ws_string (this, "__builtin_va_arg");
1652 pp_c_left_paren (this);
1653 assignment_expression (TREE_OPERAND (e, 0));
1654 pp_separate_with (this, ',');
1655 type_id (TREE_TYPE (e));
1656 pp_c_right_paren (this);
1657 break;
1659 case ADDR_EXPR:
1660 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1662 id_expression (TREE_OPERAND (e, 0));
1663 break;
1665 /* fall through. */
1667 default:
1668 primary_expression (e);
1669 break;
1673 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1675 void
1676 pp_c_expression_list (c_pretty_printer *pp, tree e)
1678 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1680 pp->expression (TREE_VALUE (e));
1681 if (TREE_CHAIN (e))
1682 pp_separate_with (pp, ',');
1686 /* Print out V, which contains the elements of a constructor. */
1688 void
1689 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1691 unsigned HOST_WIDE_INT ix;
1692 tree value;
1694 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1696 pp->expression (value);
1697 if (ix != vec_safe_length (v) - 1)
1698 pp_separate_with (pp, ',');
1702 /* Print out an expression-list in parens, as if it were the argument
1703 list to a function. */
1705 void
1706 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1708 pp_c_left_paren (pp);
1709 if (t && TREE_CODE (t) == TREE_LIST)
1710 pp_c_expression_list (pp, t);
1711 pp_c_right_paren (pp);
1714 /* unary-expression:
1715 postfix-expression
1716 ++ cast-expression
1717 -- cast-expression
1718 unary-operator cast-expression
1719 sizeof unary-expression
1720 sizeof ( type-id )
1722 unary-operator: one of
1723 * & + - ! ~
1725 GNU extensions.
1726 unary-expression:
1727 __alignof__ unary-expression
1728 __alignof__ ( type-id )
1729 __real__ unary-expression
1730 __imag__ unary-expression */
1732 void
1733 c_pretty_printer::unary_expression (tree e)
1735 enum tree_code code = TREE_CODE (e);
1736 switch (code)
1738 case PREINCREMENT_EXPR:
1739 case PREDECREMENT_EXPR:
1740 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1741 unary_expression (TREE_OPERAND (e, 0));
1742 break;
1744 case ADDR_EXPR:
1745 case INDIRECT_REF:
1746 case NEGATE_EXPR:
1747 case BIT_NOT_EXPR:
1748 case TRUTH_NOT_EXPR:
1749 case CONJ_EXPR:
1750 /* String literal are used by address. */
1751 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1752 pp_ampersand (this);
1753 else if (code == INDIRECT_REF)
1755 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
1756 if (type && TREE_CODE (type) == REFERENCE_TYPE)
1757 /* Reference decay is implicit, don't print anything. */;
1758 else
1759 pp_c_star (this);
1761 else if (code == NEGATE_EXPR)
1762 pp_minus (this);
1763 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1764 pp_complement (this);
1765 else if (code == TRUTH_NOT_EXPR)
1766 pp_exclamation (this);
1767 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1768 break;
1770 case MEM_REF:
1771 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1772 && integer_zerop (TREE_OPERAND (e, 1)))
1773 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1774 else
1776 pp_c_star (this);
1777 if (!integer_zerop (TREE_OPERAND (e, 1)))
1779 pp_c_left_paren (this);
1780 if (!integer_onep (TYPE_SIZE_UNIT
1781 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1782 pp_c_type_cast (this, ptr_type_node);
1784 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1785 if (!integer_zerop (TREE_OPERAND (e, 1)))
1787 pp_plus (this);
1788 pp_c_integer_constant (this,
1789 fold_convert (ssizetype,
1790 TREE_OPERAND (e, 1)));
1791 pp_c_right_paren (this);
1794 break;
1796 case REALPART_EXPR:
1797 case IMAGPART_EXPR:
1798 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1799 pp_c_whitespace (this);
1800 unary_expression (TREE_OPERAND (e, 0));
1801 break;
1803 default:
1804 postfix_expression (e);
1805 break;
1809 /* cast-expression:
1810 unary-expression
1811 ( type-name ) cast-expression */
1813 void
1814 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1816 switch (TREE_CODE (e))
1818 case FLOAT_EXPR:
1819 case FIX_TRUNC_EXPR:
1820 CASE_CONVERT:
1821 case VIEW_CONVERT_EXPR:
1822 pp_c_type_cast (pp, TREE_TYPE (e));
1823 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1824 break;
1826 default:
1827 pp->unary_expression (e);
1831 /* multiplicative-expression:
1832 cast-expression
1833 multiplicative-expression * cast-expression
1834 multiplicative-expression / cast-expression
1835 multiplicative-expression % cast-expression */
1837 void
1838 c_pretty_printer::multiplicative_expression (tree e)
1840 enum tree_code code = TREE_CODE (e);
1841 switch (code)
1843 case MULT_EXPR:
1844 case TRUNC_DIV_EXPR:
1845 case TRUNC_MOD_EXPR:
1846 case EXACT_DIV_EXPR:
1847 case RDIV_EXPR:
1848 multiplicative_expression (TREE_OPERAND (e, 0));
1849 pp_c_whitespace (this);
1850 if (code == MULT_EXPR)
1851 pp_c_star (this);
1852 else if (code == TRUNC_DIV_EXPR)
1853 pp_slash (this);
1854 else
1855 pp_modulo (this);
1856 pp_c_whitespace (this);
1857 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1858 break;
1860 default:
1861 pp_c_cast_expression (this, e);
1862 break;
1866 /* additive-expression:
1867 multiplicative-expression
1868 additive-expression + multiplicative-expression
1869 additive-expression - multiplicative-expression */
1871 static void
1872 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1874 enum tree_code code = TREE_CODE (e);
1875 switch (code)
1877 case POINTER_PLUS_EXPR:
1878 case PLUS_EXPR:
1879 case POINTER_DIFF_EXPR:
1880 case MINUS_EXPR:
1881 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1882 pp_c_whitespace (pp);
1883 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1884 pp_plus (pp);
1885 else
1886 pp_minus (pp);
1887 pp_c_whitespace (pp);
1888 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1889 break;
1891 default:
1892 pp->multiplicative_expression (e);
1893 break;
1897 /* additive-expression:
1898 additive-expression
1899 shift-expression << additive-expression
1900 shift-expression >> additive-expression */
1902 static void
1903 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1905 enum tree_code code = TREE_CODE (e);
1906 switch (code)
1908 case LSHIFT_EXPR:
1909 case RSHIFT_EXPR:
1910 case LROTATE_EXPR:
1911 case RROTATE_EXPR:
1912 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1913 pp_c_whitespace (pp);
1914 pp_string (pp, code == LSHIFT_EXPR ? "<<" :
1915 code == RSHIFT_EXPR ? ">>" :
1916 code == LROTATE_EXPR ? "<<<" : ">>>");
1917 pp_c_whitespace (pp);
1918 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1919 break;
1921 default:
1922 pp_c_additive_expression (pp, e);
1926 /* relational-expression:
1927 shift-expression
1928 relational-expression < shift-expression
1929 relational-expression > shift-expression
1930 relational-expression <= shift-expression
1931 relational-expression >= shift-expression */
1933 static void
1934 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1936 enum tree_code code = TREE_CODE (e);
1937 switch (code)
1939 case LT_EXPR:
1940 case GT_EXPR:
1941 case LE_EXPR:
1942 case GE_EXPR:
1943 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1944 pp_c_whitespace (pp);
1945 if (code == LT_EXPR)
1946 pp_less (pp);
1947 else if (code == GT_EXPR)
1948 pp_greater (pp);
1949 else if (code == LE_EXPR)
1950 pp_less_equal (pp);
1951 else if (code == GE_EXPR)
1952 pp_greater_equal (pp);
1953 pp_c_whitespace (pp);
1954 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1955 break;
1957 default:
1958 pp_c_shift_expression (pp, e);
1959 break;
1963 /* equality-expression:
1964 relational-expression
1965 equality-expression == relational-expression
1966 equality-equality != relational-expression */
1968 static void
1969 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1971 enum tree_code code = TREE_CODE (e);
1972 switch (code)
1974 case EQ_EXPR:
1975 case NE_EXPR:
1976 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1977 pp_c_whitespace (pp);
1978 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1979 pp_c_whitespace (pp);
1980 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1981 break;
1983 default:
1984 pp_c_relational_expression (pp, e);
1985 break;
1989 /* AND-expression:
1990 equality-expression
1991 AND-expression & equality-equality */
1993 static void
1994 pp_c_and_expression (c_pretty_printer *pp, tree e)
1996 if (TREE_CODE (e) == BIT_AND_EXPR)
1998 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1999 pp_c_whitespace (pp);
2000 pp_ampersand (pp);
2001 pp_c_whitespace (pp);
2002 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2004 else
2005 pp_c_equality_expression (pp, e);
2008 /* exclusive-OR-expression:
2009 AND-expression
2010 exclusive-OR-expression ^ AND-expression */
2012 static void
2013 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2015 if (TREE_CODE (e) == BIT_XOR_EXPR
2016 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2018 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2019 if (TREE_CODE (e) == BIT_XOR_EXPR)
2020 pp_c_maybe_whitespace (pp);
2021 else
2022 pp_c_whitespace (pp);
2023 pp_carret (pp);
2024 pp_c_whitespace (pp);
2025 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2027 else
2028 pp_c_and_expression (pp, e);
2031 /* inclusive-OR-expression:
2032 exclusive-OR-expression
2033 inclusive-OR-expression | exclusive-OR-expression */
2035 static void
2036 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2038 if (TREE_CODE (e) == BIT_IOR_EXPR)
2040 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2041 pp_c_whitespace (pp);
2042 pp_bar (pp);
2043 pp_c_whitespace (pp);
2044 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2046 else
2047 pp_c_exclusive_or_expression (pp, e);
2050 /* logical-AND-expression:
2051 inclusive-OR-expression
2052 logical-AND-expression && inclusive-OR-expression */
2054 static void
2055 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2057 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2058 || TREE_CODE (e) == TRUTH_AND_EXPR)
2060 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2061 pp_c_whitespace (pp);
2062 pp_ampersand_ampersand (pp);
2063 pp_c_whitespace (pp);
2064 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2066 else
2067 pp_c_inclusive_or_expression (pp, e);
2070 /* logical-OR-expression:
2071 logical-AND-expression
2072 logical-OR-expression || logical-AND-expression */
2074 void
2075 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2077 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2078 || TREE_CODE (e) == TRUTH_OR_EXPR)
2080 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2081 pp_c_whitespace (pp);
2082 pp_bar_bar (pp);
2083 pp_c_whitespace (pp);
2084 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2086 else
2087 pp_c_logical_and_expression (pp, e);
2090 /* conditional-expression:
2091 logical-OR-expression
2092 logical-OR-expression ? expression : conditional-expression */
2094 void
2095 c_pretty_printer::conditional_expression (tree e)
2097 if (TREE_CODE (e) == COND_EXPR)
2099 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2100 pp_c_whitespace (this);
2101 pp_question (this);
2102 pp_c_whitespace (this);
2103 expression (TREE_OPERAND (e, 1));
2104 pp_c_whitespace (this);
2105 pp_colon (this);
2106 pp_c_whitespace (this);
2107 conditional_expression (TREE_OPERAND (e, 2));
2109 else
2110 pp_c_logical_or_expression (this, e);
2114 /* assignment-expression:
2115 conditional-expression
2116 unary-expression assignment-operator assignment-expression
2118 assignment-expression: one of
2119 = *= /= %= += -= >>= <<= &= ^= |= */
2121 void
2122 c_pretty_printer::assignment_expression (tree e)
2124 if (TREE_CODE (e) == MODIFY_EXPR
2125 || TREE_CODE (e) == INIT_EXPR)
2127 unary_expression (TREE_OPERAND (e, 0));
2128 pp_c_whitespace (this);
2129 pp_equal (this);
2130 pp_space (this);
2131 expression (TREE_OPERAND (e, 1));
2133 else
2134 conditional_expression (e);
2137 /* expression:
2138 assignment-expression
2139 expression , assignment-expression
2141 Implementation note: instead of going through the usual recursion
2142 chain, I take the liberty of dispatching nodes to the appropriate
2143 functions. This makes some redundancy, but it worths it. That also
2144 prevents a possible infinite recursion between primary_expression ()
2145 and expression (). */
2147 void
2148 c_pretty_printer::expression (tree e)
2150 switch (TREE_CODE (e))
2152 case VOID_CST:
2153 pp_c_void_constant (this);
2154 break;
2156 case INTEGER_CST:
2157 pp_c_integer_constant (this, e);
2158 break;
2160 case REAL_CST:
2161 pp_c_floating_constant (this, e);
2162 break;
2164 case FIXED_CST:
2165 pp_c_fixed_constant (this, e);
2166 break;
2168 case STRING_CST:
2169 pp_c_string_literal (this, e);
2170 break;
2172 case IDENTIFIER_NODE:
2173 case FUNCTION_DECL:
2174 case VAR_DECL:
2175 case CONST_DECL:
2176 case PARM_DECL:
2177 case RESULT_DECL:
2178 case FIELD_DECL:
2179 case LABEL_DECL:
2180 case ERROR_MARK:
2181 primary_expression (e);
2182 break;
2184 case SSA_NAME:
2185 if (SSA_NAME_VAR (e)
2186 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2187 expression (SSA_NAME_VAR (e));
2188 else
2189 translate_string ("<unknown>");
2190 break;
2192 case POSTINCREMENT_EXPR:
2193 case POSTDECREMENT_EXPR:
2194 case ARRAY_REF:
2195 case ARRAY_NOTATION_REF:
2196 case CALL_EXPR:
2197 case COMPONENT_REF:
2198 case BIT_FIELD_REF:
2199 case COMPLEX_CST:
2200 case COMPLEX_EXPR:
2201 case VECTOR_CST:
2202 case ORDERED_EXPR:
2203 case UNORDERED_EXPR:
2204 case LTGT_EXPR:
2205 case UNEQ_EXPR:
2206 case UNLE_EXPR:
2207 case UNLT_EXPR:
2208 case UNGE_EXPR:
2209 case UNGT_EXPR:
2210 case MAX_EXPR:
2211 case MIN_EXPR:
2212 case ABS_EXPR:
2213 case CONSTRUCTOR:
2214 case COMPOUND_LITERAL_EXPR:
2215 case VA_ARG_EXPR:
2216 postfix_expression (e);
2217 break;
2219 case CONJ_EXPR:
2220 case ADDR_EXPR:
2221 case INDIRECT_REF:
2222 case MEM_REF:
2223 case NEGATE_EXPR:
2224 case BIT_NOT_EXPR:
2225 case TRUTH_NOT_EXPR:
2226 case PREINCREMENT_EXPR:
2227 case PREDECREMENT_EXPR:
2228 case REALPART_EXPR:
2229 case IMAGPART_EXPR:
2230 unary_expression (e);
2231 break;
2233 case FLOAT_EXPR:
2234 case FIX_TRUNC_EXPR:
2235 CASE_CONVERT:
2236 case VIEW_CONVERT_EXPR:
2237 pp_c_cast_expression (this, e);
2238 break;
2240 case MULT_EXPR:
2241 case TRUNC_MOD_EXPR:
2242 case TRUNC_DIV_EXPR:
2243 case EXACT_DIV_EXPR:
2244 case RDIV_EXPR:
2245 multiplicative_expression (e);
2246 break;
2248 case LSHIFT_EXPR:
2249 case RSHIFT_EXPR:
2250 case LROTATE_EXPR:
2251 case RROTATE_EXPR:
2252 pp_c_shift_expression (this, e);
2253 break;
2255 case LT_EXPR:
2256 case GT_EXPR:
2257 case LE_EXPR:
2258 case GE_EXPR:
2259 pp_c_relational_expression (this, e);
2260 break;
2262 case BIT_AND_EXPR:
2263 pp_c_and_expression (this, e);
2264 break;
2266 case BIT_XOR_EXPR:
2267 case TRUTH_XOR_EXPR:
2268 pp_c_exclusive_or_expression (this, e);
2269 break;
2271 case BIT_IOR_EXPR:
2272 pp_c_inclusive_or_expression (this, e);
2273 break;
2275 case TRUTH_ANDIF_EXPR:
2276 case TRUTH_AND_EXPR:
2277 pp_c_logical_and_expression (this, e);
2278 break;
2280 case TRUTH_ORIF_EXPR:
2281 case TRUTH_OR_EXPR:
2282 pp_c_logical_or_expression (this, e);
2283 break;
2285 case EQ_EXPR:
2286 case NE_EXPR:
2287 pp_c_equality_expression (this, e);
2288 break;
2290 case COND_EXPR:
2291 conditional_expression (e);
2292 break;
2294 case POINTER_PLUS_EXPR:
2295 case PLUS_EXPR:
2296 case POINTER_DIFF_EXPR:
2297 case MINUS_EXPR:
2298 pp_c_additive_expression (this, e);
2299 break;
2301 case MODIFY_EXPR:
2302 case INIT_EXPR:
2303 assignment_expression (e);
2304 break;
2306 case COMPOUND_EXPR:
2307 pp_c_left_paren (this);
2308 expression (TREE_OPERAND (e, 0));
2309 pp_separate_with (this, ',');
2310 assignment_expression (TREE_OPERAND (e, 1));
2311 pp_c_right_paren (this);
2312 break;
2314 case NON_LVALUE_EXPR:
2315 case SAVE_EXPR:
2316 expression (TREE_OPERAND (e, 0));
2317 break;
2319 case TARGET_EXPR:
2320 postfix_expression (TREE_OPERAND (e, 1));
2321 break;
2323 case BIND_EXPR:
2324 case GOTO_EXPR:
2325 /* We don't yet have a way of dumping statements in a
2326 human-readable format. */
2327 pp_string (this, "({...})");
2328 break;
2330 case C_MAYBE_CONST_EXPR:
2331 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2332 break;
2334 default:
2335 pp_unsupported_tree (this, e);
2336 break;
2342 /* Statements. */
2344 void
2345 c_pretty_printer::statement (tree stmt)
2347 if (stmt == NULL)
2348 return;
2350 if (pp_needs_newline (this))
2351 pp_newline_and_indent (this, 0);
2353 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2357 /* Initialize the PRETTY-PRINTER for handling C codes. */
2359 c_pretty_printer::c_pretty_printer ()
2360 : pretty_printer (),
2361 offset_list (),
2362 flags ()
2364 type_specifier_seq = pp_c_specifier_qualifier_list;
2365 ptr_operator = pp_c_pointer;
2366 parameter_list = pp_c_parameter_type_list;
2370 /* Print the tree T in full, on file FILE. */
2372 void
2373 print_c_tree (FILE *file, tree t)
2375 c_pretty_printer pp;
2377 pp_needs_newline (&pp) = true;
2378 pp.buffer->stream = file;
2379 pp.statement (t);
2380 pp_newline_and_flush (&pp);
2383 /* Print the tree T in full, on stderr. */
2385 DEBUG_FUNCTION void
2386 debug_c_tree (tree t)
2388 print_c_tree (stderr, t);
2389 fputc ('\n', stderr);
2392 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2393 up of T's memory address. */
2395 void
2396 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2398 const char *name;
2400 gcc_assert (DECL_P (t));
2402 if (DECL_NAME (t))
2403 name = IDENTIFIER_POINTER (DECL_NAME (t));
2404 else
2406 static char xname[8];
2407 sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t)
2408 & 0xffff)));
2409 name = xname;
2412 pp_c_identifier (pp, name);