2017-05-11 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blobfdb7b41f592e1ea068f3950a92fa4be8e03c9ed5
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 "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 tree common_t;
348 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
349 common_t = c_common_type_for_mode (TYPE_MODE (t),
350 TYPE_SATURATING (t));
351 else
352 common_t = c_common_type_for_mode (TYPE_MODE (t),
353 TYPE_UNSIGNED (t));
354 if (common_t && TYPE_NAME (common_t))
356 simple_type_specifier (common_t);
357 if (TYPE_PRECISION (common_t) != prec)
359 pp_colon (this);
360 pp_decimal_int (this, prec);
363 else
365 switch (code)
367 case INTEGER_TYPE:
368 translate_string (TYPE_UNSIGNED (t)
369 ? "<unnamed-unsigned:"
370 : "<unnamed-signed:");
371 break;
372 case REAL_TYPE:
373 translate_string ("<unnamed-float:");
374 break;
375 case FIXED_POINT_TYPE:
376 translate_string ("<unnamed-fixed:");
377 break;
378 default:
379 gcc_unreachable ();
381 pp_decimal_int (this, prec);
382 pp_greater (this);
385 break;
387 case TYPE_DECL:
388 if (DECL_NAME (t))
389 id_expression (t);
390 else
391 translate_string ("<typedef-error>");
392 break;
394 case UNION_TYPE:
395 case RECORD_TYPE:
396 case ENUMERAL_TYPE:
397 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
398 /* Don't decorate the type if this is a typedef name. */;
399 else if (code == UNION_TYPE)
400 pp_c_ws_string (this, "union");
401 else if (code == RECORD_TYPE)
402 pp_c_ws_string (this, "struct");
403 else if (code == ENUMERAL_TYPE)
404 pp_c_ws_string (this, "enum");
405 else
406 translate_string ("<tag-error>");
408 if (TYPE_NAME (t))
409 id_expression (TYPE_NAME (t));
410 else
411 translate_string ("<anonymous>");
412 break;
414 default:
415 pp_unsupported_tree (this, t);
416 break;
420 /* specifier-qualifier-list:
421 type-specifier specifier-qualifier-list-opt
422 type-qualifier specifier-qualifier-list-opt
425 Implementation note: Because of the non-linearities in array or
426 function declarations, this routine prints not just the
427 specifier-qualifier-list of such entities or types of such entities,
428 but also the 'pointer' production part of their declarators. The
429 remaining part is done by declarator() or abstract_declarator(). */
431 void
432 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
434 const enum tree_code code = TREE_CODE (t);
436 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
437 pp_c_type_qualifier_list (pp, t);
438 switch (code)
440 case REFERENCE_TYPE:
441 case POINTER_TYPE:
443 /* Get the types-specifier of this type. */
444 tree pointee = strip_pointer_operator (TREE_TYPE (t));
445 pp_c_specifier_qualifier_list (pp, pointee);
446 if (TREE_CODE (pointee) == ARRAY_TYPE
447 || TREE_CODE (pointee) == FUNCTION_TYPE)
449 pp_c_whitespace (pp);
450 pp_c_left_paren (pp);
451 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
453 else if (!c_dialect_cxx ())
454 pp_c_whitespace (pp);
455 pp_ptr_operator (pp, t);
457 break;
459 case FUNCTION_TYPE:
460 case ARRAY_TYPE:
461 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
462 break;
464 case VECTOR_TYPE:
465 case COMPLEX_TYPE:
466 if (code == COMPLEX_TYPE)
467 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
468 ? "_Complex" : "__complex__"));
469 else if (code == VECTOR_TYPE)
471 pp_c_ws_string (pp, "__vector");
472 pp_c_left_paren (pp);
473 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
474 pp_c_right_paren (pp);
475 pp_c_whitespace (pp);
477 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
478 break;
480 default:
481 pp->simple_type_specifier (t);
482 break;
484 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
485 pp_c_type_qualifier_list (pp, t);
488 /* parameter-type-list:
489 parameter-list
490 parameter-list , ...
492 parameter-list:
493 parameter-declaration
494 parameter-list , parameter-declaration
496 parameter-declaration:
497 declaration-specifiers declarator
498 declaration-specifiers abstract-declarator(opt) */
500 void
501 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
503 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
504 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
505 pp_c_left_paren (pp);
506 if (parms == void_list_node)
507 pp_c_ws_string (pp, "void");
508 else
510 bool first = true;
511 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
513 if (!first)
514 pp_separate_with (pp, ',');
515 first = false;
516 pp->declaration_specifiers
517 (want_parm_decl ? parms : TREE_VALUE (parms));
518 if (want_parm_decl)
519 pp->declarator (parms);
520 else
521 pp->abstract_declarator (TREE_VALUE (parms));
524 pp_c_right_paren (pp);
527 /* abstract-declarator:
528 pointer
529 pointer(opt) direct-abstract-declarator */
531 void
532 c_pretty_printer::abstract_declarator (tree t)
534 if (TREE_CODE (t) == POINTER_TYPE)
536 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
537 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
538 pp_c_right_paren (this);
539 t = TREE_TYPE (t);
542 direct_abstract_declarator (t);
545 /* direct-abstract-declarator:
546 ( abstract-declarator )
547 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
548 direct-abstract-declarator(opt) [ * ]
549 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
551 void
552 c_pretty_printer::direct_abstract_declarator (tree t)
554 switch (TREE_CODE (t))
556 case POINTER_TYPE:
557 abstract_declarator (t);
558 break;
560 case FUNCTION_TYPE:
561 pp_c_parameter_type_list (this, t);
562 direct_abstract_declarator (TREE_TYPE (t));
563 break;
565 case ARRAY_TYPE:
566 pp_c_left_bracket (this);
567 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
569 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
570 tree type = TREE_TYPE (maxval);
572 if (tree_fits_shwi_p (maxval))
573 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
574 else
575 expression (fold_build2 (PLUS_EXPR, type, maxval,
576 build_int_cst (type, 1)));
578 pp_c_right_bracket (this);
579 direct_abstract_declarator (TREE_TYPE (t));
580 break;
582 case IDENTIFIER_NODE:
583 case VOID_TYPE:
584 case BOOLEAN_TYPE:
585 case INTEGER_TYPE:
586 case REAL_TYPE:
587 case FIXED_POINT_TYPE:
588 case ENUMERAL_TYPE:
589 case RECORD_TYPE:
590 case UNION_TYPE:
591 case VECTOR_TYPE:
592 case COMPLEX_TYPE:
593 case TYPE_DECL:
594 break;
596 default:
597 pp_unsupported_tree (this, t);
598 break;
602 /* type-name:
603 specifier-qualifier-list abstract-declarator(opt) */
605 void
606 c_pretty_printer::type_id (tree t)
608 pp_c_specifier_qualifier_list (this, t);
609 abstract_declarator (t);
612 /* storage-class-specifier:
613 typedef
614 extern
615 static
616 auto
617 register */
619 void
620 c_pretty_printer::storage_class_specifier (tree t)
622 if (TREE_CODE (t) == TYPE_DECL)
623 pp_c_ws_string (this, "typedef");
624 else if (DECL_P (t))
626 if (DECL_REGISTER (t))
627 pp_c_ws_string (this, "register");
628 else if (TREE_STATIC (t) && VAR_P (t))
629 pp_c_ws_string (this, "static");
633 /* function-specifier:
634 inline */
636 void
637 c_pretty_printer::function_specifier (tree t)
639 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
640 pp_c_ws_string (this, "inline");
643 /* declaration-specifiers:
644 storage-class-specifier declaration-specifiers(opt)
645 type-specifier declaration-specifiers(opt)
646 type-qualifier declaration-specifiers(opt)
647 function-specifier declaration-specifiers(opt) */
649 void
650 c_pretty_printer::declaration_specifiers (tree t)
652 storage_class_specifier (t);
653 function_specifier (t);
654 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
657 /* direct-declarator
658 identifier
659 ( declarator )
660 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
661 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
662 direct-declarator [ type-qualifier-list static assignment-expression ]
663 direct-declarator [ type-qualifier-list * ]
664 direct-declarator ( parameter-type-list )
665 direct-declarator ( identifier-list(opt) ) */
667 void
668 c_pretty_printer::direct_declarator (tree t)
670 switch (TREE_CODE (t))
672 case VAR_DECL:
673 case PARM_DECL:
674 case TYPE_DECL:
675 case FIELD_DECL:
676 case LABEL_DECL:
677 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
678 pp_c_tree_decl_identifier (this, t);
679 break;
681 case ARRAY_TYPE:
682 case POINTER_TYPE:
683 abstract_declarator (TREE_TYPE (t));
684 break;
686 case FUNCTION_TYPE:
687 pp_parameter_list (this, t);
688 abstract_declarator (TREE_TYPE (t));
689 break;
691 case FUNCTION_DECL:
692 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
693 pp_c_tree_decl_identifier (this, t);
694 if (flags & pp_c_flag_abstract)
695 abstract_declarator (TREE_TYPE (t));
696 else
698 pp_parameter_list (this, t);
699 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
701 break;
703 case INTEGER_TYPE:
704 case REAL_TYPE:
705 case FIXED_POINT_TYPE:
706 case ENUMERAL_TYPE:
707 case UNION_TYPE:
708 case RECORD_TYPE:
709 break;
711 default:
712 pp_unsupported_tree (this, t);
713 break;
718 /* declarator:
719 pointer(opt) direct-declarator */
721 void
722 c_pretty_printer::declarator (tree t)
724 switch (TREE_CODE (t))
726 case INTEGER_TYPE:
727 case REAL_TYPE:
728 case FIXED_POINT_TYPE:
729 case ENUMERAL_TYPE:
730 case UNION_TYPE:
731 case RECORD_TYPE:
732 break;
734 case VAR_DECL:
735 case PARM_DECL:
736 case FIELD_DECL:
737 case ARRAY_TYPE:
738 case FUNCTION_TYPE:
739 case FUNCTION_DECL:
740 case TYPE_DECL:
741 direct_declarator (t);
742 break;
745 default:
746 pp_unsupported_tree (this, t);
747 break;
751 /* declaration:
752 declaration-specifiers init-declarator-list(opt) ; */
754 void
755 c_pretty_printer::declaration (tree t)
757 declaration_specifiers (t);
758 pp_c_init_declarator (this, t);
761 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
763 void
764 pp_c_attributes (c_pretty_printer *pp, tree attributes)
766 if (attributes == NULL_TREE)
767 return;
769 pp_c_ws_string (pp, "__attribute__");
770 pp_c_left_paren (pp);
771 pp_c_left_paren (pp);
772 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
774 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
775 if (TREE_VALUE (attributes))
776 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
778 if (TREE_CHAIN (attributes))
779 pp_separate_with (pp, ',');
781 pp_c_right_paren (pp);
782 pp_c_right_paren (pp);
785 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
786 marked to be displayed on disgnostic. */
788 void
789 pp_c_attributes_display (c_pretty_printer *pp, tree a)
791 bool is_first = true;
793 if (a == NULL_TREE)
794 return;
796 for (; a != NULL_TREE; a = TREE_CHAIN (a))
798 const struct attribute_spec *as;
799 as = lookup_attribute_spec (TREE_PURPOSE (a));
800 if (!as || as->affects_type_identity == false)
801 continue;
802 if (c_dialect_cxx ()
803 && !strcmp ("transaction_safe", as->name))
804 /* In C++ transaction_safe is printed at the end of the declarator. */
805 continue;
806 if (is_first)
808 pp_c_ws_string (pp, "__attribute__");
809 pp_c_left_paren (pp);
810 pp_c_left_paren (pp);
811 is_first = false;
813 else
815 pp_separate_with (pp, ',');
817 pp_tree_identifier (pp, TREE_PURPOSE (a));
818 if (TREE_VALUE (a))
819 pp_c_call_argument_list (pp, TREE_VALUE (a));
822 if (!is_first)
824 pp_c_right_paren (pp);
825 pp_c_right_paren (pp);
826 pp_c_whitespace (pp);
830 /* function-definition:
831 declaration-specifiers declarator compound-statement */
833 void
834 pp_c_function_definition (c_pretty_printer *pp, tree t)
836 pp->declaration_specifiers (t);
837 pp->declarator (t);
838 pp_needs_newline (pp) = true;
839 pp->statement (DECL_SAVED_TREE (t));
840 pp_newline_and_flush (pp);
844 /* Expressions. */
846 /* Print out a c-char. This is called solely for characters which are
847 in the *target* execution character set. We ought to convert them
848 back to the *host* execution character set before printing, but we
849 have no way to do this at present. A decent compromise is to print
850 all characters as if they were in the host execution character set,
851 and not attempt to recover any named escape characters, but render
852 all unprintables as octal escapes. If the host and target character
853 sets are the same, this produces relatively readable output. If they
854 are not the same, strings may appear as gibberish, but that's okay
855 (in fact, it may well be what the reader wants, e.g. if they are looking
856 to see if conversion to the target character set happened correctly).
858 A special case: we need to prefix \, ", and ' with backslashes. It is
859 correct to do so for the *host*'s \, ", and ', because the rest of the
860 file appears in the host character set. */
862 static void
863 pp_c_char (c_pretty_printer *pp, int c)
865 if (ISPRINT (c))
867 switch (c)
869 case '\\': pp_string (pp, "\\\\"); break;
870 case '\'': pp_string (pp, "\\\'"); break;
871 case '\"': pp_string (pp, "\\\""); break;
872 default: pp_character (pp, c);
875 else
876 pp_scalar (pp, "\\%03o", (unsigned) c);
879 /* Print out a STRING literal. */
881 void
882 pp_c_string_literal (c_pretty_printer *pp, tree s)
884 const char *p = TREE_STRING_POINTER (s);
885 int n = TREE_STRING_LENGTH (s) - 1;
886 int i;
887 pp_doublequote (pp);
888 for (i = 0; i < n; ++i)
889 pp_c_char (pp, p[i]);
890 pp_doublequote (pp);
893 /* Pretty-print a VOID_CST (void_node). */
895 static void
896 pp_c_void_constant (c_pretty_printer *pp)
898 pp_c_type_cast (pp, void_type_node);
899 pp_string (pp, "0");
902 /* Pretty-print an INTEGER literal. */
904 static void
905 pp_c_integer_constant (c_pretty_printer *pp, tree i)
907 if (tree_fits_shwi_p (i))
908 pp_wide_integer (pp, tree_to_shwi (i));
909 else if (tree_fits_uhwi_p (i))
910 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
911 else
913 wide_int wi = i;
915 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
917 pp_minus (pp);
918 wi = -wi;
920 print_hex (wi, pp_buffer (pp)->digit_buffer);
921 pp_string (pp, pp_buffer (pp)->digit_buffer);
925 /* Print out a CHARACTER literal. */
927 static void
928 pp_c_character_constant (c_pretty_printer *pp, tree c)
930 pp_quote (pp);
931 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
932 pp_quote (pp);
935 /* Print out a BOOLEAN literal. */
937 static void
938 pp_c_bool_constant (c_pretty_printer *pp, tree b)
940 if (b == boolean_false_node)
942 if (c_dialect_cxx ())
943 pp_c_ws_string (pp, "false");
944 else if (flag_isoc99)
945 pp_c_ws_string (pp, "_False");
946 else
947 pp_unsupported_tree (pp, b);
949 else if (b == boolean_true_node)
951 if (c_dialect_cxx ())
952 pp_c_ws_string (pp, "true");
953 else if (flag_isoc99)
954 pp_c_ws_string (pp, "_True");
955 else
956 pp_unsupported_tree (pp, b);
958 else if (TREE_CODE (b) == INTEGER_CST)
959 pp_c_integer_constant (pp, b);
960 else
961 pp_unsupported_tree (pp, b);
964 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
965 false; that means the value was obtained by a cast, in which case
966 print out the type-id part of the cast-expression -- the casted value
967 is then printed by pp_c_integer_literal. */
969 static bool
970 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
972 bool value_is_named = true;
973 tree type = TREE_TYPE (e);
974 tree value;
976 /* Find the name of this constant. */
977 for (value = TYPE_VALUES (type);
978 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
979 value = TREE_CHAIN (value))
982 if (value != NULL_TREE)
983 pp->id_expression (TREE_PURPOSE (value));
984 else
986 /* Value must have been cast. */
987 pp_c_type_cast (pp, type);
988 value_is_named = false;
991 return value_is_named;
994 /* Print out a REAL value as a decimal-floating-constant. */
996 static void
997 pp_c_floating_constant (c_pretty_printer *pp, tree r)
999 const struct real_format *fmt
1000 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1002 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1003 bool is_decimal = floating_cst.decimal;
1005 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1006 log10(2) to 7 significant digits. */
1007 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1009 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1010 sizeof (pp_buffer (pp)->digit_buffer),
1011 max_digits10, 1);
1013 pp_string (pp, pp_buffer(pp)->digit_buffer);
1014 if (TREE_TYPE (r) == float_type_node)
1015 pp_character (pp, 'f');
1016 else if (TREE_TYPE (r) == long_double_type_node)
1017 pp_character (pp, 'l');
1018 else if (TREE_TYPE (r) == dfloat128_type_node)
1019 pp_string (pp, "dl");
1020 else if (TREE_TYPE (r) == dfloat64_type_node)
1021 pp_string (pp, "dd");
1022 else if (TREE_TYPE (r) == dfloat32_type_node)
1023 pp_string (pp, "df");
1024 else if (TREE_TYPE (r) != double_type_node)
1025 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1026 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i))
1028 pp_character (pp, 'f');
1029 pp_decimal_int (pp, floatn_nx_types[i].n);
1030 if (floatn_nx_types[i].extended)
1031 pp_character (pp, 'x');
1032 break;
1036 /* Print out a FIXED value as a decimal-floating-constant. */
1038 static void
1039 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1041 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1042 sizeof (pp_buffer (pp)->digit_buffer));
1043 pp_string (pp, pp_buffer(pp)->digit_buffer);
1046 /* Pretty-print a compound literal expression. GNU extensions include
1047 vector constants. */
1049 static void
1050 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1052 tree type = TREE_TYPE (e);
1053 pp_c_type_cast (pp, type);
1055 switch (TREE_CODE (type))
1057 case RECORD_TYPE:
1058 case UNION_TYPE:
1059 case ARRAY_TYPE:
1060 case VECTOR_TYPE:
1061 case COMPLEX_TYPE:
1062 pp_c_brace_enclosed_initializer_list (pp, e);
1063 break;
1065 default:
1066 pp_unsupported_tree (pp, e);
1067 break;
1071 /* Pretty-print a COMPLEX_EXPR expression. */
1073 static void
1074 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1076 /* Handle a few common special cases, otherwise fallback
1077 to printing it as compound literal. */
1078 tree type = TREE_TYPE (e);
1079 tree realexpr = TREE_OPERAND (e, 0);
1080 tree imagexpr = TREE_OPERAND (e, 1);
1082 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1083 if (TREE_CODE (realexpr) == NOP_EXPR
1084 && TREE_CODE (imagexpr) == NOP_EXPR
1085 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1086 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1087 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1088 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1089 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1090 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1092 pp_c_type_cast (pp, type);
1093 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1094 return;
1097 /* Cast of an scalar expression to COMPLEX_TYPE. */
1098 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1099 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1101 pp_c_type_cast (pp, type);
1102 if (TREE_CODE (realexpr) == NOP_EXPR)
1103 realexpr = TREE_OPERAND (realexpr, 0);
1104 pp->expression (realexpr);
1105 return;
1108 pp_c_compound_literal (pp, e);
1111 /* constant:
1112 integer-constant
1113 floating-constant
1114 fixed-point-constant
1115 enumeration-constant
1116 character-constant */
1118 void
1119 c_pretty_printer::constant (tree e)
1121 const enum tree_code code = TREE_CODE (e);
1123 switch (code)
1125 case VOID_CST:
1126 pp_c_void_constant (this);
1127 break;
1129 case INTEGER_CST:
1131 tree type = TREE_TYPE (e);
1132 if (type == boolean_type_node)
1133 pp_c_bool_constant (this, e);
1134 else if (type == char_type_node)
1135 pp_c_character_constant (this, e);
1136 else if (TREE_CODE (type) == ENUMERAL_TYPE
1137 && pp_c_enumeration_constant (this, e))
1139 else
1140 pp_c_integer_constant (this, e);
1142 break;
1144 case REAL_CST:
1145 pp_c_floating_constant (this, e);
1146 break;
1148 case FIXED_CST:
1149 pp_c_fixed_constant (this, e);
1150 break;
1152 case STRING_CST:
1153 pp_c_string_literal (this, e);
1154 break;
1156 case COMPLEX_CST:
1157 /* Sometimes, we are confused and we think a complex literal
1158 is a constant. Such thing is a compound literal which
1159 grammatically belongs to postfix-expr production. */
1160 pp_c_compound_literal (this, e);
1161 break;
1163 default:
1164 pp_unsupported_tree (this, e);
1165 break;
1169 /* Pretty-print a string such as an identifier, without changing its
1170 encoding, preceded by whitespace is necessary. */
1172 void
1173 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1175 pp_c_maybe_whitespace (pp);
1176 pp_string (pp, str);
1177 pp->padding = pp_before;
1180 void
1181 c_pretty_printer::translate_string (const char *gmsgid)
1183 if (pp_translate_identifiers (this))
1184 pp_c_ws_string (this, _(gmsgid));
1185 else
1186 pp_c_ws_string (this, gmsgid);
1189 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1190 that need converting to the locale encoding, preceded by whitespace
1191 is necessary. */
1193 void
1194 pp_c_identifier (c_pretty_printer *pp, const char *id)
1196 pp_c_maybe_whitespace (pp);
1197 pp_identifier (pp, id);
1198 pp->padding = pp_before;
1201 /* Pretty-print a C primary-expression.
1202 primary-expression:
1203 identifier
1204 constant
1205 string-literal
1206 ( expression ) */
1208 void
1209 c_pretty_printer::primary_expression (tree e)
1211 switch (TREE_CODE (e))
1213 case VAR_DECL:
1214 case PARM_DECL:
1215 case FIELD_DECL:
1216 case CONST_DECL:
1217 case FUNCTION_DECL:
1218 case LABEL_DECL:
1219 pp_c_tree_decl_identifier (this, e);
1220 break;
1222 case IDENTIFIER_NODE:
1223 pp_c_tree_identifier (this, e);
1224 break;
1226 case ERROR_MARK:
1227 translate_string ("<erroneous-expression>");
1228 break;
1230 case RESULT_DECL:
1231 translate_string ("<return-value>");
1232 break;
1234 case VOID_CST:
1235 case INTEGER_CST:
1236 case REAL_CST:
1237 case FIXED_CST:
1238 case STRING_CST:
1239 constant (e);
1240 break;
1242 case TARGET_EXPR:
1243 pp_c_ws_string (this, "__builtin_memcpy");
1244 pp_c_left_paren (this);
1245 pp_ampersand (this);
1246 primary_expression (TREE_OPERAND (e, 0));
1247 pp_separate_with (this, ',');
1248 pp_ampersand (this);
1249 initializer (TREE_OPERAND (e, 1));
1250 if (TREE_OPERAND (e, 2))
1252 pp_separate_with (this, ',');
1253 expression (TREE_OPERAND (e, 2));
1255 pp_c_right_paren (this);
1256 break;
1258 default:
1259 /* FIXME: Make sure we won't get into an infinite loop. */
1260 pp_c_left_paren (this);
1261 expression (e);
1262 pp_c_right_paren (this);
1263 break;
1267 /* Print out a C initializer -- also support C compound-literals.
1268 initializer:
1269 assignment-expression:
1270 { initializer-list }
1271 { initializer-list , } */
1273 void
1274 c_pretty_printer::initializer (tree e)
1276 if (TREE_CODE (e) == CONSTRUCTOR)
1277 pp_c_brace_enclosed_initializer_list (this, e);
1278 else
1279 expression (e);
1282 /* init-declarator:
1283 declarator:
1284 declarator = initializer */
1286 void
1287 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1289 pp->declarator (t);
1290 /* We don't want to output function definitions here. There are handled
1291 elsewhere (and the syntactic form is bogus anyway). */
1292 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1294 tree init = DECL_INITIAL (t);
1295 /* This C++ bit is handled here because it is easier to do so.
1296 In templates, the C++ parser builds a TREE_LIST for a
1297 direct-initialization; the TREE_PURPOSE is the variable to
1298 initialize and the TREE_VALUE is the initializer. */
1299 if (TREE_CODE (init) == TREE_LIST)
1301 pp_c_left_paren (pp);
1302 pp->expression (TREE_VALUE (init));
1303 pp_right_paren (pp);
1305 else
1307 pp_space (pp);
1308 pp_equal (pp);
1309 pp_space (pp);
1310 pp->initializer (init);
1315 /* initializer-list:
1316 designation(opt) initializer
1317 initializer-list , designation(opt) initializer
1319 designation:
1320 designator-list =
1322 designator-list:
1323 designator
1324 designator-list designator
1326 designator:
1327 [ constant-expression ]
1328 identifier */
1330 static void
1331 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1333 tree type = TREE_TYPE (e);
1334 const enum tree_code code = TREE_CODE (type);
1336 if (TREE_CODE (e) == CONSTRUCTOR)
1338 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1339 return;
1342 switch (code)
1344 case RECORD_TYPE:
1345 case UNION_TYPE:
1346 case ARRAY_TYPE:
1348 tree init = TREE_OPERAND (e, 0);
1349 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1351 if (code == RECORD_TYPE || code == UNION_TYPE)
1353 pp_c_dot (pp);
1354 pp->primary_expression (TREE_PURPOSE (init));
1356 else
1358 pp_c_left_bracket (pp);
1359 if (TREE_PURPOSE (init))
1360 pp->constant (TREE_PURPOSE (init));
1361 pp_c_right_bracket (pp);
1363 pp_c_whitespace (pp);
1364 pp_equal (pp);
1365 pp_c_whitespace (pp);
1366 pp->initializer (TREE_VALUE (init));
1367 if (TREE_CHAIN (init))
1368 pp_separate_with (pp, ',');
1371 return;
1373 case VECTOR_TYPE:
1374 if (TREE_CODE (e) == VECTOR_CST)
1376 unsigned i;
1377 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1379 if (i > 0)
1380 pp_separate_with (pp, ',');
1381 pp->expression (VECTOR_CST_ELT (e, i));
1384 else
1385 break;
1386 return;
1388 case COMPLEX_TYPE:
1389 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1391 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1392 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1393 pp_separate_with (pp, ',');
1394 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1396 else
1397 break;
1398 return;
1400 default:
1401 break;
1404 pp_unsupported_tree (pp, type);
1407 /* Pretty-print a brace-enclosed initializer-list. */
1409 static void
1410 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1412 pp_c_left_brace (pp);
1413 pp_c_initializer_list (pp, l);
1414 pp_c_right_brace (pp);
1418 /* This is a convenient function, used to bridge gap between C and C++
1419 grammars.
1421 id-expression:
1422 identifier */
1424 void
1425 c_pretty_printer::id_expression (tree t)
1427 switch (TREE_CODE (t))
1429 case VAR_DECL:
1430 case PARM_DECL:
1431 case CONST_DECL:
1432 case TYPE_DECL:
1433 case FUNCTION_DECL:
1434 case FIELD_DECL:
1435 case LABEL_DECL:
1436 pp_c_tree_decl_identifier (this, t);
1437 break;
1439 case IDENTIFIER_NODE:
1440 pp_c_tree_identifier (this, t);
1441 break;
1443 default:
1444 pp_unsupported_tree (this, t);
1445 break;
1449 /* postfix-expression:
1450 primary-expression
1451 postfix-expression [ expression ]
1452 postfix-expression ( argument-expression-list(opt) )
1453 postfix-expression . identifier
1454 postfix-expression -> identifier
1455 postfix-expression ++
1456 postfix-expression --
1457 ( type-name ) { initializer-list }
1458 ( type-name ) { initializer-list , } */
1460 void
1461 c_pretty_printer::postfix_expression (tree e)
1463 enum tree_code code = TREE_CODE (e);
1464 switch (code)
1466 case POSTINCREMENT_EXPR:
1467 case POSTDECREMENT_EXPR:
1468 postfix_expression (TREE_OPERAND (e, 0));
1469 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1470 break;
1472 case ARRAY_REF:
1473 postfix_expression (TREE_OPERAND (e, 0));
1474 pp_c_left_bracket (this);
1475 expression (TREE_OPERAND (e, 1));
1476 pp_c_right_bracket (this);
1477 break;
1479 case ARRAY_NOTATION_REF:
1480 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1481 pp_c_left_bracket (this);
1482 expression (ARRAY_NOTATION_START (e));
1483 pp_colon (this);
1484 expression (ARRAY_NOTATION_LENGTH (e));
1485 pp_colon (this);
1486 expression (ARRAY_NOTATION_STRIDE (e));
1487 pp_c_right_bracket (this);
1488 break;
1490 case CALL_EXPR:
1492 call_expr_arg_iterator iter;
1493 tree arg;
1494 postfix_expression (CALL_EXPR_FN (e));
1495 pp_c_left_paren (this);
1496 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1498 expression (arg);
1499 if (more_call_expr_args_p (&iter))
1500 pp_separate_with (this, ',');
1502 pp_c_right_paren (this);
1503 break;
1506 case UNORDERED_EXPR:
1507 pp_c_ws_string (this, flag_isoc99
1508 ? "isunordered"
1509 : "__builtin_isunordered");
1510 goto two_args_fun;
1512 case ORDERED_EXPR:
1513 pp_c_ws_string (this, flag_isoc99
1514 ? "!isunordered"
1515 : "!__builtin_isunordered");
1516 goto two_args_fun;
1518 case UNLT_EXPR:
1519 pp_c_ws_string (this, flag_isoc99
1520 ? "!isgreaterequal"
1521 : "!__builtin_isgreaterequal");
1522 goto two_args_fun;
1524 case UNLE_EXPR:
1525 pp_c_ws_string (this, flag_isoc99
1526 ? "!isgreater"
1527 : "!__builtin_isgreater");
1528 goto two_args_fun;
1530 case UNGT_EXPR:
1531 pp_c_ws_string (this, flag_isoc99
1532 ? "!islessequal"
1533 : "!__builtin_islessequal");
1534 goto two_args_fun;
1536 case UNGE_EXPR:
1537 pp_c_ws_string (this, flag_isoc99
1538 ? "!isless"
1539 : "!__builtin_isless");
1540 goto two_args_fun;
1542 case UNEQ_EXPR:
1543 pp_c_ws_string (this, flag_isoc99
1544 ? "!islessgreater"
1545 : "!__builtin_islessgreater");
1546 goto two_args_fun;
1548 case LTGT_EXPR:
1549 pp_c_ws_string (this, flag_isoc99
1550 ? "islessgreater"
1551 : "__builtin_islessgreater");
1552 goto two_args_fun;
1554 case MAX_EXPR:
1555 pp_c_ws_string (this, "max");
1556 goto two_args_fun;
1558 case MIN_EXPR:
1559 pp_c_ws_string (this, "min");
1560 goto two_args_fun;
1562 two_args_fun:
1563 pp_c_left_paren (this);
1564 expression (TREE_OPERAND (e, 0));
1565 pp_separate_with (this, ',');
1566 expression (TREE_OPERAND (e, 1));
1567 pp_c_right_paren (this);
1568 break;
1570 case ABS_EXPR:
1571 pp_c_ws_string (this, "__builtin_abs");
1572 pp_c_left_paren (this);
1573 expression (TREE_OPERAND (e, 0));
1574 pp_c_right_paren (this);
1575 break;
1577 case COMPONENT_REF:
1579 tree object = TREE_OPERAND (e, 0);
1580 if (INDIRECT_REF_P (object))
1582 postfix_expression (TREE_OPERAND (object, 0));
1583 pp_c_arrow (this);
1585 else
1587 postfix_expression (object);
1588 pp_c_dot (this);
1590 expression (TREE_OPERAND (e, 1));
1592 break;
1594 case BIT_FIELD_REF:
1596 tree type = TREE_TYPE (e);
1598 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1599 if (type
1600 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1602 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1603 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1604 if ((bitpos % size) == 0)
1606 pp_c_left_paren (this);
1607 pp_c_left_paren (this);
1608 type_id (type);
1609 pp_c_star (this);
1610 pp_c_right_paren (this);
1611 pp_c_ampersand (this);
1612 expression (TREE_OPERAND (e, 0));
1613 pp_c_right_paren (this);
1614 pp_c_left_bracket (this);
1615 pp_wide_integer (this, bitpos / size);
1616 pp_c_right_bracket (this);
1617 break;
1620 pp_unsupported_tree (this, e);
1622 break;
1624 case MEM_REF:
1625 expression (e);
1626 break;
1628 case COMPLEX_CST:
1629 case VECTOR_CST:
1630 pp_c_compound_literal (this, e);
1631 break;
1633 case COMPLEX_EXPR:
1634 pp_c_complex_expr (this, e);
1635 break;
1637 case COMPOUND_LITERAL_EXPR:
1638 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1639 /* Fall through. */
1640 case CONSTRUCTOR:
1641 initializer (e);
1642 break;
1644 case VA_ARG_EXPR:
1645 pp_c_ws_string (this, "__builtin_va_arg");
1646 pp_c_left_paren (this);
1647 assignment_expression (TREE_OPERAND (e, 0));
1648 pp_separate_with (this, ',');
1649 type_id (TREE_TYPE (e));
1650 pp_c_right_paren (this);
1651 break;
1653 case ADDR_EXPR:
1654 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1656 id_expression (TREE_OPERAND (e, 0));
1657 break;
1659 /* fall through. */
1661 default:
1662 primary_expression (e);
1663 break;
1667 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1669 void
1670 pp_c_expression_list (c_pretty_printer *pp, tree e)
1672 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1674 pp->expression (TREE_VALUE (e));
1675 if (TREE_CHAIN (e))
1676 pp_separate_with (pp, ',');
1680 /* Print out V, which contains the elements of a constructor. */
1682 void
1683 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1685 unsigned HOST_WIDE_INT ix;
1686 tree value;
1688 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1690 pp->expression (value);
1691 if (ix != vec_safe_length (v) - 1)
1692 pp_separate_with (pp, ',');
1696 /* Print out an expression-list in parens, as if it were the argument
1697 list to a function. */
1699 void
1700 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1702 pp_c_left_paren (pp);
1703 if (t && TREE_CODE (t) == TREE_LIST)
1704 pp_c_expression_list (pp, t);
1705 pp_c_right_paren (pp);
1708 /* unary-expression:
1709 postfix-expression
1710 ++ cast-expression
1711 -- cast-expression
1712 unary-operator cast-expression
1713 sizeof unary-expression
1714 sizeof ( type-id )
1716 unary-operator: one of
1717 * & + - ! ~
1719 GNU extensions.
1720 unary-expression:
1721 __alignof__ unary-expression
1722 __alignof__ ( type-id )
1723 __real__ unary-expression
1724 __imag__ unary-expression */
1726 void
1727 c_pretty_printer::unary_expression (tree e)
1729 enum tree_code code = TREE_CODE (e);
1730 switch (code)
1732 case PREINCREMENT_EXPR:
1733 case PREDECREMENT_EXPR:
1734 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1735 unary_expression (TREE_OPERAND (e, 0));
1736 break;
1738 case ADDR_EXPR:
1739 case INDIRECT_REF:
1740 case NEGATE_EXPR:
1741 case BIT_NOT_EXPR:
1742 case TRUTH_NOT_EXPR:
1743 case CONJ_EXPR:
1744 /* String literal are used by address. */
1745 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1746 pp_ampersand (this);
1747 else if (code == INDIRECT_REF)
1749 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
1750 if (type && TREE_CODE (type) == REFERENCE_TYPE)
1751 /* Reference decay is implicit, don't print anything. */;
1752 else
1753 pp_c_star (this);
1755 else if (code == NEGATE_EXPR)
1756 pp_minus (this);
1757 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1758 pp_complement (this);
1759 else if (code == TRUTH_NOT_EXPR)
1760 pp_exclamation (this);
1761 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1762 break;
1764 case MEM_REF:
1765 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1766 && integer_zerop (TREE_OPERAND (e, 1)))
1767 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1768 else
1770 pp_c_star (this);
1771 if (!integer_zerop (TREE_OPERAND (e, 1)))
1773 pp_c_left_paren (this);
1774 if (!integer_onep (TYPE_SIZE_UNIT
1775 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1776 pp_c_type_cast (this, ptr_type_node);
1778 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1779 if (!integer_zerop (TREE_OPERAND (e, 1)))
1781 pp_plus (this);
1782 pp_c_integer_constant (this,
1783 fold_convert (ssizetype,
1784 TREE_OPERAND (e, 1)));
1785 pp_c_right_paren (this);
1788 break;
1790 case REALPART_EXPR:
1791 case IMAGPART_EXPR:
1792 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1793 pp_c_whitespace (this);
1794 unary_expression (TREE_OPERAND (e, 0));
1795 break;
1797 default:
1798 postfix_expression (e);
1799 break;
1803 /* cast-expression:
1804 unary-expression
1805 ( type-name ) cast-expression */
1807 void
1808 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1810 switch (TREE_CODE (e))
1812 case FLOAT_EXPR:
1813 case FIX_TRUNC_EXPR:
1814 CASE_CONVERT:
1815 case VIEW_CONVERT_EXPR:
1816 pp_c_type_cast (pp, TREE_TYPE (e));
1817 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1818 break;
1820 default:
1821 pp->unary_expression (e);
1825 /* multiplicative-expression:
1826 cast-expression
1827 multiplicative-expression * cast-expression
1828 multiplicative-expression / cast-expression
1829 multiplicative-expression % cast-expression */
1831 void
1832 c_pretty_printer::multiplicative_expression (tree e)
1834 enum tree_code code = TREE_CODE (e);
1835 switch (code)
1837 case MULT_EXPR:
1838 case TRUNC_DIV_EXPR:
1839 case TRUNC_MOD_EXPR:
1840 case EXACT_DIV_EXPR:
1841 case RDIV_EXPR:
1842 multiplicative_expression (TREE_OPERAND (e, 0));
1843 pp_c_whitespace (this);
1844 if (code == MULT_EXPR)
1845 pp_c_star (this);
1846 else if (code == TRUNC_DIV_EXPR)
1847 pp_slash (this);
1848 else
1849 pp_modulo (this);
1850 pp_c_whitespace (this);
1851 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1852 break;
1854 default:
1855 pp_c_cast_expression (this, e);
1856 break;
1860 /* additive-expression:
1861 multiplicative-expression
1862 additive-expression + multiplicative-expression
1863 additive-expression - multiplicative-expression */
1865 static void
1866 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1868 enum tree_code code = TREE_CODE (e);
1869 switch (code)
1871 case POINTER_PLUS_EXPR:
1872 case PLUS_EXPR:
1873 case MINUS_EXPR:
1874 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1875 pp_c_whitespace (pp);
1876 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1877 pp_plus (pp);
1878 else
1879 pp_minus (pp);
1880 pp_c_whitespace (pp);
1881 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1882 break;
1884 default:
1885 pp->multiplicative_expression (e);
1886 break;
1890 /* additive-expression:
1891 additive-expression
1892 shift-expression << additive-expression
1893 shift-expression >> additive-expression */
1895 static void
1896 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1898 enum tree_code code = TREE_CODE (e);
1899 switch (code)
1901 case LSHIFT_EXPR:
1902 case RSHIFT_EXPR:
1903 case LROTATE_EXPR:
1904 case RROTATE_EXPR:
1905 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1906 pp_c_whitespace (pp);
1907 pp_string (pp, code == LSHIFT_EXPR ? "<<" :
1908 code == RSHIFT_EXPR ? ">>" :
1909 code == LROTATE_EXPR ? "<<<" : ">>>");
1910 pp_c_whitespace (pp);
1911 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1912 break;
1914 default:
1915 pp_c_additive_expression (pp, e);
1919 /* relational-expression:
1920 shift-expression
1921 relational-expression < shift-expression
1922 relational-expression > shift-expression
1923 relational-expression <= shift-expression
1924 relational-expression >= shift-expression */
1926 static void
1927 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1929 enum tree_code code = TREE_CODE (e);
1930 switch (code)
1932 case LT_EXPR:
1933 case GT_EXPR:
1934 case LE_EXPR:
1935 case GE_EXPR:
1936 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1937 pp_c_whitespace (pp);
1938 if (code == LT_EXPR)
1939 pp_less (pp);
1940 else if (code == GT_EXPR)
1941 pp_greater (pp);
1942 else if (code == LE_EXPR)
1943 pp_less_equal (pp);
1944 else if (code == GE_EXPR)
1945 pp_greater_equal (pp);
1946 pp_c_whitespace (pp);
1947 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1948 break;
1950 default:
1951 pp_c_shift_expression (pp, e);
1952 break;
1956 /* equality-expression:
1957 relational-expression
1958 equality-expression == relational-expression
1959 equality-equality != relational-expression */
1961 static void
1962 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1964 enum tree_code code = TREE_CODE (e);
1965 switch (code)
1967 case EQ_EXPR:
1968 case NE_EXPR:
1969 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1970 pp_c_whitespace (pp);
1971 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1972 pp_c_whitespace (pp);
1973 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1974 break;
1976 default:
1977 pp_c_relational_expression (pp, e);
1978 break;
1982 /* AND-expression:
1983 equality-expression
1984 AND-expression & equality-equality */
1986 static void
1987 pp_c_and_expression (c_pretty_printer *pp, tree e)
1989 if (TREE_CODE (e) == BIT_AND_EXPR)
1991 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1992 pp_c_whitespace (pp);
1993 pp_ampersand (pp);
1994 pp_c_whitespace (pp);
1995 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1997 else
1998 pp_c_equality_expression (pp, e);
2001 /* exclusive-OR-expression:
2002 AND-expression
2003 exclusive-OR-expression ^ AND-expression */
2005 static void
2006 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2008 if (TREE_CODE (e) == BIT_XOR_EXPR
2009 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2011 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2012 if (TREE_CODE (e) == BIT_XOR_EXPR)
2013 pp_c_maybe_whitespace (pp);
2014 else
2015 pp_c_whitespace (pp);
2016 pp_carret (pp);
2017 pp_c_whitespace (pp);
2018 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2020 else
2021 pp_c_and_expression (pp, e);
2024 /* inclusive-OR-expression:
2025 exclusive-OR-expression
2026 inclusive-OR-expression | exclusive-OR-expression */
2028 static void
2029 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2031 if (TREE_CODE (e) == BIT_IOR_EXPR)
2033 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2034 pp_c_whitespace (pp);
2035 pp_bar (pp);
2036 pp_c_whitespace (pp);
2037 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2039 else
2040 pp_c_exclusive_or_expression (pp, e);
2043 /* logical-AND-expression:
2044 inclusive-OR-expression
2045 logical-AND-expression && inclusive-OR-expression */
2047 static void
2048 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2050 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2051 || TREE_CODE (e) == TRUTH_AND_EXPR)
2053 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2054 pp_c_whitespace (pp);
2055 pp_ampersand_ampersand (pp);
2056 pp_c_whitespace (pp);
2057 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2059 else
2060 pp_c_inclusive_or_expression (pp, e);
2063 /* logical-OR-expression:
2064 logical-AND-expression
2065 logical-OR-expression || logical-AND-expression */
2067 void
2068 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2070 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2071 || TREE_CODE (e) == TRUTH_OR_EXPR)
2073 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2074 pp_c_whitespace (pp);
2075 pp_bar_bar (pp);
2076 pp_c_whitespace (pp);
2077 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2079 else
2080 pp_c_logical_and_expression (pp, e);
2083 /* conditional-expression:
2084 logical-OR-expression
2085 logical-OR-expression ? expression : conditional-expression */
2087 void
2088 c_pretty_printer::conditional_expression (tree e)
2090 if (TREE_CODE (e) == COND_EXPR)
2092 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2093 pp_c_whitespace (this);
2094 pp_question (this);
2095 pp_c_whitespace (this);
2096 expression (TREE_OPERAND (e, 1));
2097 pp_c_whitespace (this);
2098 pp_colon (this);
2099 pp_c_whitespace (this);
2100 conditional_expression (TREE_OPERAND (e, 2));
2102 else
2103 pp_c_logical_or_expression (this, e);
2107 /* assignment-expression:
2108 conditional-expression
2109 unary-expression assignment-operator assignment-expression
2111 assignment-expression: one of
2112 = *= /= %= += -= >>= <<= &= ^= |= */
2114 void
2115 c_pretty_printer::assignment_expression (tree e)
2117 if (TREE_CODE (e) == MODIFY_EXPR
2118 || TREE_CODE (e) == INIT_EXPR)
2120 unary_expression (TREE_OPERAND (e, 0));
2121 pp_c_whitespace (this);
2122 pp_equal (this);
2123 pp_space (this);
2124 expression (TREE_OPERAND (e, 1));
2126 else
2127 conditional_expression (e);
2130 /* expression:
2131 assignment-expression
2132 expression , assignment-expression
2134 Implementation note: instead of going through the usual recursion
2135 chain, I take the liberty of dispatching nodes to the appropriate
2136 functions. This makes some redundancy, but it worths it. That also
2137 prevents a possible infinite recursion between primary_expression ()
2138 and expression (). */
2140 void
2141 c_pretty_printer::expression (tree e)
2143 switch (TREE_CODE (e))
2145 case VOID_CST:
2146 pp_c_void_constant (this);
2147 break;
2149 case INTEGER_CST:
2150 pp_c_integer_constant (this, e);
2151 break;
2153 case REAL_CST:
2154 pp_c_floating_constant (this, e);
2155 break;
2157 case FIXED_CST:
2158 pp_c_fixed_constant (this, e);
2159 break;
2161 case STRING_CST:
2162 pp_c_string_literal (this, e);
2163 break;
2165 case IDENTIFIER_NODE:
2166 case FUNCTION_DECL:
2167 case VAR_DECL:
2168 case CONST_DECL:
2169 case PARM_DECL:
2170 case RESULT_DECL:
2171 case FIELD_DECL:
2172 case LABEL_DECL:
2173 case ERROR_MARK:
2174 primary_expression (e);
2175 break;
2177 case SSA_NAME:
2178 if (SSA_NAME_VAR (e)
2179 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2180 expression (SSA_NAME_VAR (e));
2181 else
2182 translate_string ("<unknown>");
2183 break;
2185 case POSTINCREMENT_EXPR:
2186 case POSTDECREMENT_EXPR:
2187 case ARRAY_REF:
2188 case ARRAY_NOTATION_REF:
2189 case CALL_EXPR:
2190 case COMPONENT_REF:
2191 case BIT_FIELD_REF:
2192 case COMPLEX_CST:
2193 case COMPLEX_EXPR:
2194 case VECTOR_CST:
2195 case ORDERED_EXPR:
2196 case UNORDERED_EXPR:
2197 case LTGT_EXPR:
2198 case UNEQ_EXPR:
2199 case UNLE_EXPR:
2200 case UNLT_EXPR:
2201 case UNGE_EXPR:
2202 case UNGT_EXPR:
2203 case MAX_EXPR:
2204 case MIN_EXPR:
2205 case ABS_EXPR:
2206 case CONSTRUCTOR:
2207 case COMPOUND_LITERAL_EXPR:
2208 case VA_ARG_EXPR:
2209 postfix_expression (e);
2210 break;
2212 case CONJ_EXPR:
2213 case ADDR_EXPR:
2214 case INDIRECT_REF:
2215 case MEM_REF:
2216 case NEGATE_EXPR:
2217 case BIT_NOT_EXPR:
2218 case TRUTH_NOT_EXPR:
2219 case PREINCREMENT_EXPR:
2220 case PREDECREMENT_EXPR:
2221 case REALPART_EXPR:
2222 case IMAGPART_EXPR:
2223 unary_expression (e);
2224 break;
2226 case FLOAT_EXPR:
2227 case FIX_TRUNC_EXPR:
2228 CASE_CONVERT:
2229 case VIEW_CONVERT_EXPR:
2230 pp_c_cast_expression (this, e);
2231 break;
2233 case MULT_EXPR:
2234 case TRUNC_MOD_EXPR:
2235 case TRUNC_DIV_EXPR:
2236 case EXACT_DIV_EXPR:
2237 case RDIV_EXPR:
2238 multiplicative_expression (e);
2239 break;
2241 case LSHIFT_EXPR:
2242 case RSHIFT_EXPR:
2243 case LROTATE_EXPR:
2244 case RROTATE_EXPR:
2245 pp_c_shift_expression (this, e);
2246 break;
2248 case LT_EXPR:
2249 case GT_EXPR:
2250 case LE_EXPR:
2251 case GE_EXPR:
2252 pp_c_relational_expression (this, e);
2253 break;
2255 case BIT_AND_EXPR:
2256 pp_c_and_expression (this, e);
2257 break;
2259 case BIT_XOR_EXPR:
2260 case TRUTH_XOR_EXPR:
2261 pp_c_exclusive_or_expression (this, e);
2262 break;
2264 case BIT_IOR_EXPR:
2265 pp_c_inclusive_or_expression (this, e);
2266 break;
2268 case TRUTH_ANDIF_EXPR:
2269 case TRUTH_AND_EXPR:
2270 pp_c_logical_and_expression (this, e);
2271 break;
2273 case TRUTH_ORIF_EXPR:
2274 case TRUTH_OR_EXPR:
2275 pp_c_logical_or_expression (this, e);
2276 break;
2278 case EQ_EXPR:
2279 case NE_EXPR:
2280 pp_c_equality_expression (this, e);
2281 break;
2283 case COND_EXPR:
2284 conditional_expression (e);
2285 break;
2287 case POINTER_PLUS_EXPR:
2288 case PLUS_EXPR:
2289 case MINUS_EXPR:
2290 pp_c_additive_expression (this, e);
2291 break;
2293 case MODIFY_EXPR:
2294 case INIT_EXPR:
2295 assignment_expression (e);
2296 break;
2298 case COMPOUND_EXPR:
2299 pp_c_left_paren (this);
2300 expression (TREE_OPERAND (e, 0));
2301 pp_separate_with (this, ',');
2302 assignment_expression (TREE_OPERAND (e, 1));
2303 pp_c_right_paren (this);
2304 break;
2306 case NON_LVALUE_EXPR:
2307 case SAVE_EXPR:
2308 expression (TREE_OPERAND (e, 0));
2309 break;
2311 case TARGET_EXPR:
2312 postfix_expression (TREE_OPERAND (e, 1));
2313 break;
2315 case BIND_EXPR:
2316 case GOTO_EXPR:
2317 /* We don't yet have a way of dumping statements in a
2318 human-readable format. */
2319 pp_string (this, "({...})");
2320 break;
2322 case C_MAYBE_CONST_EXPR:
2323 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2324 break;
2326 default:
2327 pp_unsupported_tree (this, e);
2328 break;
2334 /* Statements. */
2336 void
2337 c_pretty_printer::statement (tree stmt)
2339 if (stmt == NULL)
2340 return;
2342 if (pp_needs_newline (this))
2343 pp_newline_and_indent (this, 0);
2345 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2349 /* Initialize the PRETTY-PRINTER for handling C codes. */
2351 c_pretty_printer::c_pretty_printer ()
2352 : pretty_printer (),
2353 offset_list (),
2354 flags ()
2356 type_specifier_seq = pp_c_specifier_qualifier_list;
2357 ptr_operator = pp_c_pointer;
2358 parameter_list = pp_c_parameter_type_list;
2362 /* Print the tree T in full, on file FILE. */
2364 void
2365 print_c_tree (FILE *file, tree t)
2367 c_pretty_printer pp;
2369 pp_needs_newline (&pp) = true;
2370 pp.buffer->stream = file;
2371 pp.statement (t);
2372 pp_newline_and_flush (&pp);
2375 /* Print the tree T in full, on stderr. */
2377 DEBUG_FUNCTION void
2378 debug_c_tree (tree t)
2380 print_c_tree (stderr, t);
2381 fputc ('\n', stderr);
2384 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2385 up of T's memory address. */
2387 void
2388 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2390 const char *name;
2392 gcc_assert (DECL_P (t));
2394 if (DECL_NAME (t))
2395 name = IDENTIFIER_POINTER (DECL_NAME (t));
2396 else
2398 static char xname[8];
2399 sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t)
2400 & 0xffff)));
2401 name = xname;
2404 pp_c_identifier (pp, name);