S/390: Add static OSC breaker if necessary.
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob90428cac183814dd5fedf06479ce18cafa5d6fa9
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2016 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 "attribs.h"
28 #include "intl.h"
29 #include "tree-pretty-print.h"
31 /* The pretty-printer code is primarily designed to closely follow
32 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
33 codes we used to have in the past. Following a structured
34 approach (preferably the official grammars) is believed to make it
35 much easier to add extensions and nifty pretty-printing effects that
36 takes expression or declaration contexts into account. */
39 #define pp_c_maybe_whitespace(PP) \
40 do { \
41 if ((PP)->padding == pp_before) \
42 pp_c_whitespace (PP); \
43 } while (0)
45 /* literal */
46 static void pp_c_char (c_pretty_printer *, int);
48 /* postfix-expression */
49 static void pp_c_initializer_list (c_pretty_printer *, tree);
50 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
52 static void pp_c_additive_expression (c_pretty_printer *, tree);
53 static void pp_c_shift_expression (c_pretty_printer *, tree);
54 static void pp_c_relational_expression (c_pretty_printer *, tree);
55 static void pp_c_equality_expression (c_pretty_printer *, tree);
56 static void pp_c_and_expression (c_pretty_printer *, tree);
57 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
58 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
59 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
61 /* declarations. */
64 /* Helper functions. */
66 void
67 pp_c_whitespace (c_pretty_printer *pp)
69 pp_space (pp);
70 pp->padding = pp_none;
73 void
74 pp_c_left_paren (c_pretty_printer *pp)
76 pp_left_paren (pp);
77 pp->padding = pp_none;
80 void
81 pp_c_right_paren (c_pretty_printer *pp)
83 pp_right_paren (pp);
84 pp->padding = pp_none;
87 void
88 pp_c_left_brace (c_pretty_printer *pp)
90 pp_left_brace (pp);
91 pp->padding = pp_none;
94 void
95 pp_c_right_brace (c_pretty_printer *pp)
97 pp_right_brace (pp);
98 pp->padding = pp_none;
101 void
102 pp_c_left_bracket (c_pretty_printer *pp)
104 pp_left_bracket (pp);
105 pp->padding = pp_none;
108 void
109 pp_c_right_bracket (c_pretty_printer *pp)
111 pp_right_bracket (pp);
112 pp->padding = pp_none;
115 void
116 pp_c_dot (c_pretty_printer *pp)
118 pp_dot (pp);
119 pp->padding = pp_none;
122 void
123 pp_c_ampersand (c_pretty_printer *pp)
125 pp_ampersand (pp);
126 pp->padding = pp_none;
129 void
130 pp_c_star (c_pretty_printer *pp)
132 pp_star (pp);
133 pp->padding = pp_none;
136 void
137 pp_c_arrow (c_pretty_printer *pp)
139 pp_arrow (pp);
140 pp->padding = pp_none;
143 void
144 pp_c_semicolon (c_pretty_printer *pp)
146 pp_semicolon (pp);
147 pp->padding = pp_none;
150 void
151 pp_c_complement (c_pretty_printer *pp)
153 pp_complement (pp);
154 pp->padding = pp_none;
157 void
158 pp_c_exclamation (c_pretty_printer *pp)
160 pp_exclamation (pp);
161 pp->padding = pp_none;
164 /* Print out the external representation of QUALIFIERS. */
166 void
167 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
169 const char *p = pp_last_position_in_text (pp);
171 if (!qualifiers)
172 return;
174 /* The C programming language does not have references, but it is much
175 simpler to handle those here rather than going through the same
176 logic in the C++ pretty-printer. */
177 if (p != NULL && (*p == '*' || *p == '&'))
178 pp_c_whitespace (pp);
180 if (qualifiers & TYPE_QUAL_ATOMIC)
181 pp_c_ws_string (pp, "_Atomic");
182 if (qualifiers & TYPE_QUAL_CONST)
183 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
184 if (qualifiers & TYPE_QUAL_VOLATILE)
185 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
186 if (qualifiers & TYPE_QUAL_RESTRICT)
187 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
188 ? "restrict" : "__restrict__"));
191 /* Pretty-print T using the type-cast notation '( type-name )'. */
193 static void
194 pp_c_type_cast (c_pretty_printer *pp, tree t)
196 pp_c_left_paren (pp);
197 pp->type_id (t);
198 pp_c_right_paren (pp);
201 /* We're about to pretty-print a pointer type as indicated by T.
202 Output a whitespace, if needed, preparing for subsequent output. */
204 void
205 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
207 if (POINTER_TYPE_P (t))
209 tree pointee = strip_pointer_operator (TREE_TYPE (t));
210 if (TREE_CODE (pointee) != ARRAY_TYPE
211 && TREE_CODE (pointee) != FUNCTION_TYPE)
212 pp_c_whitespace (pp);
217 /* Declarations. */
219 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
220 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
221 of its type. Take care of possible extensions.
223 type-qualifier-list:
224 type-qualifier
225 type-qualifier-list type-qualifier
227 type-qualifier:
228 const
229 restrict -- C99
230 __restrict__ -- GNU C
231 address-space-qualifier -- GNU C
232 volatile
233 _Atomic -- C11
235 address-space-qualifier:
236 identifier -- GNU C */
238 void
239 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
241 int qualifiers;
243 if (!t || t == error_mark_node)
244 return;
246 if (!TYPE_P (t))
247 t = TREE_TYPE (t);
249 qualifiers = TYPE_QUALS (t);
250 pp_c_cv_qualifiers (pp, qualifiers,
251 TREE_CODE (t) == FUNCTION_TYPE);
253 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
255 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
256 pp_c_identifier (pp, as);
260 /* pointer:
261 * type-qualifier-list(opt)
262 * type-qualifier-list(opt) pointer */
264 static void
265 pp_c_pointer (c_pretty_printer *pp, tree t)
267 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
268 t = TREE_TYPE (t);
269 switch (TREE_CODE (t))
271 case POINTER_TYPE:
272 /* It is easier to handle C++ reference types here. */
273 case REFERENCE_TYPE:
274 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
275 pp_c_pointer (pp, TREE_TYPE (t));
276 if (TREE_CODE (t) == POINTER_TYPE)
277 pp_c_star (pp);
278 else
279 pp_c_ampersand (pp);
280 pp_c_type_qualifier_list (pp, t);
281 break;
283 /* ??? This node is now in GENERIC and so shouldn't be here. But
284 we'll fix that later. */
285 case DECL_EXPR:
286 pp->declaration (DECL_EXPR_DECL (t));
287 pp_needs_newline (pp) = true;
288 break;
290 default:
291 pp_unsupported_tree (pp, t);
295 /* simple-type-specifier:
296 type-specifier
298 type-specifier:
299 void
300 char
301 short
303 long
304 float
305 double
306 signed
307 unsigned
308 _Bool -- C99
309 _Complex -- C99
310 _Imaginary -- C99
311 struct-or-union-specifier
312 enum-specifier
313 typedef-name.
315 GNU extensions.
316 simple-type-specifier:
317 __complex__
318 __vector__ */
320 void
321 c_pretty_printer::simple_type_specifier (tree t)
323 const enum tree_code code = TREE_CODE (t);
324 switch (code)
326 case ERROR_MARK:
327 translate_string ("<type-error>");
328 break;
330 case IDENTIFIER_NODE:
331 pp_c_identifier (this, IDENTIFIER_POINTER (t));
332 break;
334 case VOID_TYPE:
335 case BOOLEAN_TYPE:
336 case INTEGER_TYPE:
337 case REAL_TYPE:
338 case FIXED_POINT_TYPE:
339 if (TYPE_NAME (t))
341 t = TYPE_NAME (t);
342 simple_type_specifier (t);
344 else
346 int prec = TYPE_PRECISION (t);
347 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
348 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
349 else
350 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
351 if (TYPE_NAME (t))
353 simple_type_specifier (t);
354 if (TYPE_PRECISION (t) != prec)
356 pp_colon (this);
357 pp_decimal_int (this, prec);
360 else
362 switch (code)
364 case INTEGER_TYPE:
365 translate_string (TYPE_UNSIGNED (t)
366 ? "<unnamed-unsigned:"
367 : "<unnamed-signed:");
368 break;
369 case REAL_TYPE:
370 translate_string ("<unnamed-float:");
371 break;
372 case FIXED_POINT_TYPE:
373 translate_string ("<unnamed-fixed:");
374 break;
375 default:
376 gcc_unreachable ();
378 pp_decimal_int (this, prec);
379 pp_greater (this);
382 break;
384 case TYPE_DECL:
385 if (DECL_NAME (t))
386 id_expression (t);
387 else
388 translate_string ("<typedef-error>");
389 break;
391 case UNION_TYPE:
392 case RECORD_TYPE:
393 case ENUMERAL_TYPE:
394 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
395 /* Don't decorate the type if this is a typedef name. */;
396 else if (code == UNION_TYPE)
397 pp_c_ws_string (this, "union");
398 else if (code == RECORD_TYPE)
399 pp_c_ws_string (this, "struct");
400 else if (code == ENUMERAL_TYPE)
401 pp_c_ws_string (this, "enum");
402 else
403 translate_string ("<tag-error>");
405 if (TYPE_NAME (t))
406 id_expression (TYPE_NAME (t));
407 else
408 translate_string ("<anonymous>");
409 break;
411 default:
412 pp_unsupported_tree (this, t);
413 break;
417 /* specifier-qualifier-list:
418 type-specifier specifier-qualifier-list-opt
419 type-qualifier specifier-qualifier-list-opt
422 Implementation note: Because of the non-linearities in array or
423 function declarations, this routine prints not just the
424 specifier-qualifier-list of such entities or types of such entities,
425 but also the 'pointer' production part of their declarators. The
426 remaining part is done by declarator() or abstract_declarator(). */
428 void
429 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
431 const enum tree_code code = TREE_CODE (t);
433 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
434 pp_c_type_qualifier_list (pp, t);
435 switch (code)
437 case REFERENCE_TYPE:
438 case POINTER_TYPE:
440 /* Get the types-specifier of this type. */
441 tree pointee = strip_pointer_operator (TREE_TYPE (t));
442 pp_c_specifier_qualifier_list (pp, pointee);
443 if (TREE_CODE (pointee) == ARRAY_TYPE
444 || TREE_CODE (pointee) == FUNCTION_TYPE)
446 pp_c_whitespace (pp);
447 pp_c_left_paren (pp);
448 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
450 else if (!c_dialect_cxx ())
451 pp_c_whitespace (pp);
452 pp_ptr_operator (pp, t);
454 break;
456 case FUNCTION_TYPE:
457 case ARRAY_TYPE:
458 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
459 break;
461 case VECTOR_TYPE:
462 case COMPLEX_TYPE:
463 if (code == COMPLEX_TYPE)
464 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
465 ? "_Complex" : "__complex__"));
466 else if (code == VECTOR_TYPE)
468 pp_c_ws_string (pp, "__vector");
469 pp_c_left_paren (pp);
470 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
471 pp_c_right_paren (pp);
472 pp_c_whitespace (pp);
474 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
475 break;
477 default:
478 pp->simple_type_specifier (t);
479 break;
481 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
482 pp_c_type_qualifier_list (pp, t);
485 /* parameter-type-list:
486 parameter-list
487 parameter-list , ...
489 parameter-list:
490 parameter-declaration
491 parameter-list , parameter-declaration
493 parameter-declaration:
494 declaration-specifiers declarator
495 declaration-specifiers abstract-declarator(opt) */
497 void
498 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
500 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
501 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
502 pp_c_left_paren (pp);
503 if (parms == void_list_node)
504 pp_c_ws_string (pp, "void");
505 else
507 bool first = true;
508 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
510 if (!first)
511 pp_separate_with (pp, ',');
512 first = false;
513 pp->declaration_specifiers
514 (want_parm_decl ? parms : TREE_VALUE (parms));
515 if (want_parm_decl)
516 pp->declarator (parms);
517 else
518 pp->abstract_declarator (TREE_VALUE (parms));
521 pp_c_right_paren (pp);
524 /* abstract-declarator:
525 pointer
526 pointer(opt) direct-abstract-declarator */
528 void
529 c_pretty_printer::abstract_declarator (tree t)
531 if (TREE_CODE (t) == POINTER_TYPE)
533 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
534 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
535 pp_c_right_paren (this);
536 t = TREE_TYPE (t);
539 direct_abstract_declarator (t);
542 /* direct-abstract-declarator:
543 ( abstract-declarator )
544 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
545 direct-abstract-declarator(opt) [ * ]
546 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
548 void
549 c_pretty_printer::direct_abstract_declarator (tree t)
551 switch (TREE_CODE (t))
553 case POINTER_TYPE:
554 abstract_declarator (t);
555 break;
557 case FUNCTION_TYPE:
558 pp_c_parameter_type_list (this, t);
559 direct_abstract_declarator (TREE_TYPE (t));
560 break;
562 case ARRAY_TYPE:
563 pp_c_left_bracket (this);
564 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
566 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
567 tree type = TREE_TYPE (maxval);
569 if (tree_fits_shwi_p (maxval))
570 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
571 else
572 expression (fold_build2 (PLUS_EXPR, type, maxval,
573 build_int_cst (type, 1)));
575 pp_c_right_bracket (this);
576 direct_abstract_declarator (TREE_TYPE (t));
577 break;
579 case IDENTIFIER_NODE:
580 case VOID_TYPE:
581 case BOOLEAN_TYPE:
582 case INTEGER_TYPE:
583 case REAL_TYPE:
584 case FIXED_POINT_TYPE:
585 case ENUMERAL_TYPE:
586 case RECORD_TYPE:
587 case UNION_TYPE:
588 case VECTOR_TYPE:
589 case COMPLEX_TYPE:
590 case TYPE_DECL:
591 break;
593 default:
594 pp_unsupported_tree (this, t);
595 break;
599 /* type-name:
600 specifier-qualifier-list abstract-declarator(opt) */
602 void
603 c_pretty_printer::type_id (tree t)
605 pp_c_specifier_qualifier_list (this, t);
606 abstract_declarator (t);
609 /* storage-class-specifier:
610 typedef
611 extern
612 static
613 auto
614 register */
616 void
617 c_pretty_printer::storage_class_specifier (tree t)
619 if (TREE_CODE (t) == TYPE_DECL)
620 pp_c_ws_string (this, "typedef");
621 else if (DECL_P (t))
623 if (DECL_REGISTER (t))
624 pp_c_ws_string (this, "register");
625 else if (TREE_STATIC (t) && VAR_P (t))
626 pp_c_ws_string (this, "static");
630 /* function-specifier:
631 inline */
633 void
634 c_pretty_printer::function_specifier (tree t)
636 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
637 pp_c_ws_string (this, "inline");
640 /* declaration-specifiers:
641 storage-class-specifier declaration-specifiers(opt)
642 type-specifier declaration-specifiers(opt)
643 type-qualifier declaration-specifiers(opt)
644 function-specifier declaration-specifiers(opt) */
646 void
647 c_pretty_printer::declaration_specifiers (tree t)
649 storage_class_specifier (t);
650 function_specifier (t);
651 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
654 /* direct-declarator
655 identifier
656 ( declarator )
657 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
658 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
659 direct-declarator [ type-qualifier-list static assignment-expression ]
660 direct-declarator [ type-qualifier-list * ]
661 direct-declarator ( parameter-type-list )
662 direct-declarator ( identifier-list(opt) ) */
664 void
665 c_pretty_printer::direct_declarator (tree t)
667 switch (TREE_CODE (t))
669 case VAR_DECL:
670 case PARM_DECL:
671 case TYPE_DECL:
672 case FIELD_DECL:
673 case LABEL_DECL:
674 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
675 pp_c_tree_decl_identifier (this, t);
676 break;
678 case ARRAY_TYPE:
679 case POINTER_TYPE:
680 abstract_declarator (TREE_TYPE (t));
681 break;
683 case FUNCTION_TYPE:
684 pp_parameter_list (this, t);
685 abstract_declarator (TREE_TYPE (t));
686 break;
688 case FUNCTION_DECL:
689 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
690 pp_c_tree_decl_identifier (this, t);
691 if (flags & pp_c_flag_abstract)
692 abstract_declarator (TREE_TYPE (t));
693 else
695 pp_parameter_list (this, t);
696 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
698 break;
700 case INTEGER_TYPE:
701 case REAL_TYPE:
702 case FIXED_POINT_TYPE:
703 case ENUMERAL_TYPE:
704 case UNION_TYPE:
705 case RECORD_TYPE:
706 break;
708 default:
709 pp_unsupported_tree (this, t);
710 break;
715 /* declarator:
716 pointer(opt) direct-declarator */
718 void
719 c_pretty_printer::declarator (tree t)
721 switch (TREE_CODE (t))
723 case INTEGER_TYPE:
724 case REAL_TYPE:
725 case FIXED_POINT_TYPE:
726 case ENUMERAL_TYPE:
727 case UNION_TYPE:
728 case RECORD_TYPE:
729 break;
731 case VAR_DECL:
732 case PARM_DECL:
733 case FIELD_DECL:
734 case ARRAY_TYPE:
735 case FUNCTION_TYPE:
736 case FUNCTION_DECL:
737 case TYPE_DECL:
738 direct_declarator (t);
739 break;
742 default:
743 pp_unsupported_tree (this, t);
744 break;
748 /* declaration:
749 declaration-specifiers init-declarator-list(opt) ; */
751 void
752 c_pretty_printer::declaration (tree t)
754 declaration_specifiers (t);
755 pp_c_init_declarator (this, t);
758 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
760 void
761 pp_c_attributes (c_pretty_printer *pp, tree attributes)
763 if (attributes == NULL_TREE)
764 return;
766 pp_c_ws_string (pp, "__attribute__");
767 pp_c_left_paren (pp);
768 pp_c_left_paren (pp);
769 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
771 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
772 if (TREE_VALUE (attributes))
773 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
775 if (TREE_CHAIN (attributes))
776 pp_separate_with (pp, ',');
778 pp_c_right_paren (pp);
779 pp_c_right_paren (pp);
782 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
783 marked to be displayed on disgnostic. */
785 void
786 pp_c_attributes_display (c_pretty_printer *pp, tree a)
788 bool is_first = true;
790 if (a == NULL_TREE)
791 return;
793 for (; a != NULL_TREE; a = TREE_CHAIN (a))
795 const struct attribute_spec *as;
796 as = lookup_attribute_spec (TREE_PURPOSE (a));
797 if (!as || as->affects_type_identity == false)
798 continue;
799 if (c_dialect_cxx ()
800 && !strcmp ("transaction_safe", as->name))
801 /* In C++ transaction_safe is printed at the end of the declarator. */
802 continue;
803 if (is_first)
805 pp_c_ws_string (pp, "__attribute__");
806 pp_c_left_paren (pp);
807 pp_c_left_paren (pp);
808 is_first = false;
810 else
812 pp_separate_with (pp, ',');
814 pp_tree_identifier (pp, TREE_PURPOSE (a));
815 if (TREE_VALUE (a))
816 pp_c_call_argument_list (pp, TREE_VALUE (a));
819 if (!is_first)
821 pp_c_right_paren (pp);
822 pp_c_right_paren (pp);
823 pp_c_whitespace (pp);
827 /* function-definition:
828 declaration-specifiers declarator compound-statement */
830 void
831 pp_c_function_definition (c_pretty_printer *pp, tree t)
833 pp->declaration_specifiers (t);
834 pp->declarator (t);
835 pp_needs_newline (pp) = true;
836 pp->statement (DECL_SAVED_TREE (t));
837 pp_newline_and_flush (pp);
841 /* Expressions. */
843 /* Print out a c-char. This is called solely for characters which are
844 in the *target* execution character set. We ought to convert them
845 back to the *host* execution character set before printing, but we
846 have no way to do this at present. A decent compromise is to print
847 all characters as if they were in the host execution character set,
848 and not attempt to recover any named escape characters, but render
849 all unprintables as octal escapes. If the host and target character
850 sets are the same, this produces relatively readable output. If they
851 are not the same, strings may appear as gibberish, but that's okay
852 (in fact, it may well be what the reader wants, e.g. if they are looking
853 to see if conversion to the target character set happened correctly).
855 A special case: we need to prefix \, ", and ' with backslashes. It is
856 correct to do so for the *host*'s \, ", and ', because the rest of the
857 file appears in the host character set. */
859 static void
860 pp_c_char (c_pretty_printer *pp, int c)
862 if (ISPRINT (c))
864 switch (c)
866 case '\\': pp_string (pp, "\\\\"); break;
867 case '\'': pp_string (pp, "\\\'"); break;
868 case '\"': pp_string (pp, "\\\""); break;
869 default: pp_character (pp, c);
872 else
873 pp_scalar (pp, "\\%03o", (unsigned) c);
876 /* Print out a STRING literal. */
878 void
879 pp_c_string_literal (c_pretty_printer *pp, tree s)
881 const char *p = TREE_STRING_POINTER (s);
882 int n = TREE_STRING_LENGTH (s) - 1;
883 int i;
884 pp_doublequote (pp);
885 for (i = 0; i < n; ++i)
886 pp_c_char (pp, p[i]);
887 pp_doublequote (pp);
890 /* Pretty-print a VOID_CST (void_node). */
892 static void
893 pp_c_void_constant (c_pretty_printer *pp)
895 pp_c_type_cast (pp, void_type_node);
896 pp_string (pp, "0");
899 /* Pretty-print an INTEGER literal. */
901 static void
902 pp_c_integer_constant (c_pretty_printer *pp, tree i)
904 int idx;
906 /* We are going to compare the type of I to other types using
907 pointer comparison so we need to use its canonical type. */
908 tree type =
909 TYPE_CANONICAL (TREE_TYPE (i))
910 ? TYPE_CANONICAL (TREE_TYPE (i))
911 : TREE_TYPE (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 = i;
921 if (wi::lt_p (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);
929 if (TYPE_UNSIGNED (type))
930 pp_character (pp, 'u');
931 if (type == long_integer_type_node || type == long_unsigned_type_node)
932 pp_character (pp, 'l');
933 else if (type == long_long_integer_type_node
934 || type == long_long_unsigned_type_node)
935 pp_string (pp, "ll");
936 else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++)
937 if (int_n_enabled_p[idx])
939 char buf[2+20];
940 if (type == int_n_trees[idx].signed_type
941 || type == int_n_trees[idx].unsigned_type)
943 sprintf (buf, "I%d", int_n_data[idx].bitsize);
944 pp_string (pp, buf);
949 /* Print out a CHARACTER literal. */
951 static void
952 pp_c_character_constant (c_pretty_printer *pp, tree c)
954 pp_quote (pp);
955 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
956 pp_quote (pp);
959 /* Print out a BOOLEAN literal. */
961 static void
962 pp_c_bool_constant (c_pretty_printer *pp, tree b)
964 if (b == boolean_false_node)
966 if (c_dialect_cxx ())
967 pp_c_ws_string (pp, "false");
968 else if (flag_isoc99)
969 pp_c_ws_string (pp, "_False");
970 else
971 pp_unsupported_tree (pp, b);
973 else if (b == boolean_true_node)
975 if (c_dialect_cxx ())
976 pp_c_ws_string (pp, "true");
977 else if (flag_isoc99)
978 pp_c_ws_string (pp, "_True");
979 else
980 pp_unsupported_tree (pp, b);
982 else if (TREE_CODE (b) == INTEGER_CST)
983 pp_c_integer_constant (pp, b);
984 else
985 pp_unsupported_tree (pp, b);
988 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
989 false; that means the value was obtained by a cast, in which case
990 print out the type-id part of the cast-expression -- the casted value
991 is then printed by pp_c_integer_literal. */
993 static bool
994 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
996 bool value_is_named = true;
997 tree type = TREE_TYPE (e);
998 tree value;
1000 /* Find the name of this constant. */
1001 for (value = TYPE_VALUES (type);
1002 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1003 value = TREE_CHAIN (value))
1006 if (value != NULL_TREE)
1007 pp->id_expression (TREE_PURPOSE (value));
1008 else
1010 /* Value must have been cast. */
1011 pp_c_type_cast (pp, type);
1012 value_is_named = false;
1015 return value_is_named;
1018 /* Print out a REAL value as a decimal-floating-constant. */
1020 static void
1021 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1023 const struct real_format *fmt
1024 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1026 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1027 bool is_decimal = floating_cst.decimal;
1029 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1030 log10(2) to 7 significant digits. */
1031 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1033 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1034 sizeof (pp_buffer (pp)->digit_buffer),
1035 max_digits10, 1);
1037 pp_string (pp, pp_buffer(pp)->digit_buffer);
1038 if (TREE_TYPE (r) == float_type_node)
1039 pp_character (pp, 'f');
1040 else if (TREE_TYPE (r) == long_double_type_node)
1041 pp_character (pp, 'l');
1042 else if (TREE_TYPE (r) == dfloat128_type_node)
1043 pp_string (pp, "dl");
1044 else if (TREE_TYPE (r) == dfloat64_type_node)
1045 pp_string (pp, "dd");
1046 else if (TREE_TYPE (r) == dfloat32_type_node)
1047 pp_string (pp, "df");
1048 else if (TREE_TYPE (r) != double_type_node)
1049 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1050 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i))
1052 pp_character (pp, 'f');
1053 pp_decimal_int (pp, floatn_nx_types[i].n);
1054 if (floatn_nx_types[i].extended)
1055 pp_character (pp, 'x');
1056 break;
1060 /* Print out a FIXED value as a decimal-floating-constant. */
1062 static void
1063 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1065 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1066 sizeof (pp_buffer (pp)->digit_buffer));
1067 pp_string (pp, pp_buffer(pp)->digit_buffer);
1070 /* Pretty-print a compound literal expression. GNU extensions include
1071 vector constants. */
1073 static void
1074 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1076 tree type = TREE_TYPE (e);
1077 pp_c_type_cast (pp, type);
1079 switch (TREE_CODE (type))
1081 case RECORD_TYPE:
1082 case UNION_TYPE:
1083 case ARRAY_TYPE:
1084 case VECTOR_TYPE:
1085 case COMPLEX_TYPE:
1086 pp_c_brace_enclosed_initializer_list (pp, e);
1087 break;
1089 default:
1090 pp_unsupported_tree (pp, e);
1091 break;
1095 /* Pretty-print a COMPLEX_EXPR expression. */
1097 static void
1098 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1100 /* Handle a few common special cases, otherwise fallback
1101 to printing it as compound literal. */
1102 tree type = TREE_TYPE (e);
1103 tree realexpr = TREE_OPERAND (e, 0);
1104 tree imagexpr = TREE_OPERAND (e, 1);
1106 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1107 if (TREE_CODE (realexpr) == NOP_EXPR
1108 && TREE_CODE (imagexpr) == NOP_EXPR
1109 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1110 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1111 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1112 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1113 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1114 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1116 pp_c_type_cast (pp, type);
1117 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1118 return;
1121 /* Cast of an scalar expression to COMPLEX_TYPE. */
1122 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1123 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1125 pp_c_type_cast (pp, type);
1126 if (TREE_CODE (realexpr) == NOP_EXPR)
1127 realexpr = TREE_OPERAND (realexpr, 0);
1128 pp->expression (realexpr);
1129 return;
1132 pp_c_compound_literal (pp, e);
1135 /* constant:
1136 integer-constant
1137 floating-constant
1138 fixed-point-constant
1139 enumeration-constant
1140 character-constant */
1142 void
1143 c_pretty_printer::constant (tree e)
1145 const enum tree_code code = TREE_CODE (e);
1147 switch (code)
1149 case VOID_CST:
1150 pp_c_void_constant (this);
1151 break;
1153 case INTEGER_CST:
1155 tree type = TREE_TYPE (e);
1156 if (type == boolean_type_node)
1157 pp_c_bool_constant (this, e);
1158 else if (type == char_type_node)
1159 pp_c_character_constant (this, e);
1160 else if (TREE_CODE (type) == ENUMERAL_TYPE
1161 && pp_c_enumeration_constant (this, e))
1163 else
1164 pp_c_integer_constant (this, e);
1166 break;
1168 case REAL_CST:
1169 pp_c_floating_constant (this, e);
1170 break;
1172 case FIXED_CST:
1173 pp_c_fixed_constant (this, e);
1174 break;
1176 case STRING_CST:
1177 pp_c_string_literal (this, e);
1178 break;
1180 case COMPLEX_CST:
1181 /* Sometimes, we are confused and we think a complex literal
1182 is a constant. Such thing is a compound literal which
1183 grammatically belongs to postfix-expr production. */
1184 pp_c_compound_literal (this, e);
1185 break;
1187 default:
1188 pp_unsupported_tree (this, e);
1189 break;
1193 /* Pretty-print a string such as an identifier, without changing its
1194 encoding, preceded by whitespace is necessary. */
1196 void
1197 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1199 pp_c_maybe_whitespace (pp);
1200 pp_string (pp, str);
1201 pp->padding = pp_before;
1204 void
1205 c_pretty_printer::translate_string (const char *gmsgid)
1207 if (pp_translate_identifiers (this))
1208 pp_c_ws_string (this, _(gmsgid));
1209 else
1210 pp_c_ws_string (this, gmsgid);
1213 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1214 that need converting to the locale encoding, preceded by whitespace
1215 is necessary. */
1217 void
1218 pp_c_identifier (c_pretty_printer *pp, const char *id)
1220 pp_c_maybe_whitespace (pp);
1221 pp_identifier (pp, id);
1222 pp->padding = pp_before;
1225 /* Pretty-print a C primary-expression.
1226 primary-expression:
1227 identifier
1228 constant
1229 string-literal
1230 ( expression ) */
1232 void
1233 c_pretty_printer::primary_expression (tree e)
1235 switch (TREE_CODE (e))
1237 case VAR_DECL:
1238 case PARM_DECL:
1239 case FIELD_DECL:
1240 case CONST_DECL:
1241 case FUNCTION_DECL:
1242 case LABEL_DECL:
1243 pp_c_tree_decl_identifier (this, e);
1244 break;
1246 case IDENTIFIER_NODE:
1247 pp_c_tree_identifier (this, e);
1248 break;
1250 case ERROR_MARK:
1251 translate_string ("<erroneous-expression>");
1252 break;
1254 case RESULT_DECL:
1255 translate_string ("<return-value>");
1256 break;
1258 case VOID_CST:
1259 case INTEGER_CST:
1260 case REAL_CST:
1261 case FIXED_CST:
1262 case STRING_CST:
1263 constant (e);
1264 break;
1266 case TARGET_EXPR:
1267 pp_c_ws_string (this, "__builtin_memcpy");
1268 pp_c_left_paren (this);
1269 pp_ampersand (this);
1270 primary_expression (TREE_OPERAND (e, 0));
1271 pp_separate_with (this, ',');
1272 pp_ampersand (this);
1273 initializer (TREE_OPERAND (e, 1));
1274 if (TREE_OPERAND (e, 2))
1276 pp_separate_with (this, ',');
1277 expression (TREE_OPERAND (e, 2));
1279 pp_c_right_paren (this);
1280 break;
1282 default:
1283 /* FIXME: Make sure we won't get into an infinite loop. */
1284 pp_c_left_paren (this);
1285 expression (e);
1286 pp_c_right_paren (this);
1287 break;
1291 /* Print out a C initializer -- also support C compound-literals.
1292 initializer:
1293 assignment-expression:
1294 { initializer-list }
1295 { initializer-list , } */
1297 void
1298 c_pretty_printer::initializer (tree e)
1300 if (TREE_CODE (e) == CONSTRUCTOR)
1301 pp_c_brace_enclosed_initializer_list (this, e);
1302 else
1303 expression (e);
1306 /* init-declarator:
1307 declarator:
1308 declarator = initializer */
1310 void
1311 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1313 pp->declarator (t);
1314 /* We don't want to output function definitions here. There are handled
1315 elsewhere (and the syntactic form is bogus anyway). */
1316 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1318 tree init = DECL_INITIAL (t);
1319 /* This C++ bit is handled here because it is easier to do so.
1320 In templates, the C++ parser builds a TREE_LIST for a
1321 direct-initialization; the TREE_PURPOSE is the variable to
1322 initialize and the TREE_VALUE is the initializer. */
1323 if (TREE_CODE (init) == TREE_LIST)
1325 pp_c_left_paren (pp);
1326 pp->expression (TREE_VALUE (init));
1327 pp_right_paren (pp);
1329 else
1331 pp_space (pp);
1332 pp_equal (pp);
1333 pp_space (pp);
1334 pp->initializer (init);
1339 /* initializer-list:
1340 designation(opt) initializer
1341 initializer-list , designation(opt) initializer
1343 designation:
1344 designator-list =
1346 designator-list:
1347 designator
1348 designator-list designator
1350 designator:
1351 [ constant-expression ]
1352 identifier */
1354 static void
1355 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1357 tree type = TREE_TYPE (e);
1358 const enum tree_code code = TREE_CODE (type);
1360 if (TREE_CODE (e) == CONSTRUCTOR)
1362 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1363 return;
1366 switch (code)
1368 case RECORD_TYPE:
1369 case UNION_TYPE:
1370 case ARRAY_TYPE:
1372 tree init = TREE_OPERAND (e, 0);
1373 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1375 if (code == RECORD_TYPE || code == UNION_TYPE)
1377 pp_c_dot (pp);
1378 pp->primary_expression (TREE_PURPOSE (init));
1380 else
1382 pp_c_left_bracket (pp);
1383 if (TREE_PURPOSE (init))
1384 pp->constant (TREE_PURPOSE (init));
1385 pp_c_right_bracket (pp);
1387 pp_c_whitespace (pp);
1388 pp_equal (pp);
1389 pp_c_whitespace (pp);
1390 pp->initializer (TREE_VALUE (init));
1391 if (TREE_CHAIN (init))
1392 pp_separate_with (pp, ',');
1395 return;
1397 case VECTOR_TYPE:
1398 if (TREE_CODE (e) == VECTOR_CST)
1400 unsigned i;
1401 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1403 if (i > 0)
1404 pp_separate_with (pp, ',');
1405 pp->expression (VECTOR_CST_ELT (e, i));
1408 else
1409 break;
1410 return;
1412 case COMPLEX_TYPE:
1413 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1415 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1416 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1417 pp_separate_with (pp, ',');
1418 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1420 else
1421 break;
1422 return;
1424 default:
1425 break;
1428 pp_unsupported_tree (pp, type);
1431 /* Pretty-print a brace-enclosed initializer-list. */
1433 static void
1434 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1436 pp_c_left_brace (pp);
1437 pp_c_initializer_list (pp, l);
1438 pp_c_right_brace (pp);
1442 /* This is a convenient function, used to bridge gap between C and C++
1443 grammars.
1445 id-expression:
1446 identifier */
1448 void
1449 c_pretty_printer::id_expression (tree t)
1451 switch (TREE_CODE (t))
1453 case VAR_DECL:
1454 case PARM_DECL:
1455 case CONST_DECL:
1456 case TYPE_DECL:
1457 case FUNCTION_DECL:
1458 case FIELD_DECL:
1459 case LABEL_DECL:
1460 pp_c_tree_decl_identifier (this, t);
1461 break;
1463 case IDENTIFIER_NODE:
1464 pp_c_tree_identifier (this, t);
1465 break;
1467 default:
1468 pp_unsupported_tree (this, t);
1469 break;
1473 /* postfix-expression:
1474 primary-expression
1475 postfix-expression [ expression ]
1476 postfix-expression ( argument-expression-list(opt) )
1477 postfix-expression . identifier
1478 postfix-expression -> identifier
1479 postfix-expression ++
1480 postfix-expression --
1481 ( type-name ) { initializer-list }
1482 ( type-name ) { initializer-list , } */
1484 void
1485 c_pretty_printer::postfix_expression (tree e)
1487 enum tree_code code = TREE_CODE (e);
1488 switch (code)
1490 case POSTINCREMENT_EXPR:
1491 case POSTDECREMENT_EXPR:
1492 postfix_expression (TREE_OPERAND (e, 0));
1493 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1494 break;
1496 case ARRAY_REF:
1497 postfix_expression (TREE_OPERAND (e, 0));
1498 pp_c_left_bracket (this);
1499 expression (TREE_OPERAND (e, 1));
1500 pp_c_right_bracket (this);
1501 break;
1503 case ARRAY_NOTATION_REF:
1504 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1505 pp_c_left_bracket (this);
1506 expression (ARRAY_NOTATION_START (e));
1507 pp_colon (this);
1508 expression (ARRAY_NOTATION_LENGTH (e));
1509 pp_colon (this);
1510 expression (ARRAY_NOTATION_STRIDE (e));
1511 pp_c_right_bracket (this);
1512 break;
1514 case CALL_EXPR:
1516 call_expr_arg_iterator iter;
1517 tree arg;
1518 postfix_expression (CALL_EXPR_FN (e));
1519 pp_c_left_paren (this);
1520 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1522 expression (arg);
1523 if (more_call_expr_args_p (&iter))
1524 pp_separate_with (this, ',');
1526 pp_c_right_paren (this);
1527 break;
1530 case UNORDERED_EXPR:
1531 pp_c_ws_string (this, flag_isoc99
1532 ? "isunordered"
1533 : "__builtin_isunordered");
1534 goto two_args_fun;
1536 case ORDERED_EXPR:
1537 pp_c_ws_string (this, flag_isoc99
1538 ? "!isunordered"
1539 : "!__builtin_isunordered");
1540 goto two_args_fun;
1542 case UNLT_EXPR:
1543 pp_c_ws_string (this, flag_isoc99
1544 ? "!isgreaterequal"
1545 : "!__builtin_isgreaterequal");
1546 goto two_args_fun;
1548 case UNLE_EXPR:
1549 pp_c_ws_string (this, flag_isoc99
1550 ? "!isgreater"
1551 : "!__builtin_isgreater");
1552 goto two_args_fun;
1554 case UNGT_EXPR:
1555 pp_c_ws_string (this, flag_isoc99
1556 ? "!islessequal"
1557 : "!__builtin_islessequal");
1558 goto two_args_fun;
1560 case UNGE_EXPR:
1561 pp_c_ws_string (this, flag_isoc99
1562 ? "!isless"
1563 : "!__builtin_isless");
1564 goto two_args_fun;
1566 case UNEQ_EXPR:
1567 pp_c_ws_string (this, flag_isoc99
1568 ? "!islessgreater"
1569 : "!__builtin_islessgreater");
1570 goto two_args_fun;
1572 case LTGT_EXPR:
1573 pp_c_ws_string (this, flag_isoc99
1574 ? "islessgreater"
1575 : "__builtin_islessgreater");
1576 goto two_args_fun;
1578 two_args_fun:
1579 pp_c_left_paren (this);
1580 expression (TREE_OPERAND (e, 0));
1581 pp_separate_with (this, ',');
1582 expression (TREE_OPERAND (e, 1));
1583 pp_c_right_paren (this);
1584 break;
1586 case ABS_EXPR:
1587 pp_c_ws_string (this, "__builtin_abs");
1588 pp_c_left_paren (this);
1589 expression (TREE_OPERAND (e, 0));
1590 pp_c_right_paren (this);
1591 break;
1593 case COMPONENT_REF:
1595 tree object = TREE_OPERAND (e, 0);
1596 if (INDIRECT_REF_P (object))
1598 postfix_expression (TREE_OPERAND (object, 0));
1599 pp_c_arrow (this);
1601 else
1603 postfix_expression (object);
1604 pp_c_dot (this);
1606 expression (TREE_OPERAND (e, 1));
1608 break;
1610 case BIT_FIELD_REF:
1612 tree type = TREE_TYPE (e);
1614 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1615 if (type
1616 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1618 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1619 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1620 if ((bitpos % size) == 0)
1622 pp_c_left_paren (this);
1623 pp_c_left_paren (this);
1624 type_id (type);
1625 pp_c_star (this);
1626 pp_c_right_paren (this);
1627 pp_c_ampersand (this);
1628 expression (TREE_OPERAND (e, 0));
1629 pp_c_right_paren (this);
1630 pp_c_left_bracket (this);
1631 pp_wide_integer (this, bitpos / size);
1632 pp_c_right_bracket (this);
1633 break;
1636 pp_unsupported_tree (this, e);
1638 break;
1640 case MEM_REF:
1641 expression (e);
1642 break;
1644 case COMPLEX_CST:
1645 case VECTOR_CST:
1646 pp_c_compound_literal (this, e);
1647 break;
1649 case COMPLEX_EXPR:
1650 pp_c_complex_expr (this, e);
1651 break;
1653 case COMPOUND_LITERAL_EXPR:
1654 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1655 /* Fall through. */
1656 case CONSTRUCTOR:
1657 initializer (e);
1658 break;
1660 case VA_ARG_EXPR:
1661 pp_c_ws_string (this, "__builtin_va_arg");
1662 pp_c_left_paren (this);
1663 assignment_expression (TREE_OPERAND (e, 0));
1664 pp_separate_with (this, ',');
1665 type_id (TREE_TYPE (e));
1666 pp_c_right_paren (this);
1667 break;
1669 case ADDR_EXPR:
1670 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1672 id_expression (TREE_OPERAND (e, 0));
1673 break;
1675 /* fall through. */
1677 default:
1678 primary_expression (e);
1679 break;
1683 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1685 void
1686 pp_c_expression_list (c_pretty_printer *pp, tree e)
1688 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1690 pp->expression (TREE_VALUE (e));
1691 if (TREE_CHAIN (e))
1692 pp_separate_with (pp, ',');
1696 /* Print out V, which contains the elements of a constructor. */
1698 void
1699 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1701 unsigned HOST_WIDE_INT ix;
1702 tree value;
1704 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1706 pp->expression (value);
1707 if (ix != vec_safe_length (v) - 1)
1708 pp_separate_with (pp, ',');
1712 /* Print out an expression-list in parens, as if it were the argument
1713 list to a function. */
1715 void
1716 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1718 pp_c_left_paren (pp);
1719 if (t && TREE_CODE (t) == TREE_LIST)
1720 pp_c_expression_list (pp, t);
1721 pp_c_right_paren (pp);
1724 /* unary-expression:
1725 postfix-expression
1726 ++ cast-expression
1727 -- cast-expression
1728 unary-operator cast-expression
1729 sizeof unary-expression
1730 sizeof ( type-id )
1732 unary-operator: one of
1733 * & + - ! ~
1735 GNU extensions.
1736 unary-expression:
1737 __alignof__ unary-expression
1738 __alignof__ ( type-id )
1739 __real__ unary-expression
1740 __imag__ unary-expression */
1742 void
1743 c_pretty_printer::unary_expression (tree e)
1745 enum tree_code code = TREE_CODE (e);
1746 switch (code)
1748 case PREINCREMENT_EXPR:
1749 case PREDECREMENT_EXPR:
1750 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1751 unary_expression (TREE_OPERAND (e, 0));
1752 break;
1754 case ADDR_EXPR:
1755 case INDIRECT_REF:
1756 case NEGATE_EXPR:
1757 case BIT_NOT_EXPR:
1758 case TRUTH_NOT_EXPR:
1759 case CONJ_EXPR:
1760 /* String literal are used by address. */
1761 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1762 pp_ampersand (this);
1763 else if (code == INDIRECT_REF)
1765 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
1766 if (type && TREE_CODE (type) == REFERENCE_TYPE)
1767 /* Reference decay is implicit, don't print anything. */;
1768 else
1769 pp_c_star (this);
1771 else if (code == NEGATE_EXPR)
1772 pp_minus (this);
1773 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1774 pp_complement (this);
1775 else if (code == TRUTH_NOT_EXPR)
1776 pp_exclamation (this);
1777 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1778 break;
1780 case MEM_REF:
1781 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1782 && integer_zerop (TREE_OPERAND (e, 1)))
1783 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1784 else
1786 pp_c_star (this);
1787 if (!integer_zerop (TREE_OPERAND (e, 1)))
1789 pp_c_left_paren (this);
1790 if (!integer_onep (TYPE_SIZE_UNIT
1791 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1792 pp_c_type_cast (this, ptr_type_node);
1794 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1795 if (!integer_zerop (TREE_OPERAND (e, 1)))
1797 pp_plus (this);
1798 pp_c_integer_constant (this,
1799 fold_convert (ssizetype,
1800 TREE_OPERAND (e, 1)));
1801 pp_c_right_paren (this);
1804 break;
1806 case REALPART_EXPR:
1807 case IMAGPART_EXPR:
1808 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1809 pp_c_whitespace (this);
1810 unary_expression (TREE_OPERAND (e, 0));
1811 break;
1813 default:
1814 postfix_expression (e);
1815 break;
1819 /* cast-expression:
1820 unary-expression
1821 ( type-name ) cast-expression */
1823 void
1824 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1826 switch (TREE_CODE (e))
1828 case FLOAT_EXPR:
1829 case FIX_TRUNC_EXPR:
1830 CASE_CONVERT:
1831 case VIEW_CONVERT_EXPR:
1832 pp_c_type_cast (pp, TREE_TYPE (e));
1833 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1834 break;
1836 default:
1837 pp->unary_expression (e);
1841 /* multiplicative-expression:
1842 cast-expression
1843 multiplicative-expression * cast-expression
1844 multiplicative-expression / cast-expression
1845 multiplicative-expression % cast-expression */
1847 void
1848 c_pretty_printer::multiplicative_expression (tree e)
1850 enum tree_code code = TREE_CODE (e);
1851 switch (code)
1853 case MULT_EXPR:
1854 case TRUNC_DIV_EXPR:
1855 case TRUNC_MOD_EXPR:
1856 multiplicative_expression (TREE_OPERAND (e, 0));
1857 pp_c_whitespace (this);
1858 if (code == MULT_EXPR)
1859 pp_c_star (this);
1860 else if (code == TRUNC_DIV_EXPR)
1861 pp_slash (this);
1862 else
1863 pp_modulo (this);
1864 pp_c_whitespace (this);
1865 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1866 break;
1868 default:
1869 pp_c_cast_expression (this, e);
1870 break;
1874 /* additive-expression:
1875 multiplicative-expression
1876 additive-expression + multiplicative-expression
1877 additive-expression - multiplicative-expression */
1879 static void
1880 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1882 enum tree_code code = TREE_CODE (e);
1883 switch (code)
1885 case POINTER_PLUS_EXPR:
1886 case PLUS_EXPR:
1887 case MINUS_EXPR:
1888 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1889 pp_c_whitespace (pp);
1890 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1891 pp_plus (pp);
1892 else
1893 pp_minus (pp);
1894 pp_c_whitespace (pp);
1895 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1896 break;
1898 default:
1899 pp->multiplicative_expression (e);
1900 break;
1904 /* additive-expression:
1905 additive-expression
1906 shift-expression << additive-expression
1907 shift-expression >> additive-expression */
1909 static void
1910 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1912 enum tree_code code = TREE_CODE (e);
1913 switch (code)
1915 case LSHIFT_EXPR:
1916 case RSHIFT_EXPR:
1917 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1918 pp_c_whitespace (pp);
1919 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1920 pp_c_whitespace (pp);
1921 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1922 break;
1924 default:
1925 pp_c_additive_expression (pp, e);
1929 /* relational-expression:
1930 shift-expression
1931 relational-expression < shift-expression
1932 relational-expression > shift-expression
1933 relational-expression <= shift-expression
1934 relational-expression >= shift-expression */
1936 static void
1937 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1939 enum tree_code code = TREE_CODE (e);
1940 switch (code)
1942 case LT_EXPR:
1943 case GT_EXPR:
1944 case LE_EXPR:
1945 case GE_EXPR:
1946 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1947 pp_c_whitespace (pp);
1948 if (code == LT_EXPR)
1949 pp_less (pp);
1950 else if (code == GT_EXPR)
1951 pp_greater (pp);
1952 else if (code == LE_EXPR)
1953 pp_less_equal (pp);
1954 else if (code == GE_EXPR)
1955 pp_greater_equal (pp);
1956 pp_c_whitespace (pp);
1957 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1958 break;
1960 default:
1961 pp_c_shift_expression (pp, e);
1962 break;
1966 /* equality-expression:
1967 relational-expression
1968 equality-expression == relational-expression
1969 equality-equality != relational-expression */
1971 static void
1972 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1974 enum tree_code code = TREE_CODE (e);
1975 switch (code)
1977 case EQ_EXPR:
1978 case NE_EXPR:
1979 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1980 pp_c_whitespace (pp);
1981 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1982 pp_c_whitespace (pp);
1983 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1984 break;
1986 default:
1987 pp_c_relational_expression (pp, e);
1988 break;
1992 /* AND-expression:
1993 equality-expression
1994 AND-expression & equality-equality */
1996 static void
1997 pp_c_and_expression (c_pretty_printer *pp, tree e)
1999 if (TREE_CODE (e) == BIT_AND_EXPR)
2001 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2002 pp_c_whitespace (pp);
2003 pp_ampersand (pp);
2004 pp_c_whitespace (pp);
2005 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2007 else
2008 pp_c_equality_expression (pp, e);
2011 /* exclusive-OR-expression:
2012 AND-expression
2013 exclusive-OR-expression ^ AND-expression */
2015 static void
2016 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2018 if (TREE_CODE (e) == BIT_XOR_EXPR
2019 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2021 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2022 if (TREE_CODE (e) == BIT_XOR_EXPR)
2023 pp_c_maybe_whitespace (pp);
2024 else
2025 pp_c_whitespace (pp);
2026 pp_carret (pp);
2027 pp_c_whitespace (pp);
2028 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2030 else
2031 pp_c_and_expression (pp, e);
2034 /* inclusive-OR-expression:
2035 exclusive-OR-expression
2036 inclusive-OR-expression | exclusive-OR-expression */
2038 static void
2039 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2041 if (TREE_CODE (e) == BIT_IOR_EXPR)
2043 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2044 pp_c_whitespace (pp);
2045 pp_bar (pp);
2046 pp_c_whitespace (pp);
2047 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2049 else
2050 pp_c_exclusive_or_expression (pp, e);
2053 /* logical-AND-expression:
2054 inclusive-OR-expression
2055 logical-AND-expression && inclusive-OR-expression */
2057 static void
2058 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2060 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2061 || TREE_CODE (e) == TRUTH_AND_EXPR)
2063 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2064 pp_c_whitespace (pp);
2065 pp_ampersand_ampersand (pp);
2066 pp_c_whitespace (pp);
2067 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2069 else
2070 pp_c_inclusive_or_expression (pp, e);
2073 /* logical-OR-expression:
2074 logical-AND-expression
2075 logical-OR-expression || logical-AND-expression */
2077 void
2078 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2080 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2081 || TREE_CODE (e) == TRUTH_OR_EXPR)
2083 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2084 pp_c_whitespace (pp);
2085 pp_bar_bar (pp);
2086 pp_c_whitespace (pp);
2087 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2089 else
2090 pp_c_logical_and_expression (pp, e);
2093 /* conditional-expression:
2094 logical-OR-expression
2095 logical-OR-expression ? expression : conditional-expression */
2097 void
2098 c_pretty_printer::conditional_expression (tree e)
2100 if (TREE_CODE (e) == COND_EXPR)
2102 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2103 pp_c_whitespace (this);
2104 pp_question (this);
2105 pp_c_whitespace (this);
2106 expression (TREE_OPERAND (e, 1));
2107 pp_c_whitespace (this);
2108 pp_colon (this);
2109 pp_c_whitespace (this);
2110 conditional_expression (TREE_OPERAND (e, 2));
2112 else
2113 pp_c_logical_or_expression (this, e);
2117 /* assignment-expression:
2118 conditional-expression
2119 unary-expression assignment-operator assignment-expression
2121 assignment-expression: one of
2122 = *= /= %= += -= >>= <<= &= ^= |= */
2124 void
2125 c_pretty_printer::assignment_expression (tree e)
2127 if (TREE_CODE (e) == MODIFY_EXPR
2128 || TREE_CODE (e) == INIT_EXPR)
2130 unary_expression (TREE_OPERAND (e, 0));
2131 pp_c_whitespace (this);
2132 pp_equal (this);
2133 pp_space (this);
2134 expression (TREE_OPERAND (e, 1));
2136 else
2137 conditional_expression (e);
2140 /* expression:
2141 assignment-expression
2142 expression , assignment-expression
2144 Implementation note: instead of going through the usual recursion
2145 chain, I take the liberty of dispatching nodes to the appropriate
2146 functions. This makes some redundancy, but it worths it. That also
2147 prevents a possible infinite recursion between primary_expression ()
2148 and expression (). */
2150 void
2151 c_pretty_printer::expression (tree e)
2153 switch (TREE_CODE (e))
2155 case VOID_CST:
2156 pp_c_void_constant (this);
2157 break;
2159 case INTEGER_CST:
2160 pp_c_integer_constant (this, e);
2161 break;
2163 case REAL_CST:
2164 pp_c_floating_constant (this, e);
2165 break;
2167 case FIXED_CST:
2168 pp_c_fixed_constant (this, e);
2169 break;
2171 case STRING_CST:
2172 pp_c_string_literal (this, e);
2173 break;
2175 case IDENTIFIER_NODE:
2176 case FUNCTION_DECL:
2177 case VAR_DECL:
2178 case CONST_DECL:
2179 case PARM_DECL:
2180 case RESULT_DECL:
2181 case FIELD_DECL:
2182 case LABEL_DECL:
2183 case ERROR_MARK:
2184 primary_expression (e);
2185 break;
2187 case SSA_NAME:
2188 if (SSA_NAME_VAR (e)
2189 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2190 expression (SSA_NAME_VAR (e));
2191 else
2192 translate_string ("<unknown>");
2193 break;
2195 case POSTINCREMENT_EXPR:
2196 case POSTDECREMENT_EXPR:
2197 case ARRAY_REF:
2198 case ARRAY_NOTATION_REF:
2199 case CALL_EXPR:
2200 case COMPONENT_REF:
2201 case BIT_FIELD_REF:
2202 case COMPLEX_CST:
2203 case COMPLEX_EXPR:
2204 case VECTOR_CST:
2205 case ORDERED_EXPR:
2206 case UNORDERED_EXPR:
2207 case LTGT_EXPR:
2208 case UNEQ_EXPR:
2209 case UNLE_EXPR:
2210 case UNLT_EXPR:
2211 case UNGE_EXPR:
2212 case UNGT_EXPR:
2213 case ABS_EXPR:
2214 case CONSTRUCTOR:
2215 case COMPOUND_LITERAL_EXPR:
2216 case VA_ARG_EXPR:
2217 postfix_expression (e);
2218 break;
2220 case CONJ_EXPR:
2221 case ADDR_EXPR:
2222 case INDIRECT_REF:
2223 case MEM_REF:
2224 case NEGATE_EXPR:
2225 case BIT_NOT_EXPR:
2226 case TRUTH_NOT_EXPR:
2227 case PREINCREMENT_EXPR:
2228 case PREDECREMENT_EXPR:
2229 case REALPART_EXPR:
2230 case IMAGPART_EXPR:
2231 unary_expression (e);
2232 break;
2234 case FLOAT_EXPR:
2235 case FIX_TRUNC_EXPR:
2236 CASE_CONVERT:
2237 case VIEW_CONVERT_EXPR:
2238 pp_c_cast_expression (this, e);
2239 break;
2241 case MULT_EXPR:
2242 case TRUNC_MOD_EXPR:
2243 case TRUNC_DIV_EXPR:
2244 multiplicative_expression (e);
2245 break;
2247 case LSHIFT_EXPR:
2248 case RSHIFT_EXPR:
2249 pp_c_shift_expression (this, e);
2250 break;
2252 case LT_EXPR:
2253 case GT_EXPR:
2254 case LE_EXPR:
2255 case GE_EXPR:
2256 pp_c_relational_expression (this, e);
2257 break;
2259 case BIT_AND_EXPR:
2260 pp_c_and_expression (this, e);
2261 break;
2263 case BIT_XOR_EXPR:
2264 case TRUTH_XOR_EXPR:
2265 pp_c_exclusive_or_expression (this, e);
2266 break;
2268 case BIT_IOR_EXPR:
2269 pp_c_inclusive_or_expression (this, e);
2270 break;
2272 case TRUTH_ANDIF_EXPR:
2273 case TRUTH_AND_EXPR:
2274 pp_c_logical_and_expression (this, e);
2275 break;
2277 case TRUTH_ORIF_EXPR:
2278 case TRUTH_OR_EXPR:
2279 pp_c_logical_or_expression (this, e);
2280 break;
2282 case EQ_EXPR:
2283 case NE_EXPR:
2284 pp_c_equality_expression (this, e);
2285 break;
2287 case COND_EXPR:
2288 conditional_expression (e);
2289 break;
2291 case POINTER_PLUS_EXPR:
2292 case PLUS_EXPR:
2293 case MINUS_EXPR:
2294 pp_c_additive_expression (this, e);
2295 break;
2297 case MODIFY_EXPR:
2298 case INIT_EXPR:
2299 assignment_expression (e);
2300 break;
2302 case COMPOUND_EXPR:
2303 pp_c_left_paren (this);
2304 expression (TREE_OPERAND (e, 0));
2305 pp_separate_with (this, ',');
2306 assignment_expression (TREE_OPERAND (e, 1));
2307 pp_c_right_paren (this);
2308 break;
2310 case NON_LVALUE_EXPR:
2311 case SAVE_EXPR:
2312 expression (TREE_OPERAND (e, 0));
2313 break;
2315 case TARGET_EXPR:
2316 postfix_expression (TREE_OPERAND (e, 1));
2317 break;
2319 case BIND_EXPR:
2320 case GOTO_EXPR:
2321 /* We don't yet have a way of dumping statements in a
2322 human-readable format. */
2323 pp_string (this, "({...})");
2324 break;
2326 case C_MAYBE_CONST_EXPR:
2327 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2328 break;
2330 default:
2331 pp_unsupported_tree (this, e);
2332 break;
2338 /* Statements. */
2340 void
2341 c_pretty_printer::statement (tree stmt)
2343 if (stmt == NULL)
2344 return;
2346 if (pp_needs_newline (this))
2347 pp_newline_and_indent (this, 0);
2349 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2353 /* Initialize the PRETTY-PRINTER for handling C codes. */
2355 c_pretty_printer::c_pretty_printer ()
2356 : pretty_printer (),
2357 offset_list (),
2358 flags ()
2360 type_specifier_seq = pp_c_specifier_qualifier_list;
2361 ptr_operator = pp_c_pointer;
2362 parameter_list = pp_c_parameter_type_list;
2366 /* Print the tree T in full, on file FILE. */
2368 void
2369 print_c_tree (FILE *file, tree t)
2371 c_pretty_printer pp;
2373 pp_needs_newline (&pp) = true;
2374 pp.buffer->stream = file;
2375 pp.statement (t);
2376 pp_newline_and_flush (&pp);
2379 /* Print the tree T in full, on stderr. */
2381 DEBUG_FUNCTION void
2382 debug_c_tree (tree t)
2384 print_c_tree (stderr, t);
2385 fputc ('\n', stderr);
2388 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2389 up of T's memory address. */
2391 void
2392 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2394 const char *name;
2396 gcc_assert (DECL_P (t));
2398 if (DECL_NAME (t))
2399 name = IDENTIFIER_POINTER (DECL_NAME (t));
2400 else
2402 static char xname[8];
2403 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2404 name = xname;
2407 pp_c_identifier (pp, name);