Don't build libgcc-unwind.map with --disable-shared (PR libgcc/61097)
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob2e97d0147c5224db7e98b0bb615e43bfdbc071dc
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "stor-layout.h"
27 #include "attribs.h"
28 #include "intl.h"
29 #include "c-pretty-print.h"
30 #include "tree-pretty-print.h"
31 #include "tree-iterator.h"
32 #include "diagnostic.h"
33 #include "wide-int-print.h"
35 /* The pretty-printer code is primarily designed to closely follow
36 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
37 codes we used to have in the past. Following a structured
38 approach (preferably the official grammars) is believed to make it
39 much easier to add extensions and nifty pretty-printing effects that
40 takes expression or declaration contexts into account. */
43 #define pp_c_maybe_whitespace(PP) \
44 do { \
45 if ((PP)->padding == pp_before) \
46 pp_c_whitespace (PP); \
47 } while (0)
49 /* literal */
50 static void pp_c_char (c_pretty_printer *, int);
52 /* postfix-expression */
53 static void pp_c_initializer_list (c_pretty_printer *, tree);
54 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
56 static void pp_c_additive_expression (c_pretty_printer *, tree);
57 static void pp_c_shift_expression (c_pretty_printer *, tree);
58 static void pp_c_relational_expression (c_pretty_printer *, tree);
59 static void pp_c_equality_expression (c_pretty_printer *, tree);
60 static void pp_c_and_expression (c_pretty_printer *, tree);
61 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
62 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
63 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
65 /* declarations. */
68 /* Helper functions. */
70 void
71 pp_c_whitespace (c_pretty_printer *pp)
73 pp_space (pp);
74 pp->padding = pp_none;
77 void
78 pp_c_left_paren (c_pretty_printer *pp)
80 pp_left_paren (pp);
81 pp->padding = pp_none;
84 void
85 pp_c_right_paren (c_pretty_printer *pp)
87 pp_right_paren (pp);
88 pp->padding = pp_none;
91 void
92 pp_c_left_brace (c_pretty_printer *pp)
94 pp_left_brace (pp);
95 pp->padding = pp_none;
98 void
99 pp_c_right_brace (c_pretty_printer *pp)
101 pp_right_brace (pp);
102 pp->padding = pp_none;
105 void
106 pp_c_left_bracket (c_pretty_printer *pp)
108 pp_left_bracket (pp);
109 pp->padding = pp_none;
112 void
113 pp_c_right_bracket (c_pretty_printer *pp)
115 pp_right_bracket (pp);
116 pp->padding = pp_none;
119 void
120 pp_c_dot (c_pretty_printer *pp)
122 pp_dot (pp);
123 pp->padding = pp_none;
126 void
127 pp_c_ampersand (c_pretty_printer *pp)
129 pp_ampersand (pp);
130 pp->padding = pp_none;
133 void
134 pp_c_star (c_pretty_printer *pp)
136 pp_star (pp);
137 pp->padding = pp_none;
140 void
141 pp_c_arrow (c_pretty_printer *pp)
143 pp_arrow (pp);
144 pp->padding = pp_none;
147 void
148 pp_c_semicolon (c_pretty_printer *pp)
150 pp_semicolon (pp);
151 pp->padding = pp_none;
154 void
155 pp_c_complement (c_pretty_printer *pp)
157 pp_complement (pp);
158 pp->padding = pp_none;
161 void
162 pp_c_exclamation (c_pretty_printer *pp)
164 pp_exclamation (pp);
165 pp->padding = pp_none;
168 /* Print out the external representation of QUALIFIERS. */
170 void
171 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
173 const char *p = pp_last_position_in_text (pp);
174 bool previous = false;
176 if (!qualifiers)
177 return;
179 /* The C programming language does not have references, but it is much
180 simpler to handle those here rather than going through the same
181 logic in the C++ pretty-printer. */
182 if (p != NULL && (*p == '*' || *p == '&'))
183 pp_c_whitespace (pp);
185 if (qualifiers & TYPE_QUAL_ATOMIC)
187 pp_c_ws_string (pp, "_Atomic");
188 previous = true;
191 if (qualifiers & TYPE_QUAL_CONST)
193 if (previous)
194 pp_c_whitespace (pp);
195 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
196 previous = true;
199 if (qualifiers & TYPE_QUAL_VOLATILE)
201 if (previous)
202 pp_c_whitespace (pp);
203 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
204 previous = true;
207 if (qualifiers & TYPE_QUAL_RESTRICT)
209 if (previous)
210 pp_c_whitespace (pp);
211 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
212 ? "restrict" : "__restrict__"));
216 /* Pretty-print T using the type-cast notation '( type-name )'. */
218 static void
219 pp_c_type_cast (c_pretty_printer *pp, tree t)
221 pp_c_left_paren (pp);
222 pp->type_id (t);
223 pp_c_right_paren (pp);
226 /* We're about to pretty-print a pointer type as indicated by T.
227 Output a whitespace, if needed, preparing for subsequent output. */
229 void
230 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
232 if (POINTER_TYPE_P (t))
234 tree pointee = strip_pointer_operator (TREE_TYPE (t));
235 if (TREE_CODE (pointee) != ARRAY_TYPE
236 && TREE_CODE (pointee) != FUNCTION_TYPE)
237 pp_c_whitespace (pp);
242 /* Declarations. */
244 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
245 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
246 of its type. Take care of possible extensions.
248 type-qualifier-list:
249 type-qualifier
250 type-qualifier-list type-qualifier
252 type-qualifier:
253 const
254 restrict -- C99
255 __restrict__ -- GNU C
256 address-space-qualifier -- GNU C
257 volatile
258 _Atomic -- C11
260 address-space-qualifier:
261 identifier -- GNU C */
263 void
264 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
266 int qualifiers;
268 if (!t || t == error_mark_node)
269 return;
271 if (!TYPE_P (t))
272 t = TREE_TYPE (t);
274 qualifiers = TYPE_QUALS (t);
275 pp_c_cv_qualifiers (pp, qualifiers,
276 TREE_CODE (t) == FUNCTION_TYPE);
278 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
280 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
281 pp_c_identifier (pp, as);
285 /* pointer:
286 * type-qualifier-list(opt)
287 * type-qualifier-list(opt) pointer */
289 static void
290 pp_c_pointer (c_pretty_printer *pp, tree t)
292 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
293 t = TREE_TYPE (t);
294 switch (TREE_CODE (t))
296 case POINTER_TYPE:
297 /* It is easier to handle C++ reference types here. */
298 case REFERENCE_TYPE:
299 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
300 pp_c_pointer (pp, TREE_TYPE (t));
301 if (TREE_CODE (t) == POINTER_TYPE)
302 pp_c_star (pp);
303 else
304 pp_c_ampersand (pp);
305 pp_c_type_qualifier_list (pp, t);
306 break;
308 /* ??? This node is now in GENERIC and so shouldn't be here. But
309 we'll fix that later. */
310 case DECL_EXPR:
311 pp->declaration (DECL_EXPR_DECL (t));
312 pp_needs_newline (pp) = true;
313 break;
315 default:
316 pp_unsupported_tree (pp, t);
320 /* simple-type-specifier:
321 type-specifier
323 type-specifier:
324 void
325 char
326 short
328 long
329 float
330 double
331 signed
332 unsigned
333 _Bool -- C99
334 _Complex -- C99
335 _Imaginary -- C99
336 struct-or-union-specifier
337 enum-specifier
338 typedef-name.
340 GNU extensions.
341 simple-type-specifier:
342 __complex__
343 __vector__ */
345 void
346 c_pretty_printer::simple_type_specifier (tree t)
348 const enum tree_code code = TREE_CODE (t);
349 switch (code)
351 case ERROR_MARK:
352 translate_string ("<type-error>");
353 break;
355 case IDENTIFIER_NODE:
356 pp_c_identifier (this, IDENTIFIER_POINTER (t));
357 break;
359 case VOID_TYPE:
360 case BOOLEAN_TYPE:
361 case INTEGER_TYPE:
362 case REAL_TYPE:
363 case FIXED_POINT_TYPE:
364 if (TYPE_NAME (t))
366 t = TYPE_NAME (t);
367 simple_type_specifier (t);
369 else
371 int prec = TYPE_PRECISION (t);
372 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
373 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
374 else
375 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
376 if (TYPE_NAME (t))
378 simple_type_specifier (t);
379 if (TYPE_PRECISION (t) != prec)
381 pp_colon (this);
382 pp_decimal_int (this, prec);
385 else
387 switch (code)
389 case INTEGER_TYPE:
390 translate_string (TYPE_UNSIGNED (t)
391 ? "<unnamed-unsigned:"
392 : "<unnamed-signed:");
393 break;
394 case REAL_TYPE:
395 translate_string ("<unnamed-float:");
396 break;
397 case FIXED_POINT_TYPE:
398 translate_string ("<unnamed-fixed:");
399 break;
400 default:
401 gcc_unreachable ();
403 pp_decimal_int (this, prec);
404 pp_greater (this);
407 break;
409 case TYPE_DECL:
410 if (DECL_NAME (t))
411 id_expression (t);
412 else
413 translate_string ("<typedef-error>");
414 break;
416 case UNION_TYPE:
417 case RECORD_TYPE:
418 case ENUMERAL_TYPE:
419 if (code == UNION_TYPE)
420 pp_c_ws_string (this, "union");
421 else if (code == RECORD_TYPE)
422 pp_c_ws_string (this, "struct");
423 else if (code == ENUMERAL_TYPE)
424 pp_c_ws_string (this, "enum");
425 else
426 translate_string ("<tag-error>");
428 if (TYPE_NAME (t))
429 id_expression (TYPE_NAME (t));
430 else
431 translate_string ("<anonymous>");
432 break;
434 default:
435 pp_unsupported_tree (this, t);
436 break;
440 /* specifier-qualifier-list:
441 type-specifier specifier-qualifier-list-opt
442 type-qualifier specifier-qualifier-list-opt
445 Implementation note: Because of the non-linearities in array or
446 function declarations, this routine prints not just the
447 specifier-qualifier-list of such entities or types of such entities,
448 but also the 'pointer' production part of their declarators. The
449 remaining part is done by declarator() or abstract_declarator(). */
451 void
452 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
454 const enum tree_code code = TREE_CODE (t);
456 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
457 pp_c_type_qualifier_list (pp, t);
458 switch (code)
460 case REFERENCE_TYPE:
461 case POINTER_TYPE:
463 /* Get the types-specifier of this type. */
464 tree pointee = strip_pointer_operator (TREE_TYPE (t));
465 pp_c_specifier_qualifier_list (pp, pointee);
466 if (TREE_CODE (pointee) == ARRAY_TYPE
467 || TREE_CODE (pointee) == FUNCTION_TYPE)
469 pp_c_whitespace (pp);
470 pp_c_left_paren (pp);
471 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
473 else if (!c_dialect_cxx ())
474 pp_c_whitespace (pp);
475 pp_ptr_operator (pp, t);
477 break;
479 case FUNCTION_TYPE:
480 case ARRAY_TYPE:
481 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
482 break;
484 case VECTOR_TYPE:
485 case COMPLEX_TYPE:
486 if (code == COMPLEX_TYPE)
487 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
488 ? "_Complex" : "__complex__"));
489 else if (code == VECTOR_TYPE)
491 pp_c_ws_string (pp, "__vector");
492 pp_c_left_paren (pp);
493 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
494 pp_c_right_paren (pp);
495 pp_c_whitespace (pp);
497 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
498 break;
500 default:
501 pp->simple_type_specifier (t);
502 break;
504 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
505 pp_c_type_qualifier_list (pp, t);
508 /* parameter-type-list:
509 parameter-list
510 parameter-list , ...
512 parameter-list:
513 parameter-declaration
514 parameter-list , parameter-declaration
516 parameter-declaration:
517 declaration-specifiers declarator
518 declaration-specifiers abstract-declarator(opt) */
520 void
521 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
523 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
524 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
525 pp_c_left_paren (pp);
526 if (parms == void_list_node)
527 pp_c_ws_string (pp, "void");
528 else
530 bool first = true;
531 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
533 if (!first)
534 pp_separate_with (pp, ',');
535 first = false;
536 pp->declaration_specifiers
537 (want_parm_decl ? parms : TREE_VALUE (parms));
538 if (want_parm_decl)
539 pp->declarator (parms);
540 else
541 pp->abstract_declarator (TREE_VALUE (parms));
544 pp_c_right_paren (pp);
547 /* abstract-declarator:
548 pointer
549 pointer(opt) direct-abstract-declarator */
551 void
552 c_pretty_printer::abstract_declarator (tree t)
554 if (TREE_CODE (t) == POINTER_TYPE)
556 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
557 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
558 pp_c_right_paren (this);
559 t = TREE_TYPE (t);
562 direct_abstract_declarator (t);
565 /* direct-abstract-declarator:
566 ( abstract-declarator )
567 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
568 direct-abstract-declarator(opt) [ * ]
569 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
571 void
572 c_pretty_printer::direct_abstract_declarator (tree t)
574 switch (TREE_CODE (t))
576 case POINTER_TYPE:
577 abstract_declarator (t);
578 break;
580 case FUNCTION_TYPE:
581 pp_c_parameter_type_list (this, t);
582 direct_abstract_declarator (TREE_TYPE (t));
583 break;
585 case ARRAY_TYPE:
586 pp_c_left_bracket (this);
587 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
589 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
590 tree type = TREE_TYPE (maxval);
592 if (tree_fits_shwi_p (maxval))
593 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
594 else
595 expression (fold_build2 (PLUS_EXPR, type, maxval,
596 build_int_cst (type, 1)));
598 pp_c_right_bracket (this);
599 direct_abstract_declarator (TREE_TYPE (t));
600 break;
602 case IDENTIFIER_NODE:
603 case VOID_TYPE:
604 case BOOLEAN_TYPE:
605 case INTEGER_TYPE:
606 case REAL_TYPE:
607 case FIXED_POINT_TYPE:
608 case ENUMERAL_TYPE:
609 case RECORD_TYPE:
610 case UNION_TYPE:
611 case VECTOR_TYPE:
612 case COMPLEX_TYPE:
613 case TYPE_DECL:
614 break;
616 default:
617 pp_unsupported_tree (this, t);
618 break;
622 /* type-name:
623 specifier-qualifier-list abstract-declarator(opt) */
625 void
626 c_pretty_printer::type_id (tree t)
628 pp_c_specifier_qualifier_list (this, t);
629 abstract_declarator (t);
632 /* storage-class-specifier:
633 typedef
634 extern
635 static
636 auto
637 register */
639 void
640 c_pretty_printer::storage_class_specifier (tree t)
642 if (TREE_CODE (t) == TYPE_DECL)
643 pp_c_ws_string (this, "typedef");
644 else if (DECL_P (t))
646 if (DECL_REGISTER (t))
647 pp_c_ws_string (this, "register");
648 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
649 pp_c_ws_string (this, "static");
653 /* function-specifier:
654 inline */
656 void
657 c_pretty_printer::function_specifier (tree t)
659 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
660 pp_c_ws_string (this, "inline");
663 /* declaration-specifiers:
664 storage-class-specifier declaration-specifiers(opt)
665 type-specifier declaration-specifiers(opt)
666 type-qualifier declaration-specifiers(opt)
667 function-specifier declaration-specifiers(opt) */
669 void
670 c_pretty_printer::declaration_specifiers (tree t)
672 storage_class_specifier (t);
673 function_specifier (t);
674 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
677 /* direct-declarator
678 identifier
679 ( declarator )
680 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
681 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
682 direct-declarator [ type-qualifier-list static assignment-expression ]
683 direct-declarator [ type-qualifier-list * ]
684 direct-declarator ( parameter-type-list )
685 direct-declarator ( identifier-list(opt) ) */
687 void
688 c_pretty_printer::direct_declarator (tree t)
690 switch (TREE_CODE (t))
692 case VAR_DECL:
693 case PARM_DECL:
694 case TYPE_DECL:
695 case FIELD_DECL:
696 case LABEL_DECL:
697 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
698 pp_c_tree_decl_identifier (this, t);
699 break;
701 case ARRAY_TYPE:
702 case POINTER_TYPE:
703 abstract_declarator (TREE_TYPE (t));
704 break;
706 case FUNCTION_TYPE:
707 pp_parameter_list (this, t);
708 abstract_declarator (TREE_TYPE (t));
709 break;
711 case FUNCTION_DECL:
712 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
713 pp_c_tree_decl_identifier (this, t);
714 if (flags & pp_c_flag_abstract)
715 abstract_declarator (TREE_TYPE (t));
716 else
718 pp_parameter_list (this, t);
719 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
721 break;
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 default:
732 pp_unsupported_tree (this, t);
733 break;
738 /* declarator:
739 pointer(opt) direct-declarator */
741 void
742 c_pretty_printer::declarator (tree t)
744 switch (TREE_CODE (t))
746 case INTEGER_TYPE:
747 case REAL_TYPE:
748 case FIXED_POINT_TYPE:
749 case ENUMERAL_TYPE:
750 case UNION_TYPE:
751 case RECORD_TYPE:
752 break;
754 case VAR_DECL:
755 case PARM_DECL:
756 case FIELD_DECL:
757 case ARRAY_TYPE:
758 case FUNCTION_TYPE:
759 case FUNCTION_DECL:
760 case TYPE_DECL:
761 direct_declarator (t);
762 break;
765 default:
766 pp_unsupported_tree (this, t);
767 break;
771 /* declaration:
772 declaration-specifiers init-declarator-list(opt) ; */
774 void
775 c_pretty_printer::declaration (tree t)
777 declaration_specifiers (t);
778 pp_c_init_declarator (this, t);
781 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
783 void
784 pp_c_attributes (c_pretty_printer *pp, tree attributes)
786 if (attributes == NULL_TREE)
787 return;
789 pp_c_ws_string (pp, "__attribute__");
790 pp_c_left_paren (pp);
791 pp_c_left_paren (pp);
792 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
794 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
795 if (TREE_VALUE (attributes))
796 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
798 if (TREE_CHAIN (attributes))
799 pp_separate_with (pp, ',');
801 pp_c_right_paren (pp);
802 pp_c_right_paren (pp);
805 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
806 marked to be displayed on disgnostic. */
808 void
809 pp_c_attributes_display (c_pretty_printer *pp, tree a)
811 bool is_first = true;
813 if (a == NULL_TREE)
814 return;
816 for (; a != NULL_TREE; a = TREE_CHAIN (a))
818 const struct attribute_spec *as;
819 as = lookup_attribute_spec (TREE_PURPOSE (a));
820 if (!as || as->affects_type_identity == false)
821 continue;
822 if (is_first)
824 pp_c_ws_string (pp, "__attribute__");
825 pp_c_left_paren (pp);
826 pp_c_left_paren (pp);
827 is_first = false;
829 else
831 pp_separate_with (pp, ',');
833 pp_tree_identifier (pp, TREE_PURPOSE (a));
834 if (TREE_VALUE (a))
835 pp_c_call_argument_list (pp, TREE_VALUE (a));
838 if (!is_first)
840 pp_c_right_paren (pp);
841 pp_c_right_paren (pp);
842 pp_c_whitespace (pp);
846 /* function-definition:
847 declaration-specifiers declarator compound-statement */
849 void
850 pp_c_function_definition (c_pretty_printer *pp, tree t)
852 pp->declaration_specifiers (t);
853 pp->declarator (t);
854 pp_needs_newline (pp) = true;
855 pp->statement (DECL_SAVED_TREE (t));
856 pp_newline_and_flush (pp);
860 /* Expressions. */
862 /* Print out a c-char. This is called solely for characters which are
863 in the *target* execution character set. We ought to convert them
864 back to the *host* execution character set before printing, but we
865 have no way to do this at present. A decent compromise is to print
866 all characters as if they were in the host execution character set,
867 and not attempt to recover any named escape characters, but render
868 all unprintables as octal escapes. If the host and target character
869 sets are the same, this produces relatively readable output. If they
870 are not the same, strings may appear as gibberish, but that's okay
871 (in fact, it may well be what the reader wants, e.g. if they are looking
872 to see if conversion to the target character set happened correctly).
874 A special case: we need to prefix \, ", and ' with backslashes. It is
875 correct to do so for the *host*'s \, ", and ', because the rest of the
876 file appears in the host character set. */
878 static void
879 pp_c_char (c_pretty_printer *pp, int c)
881 if (ISPRINT (c))
883 switch (c)
885 case '\\': pp_string (pp, "\\\\"); break;
886 case '\'': pp_string (pp, "\\\'"); break;
887 case '\"': pp_string (pp, "\\\""); break;
888 default: pp_character (pp, c);
891 else
892 pp_scalar (pp, "\\%03o", (unsigned) c);
895 /* Print out a STRING literal. */
897 void
898 pp_c_string_literal (c_pretty_printer *pp, tree s)
900 const char *p = TREE_STRING_POINTER (s);
901 int n = TREE_STRING_LENGTH (s) - 1;
902 int i;
903 pp_doublequote (pp);
904 for (i = 0; i < n; ++i)
905 pp_c_char (pp, p[i]);
906 pp_doublequote (pp);
909 /* Pretty-print an INTEGER literal. */
911 static void
912 pp_c_integer_constant (c_pretty_printer *pp, tree i)
914 /* We are going to compare the type of I to other types using
915 pointer comparison so we need to use its canonical type. */
916 tree type =
917 TYPE_CANONICAL (TREE_TYPE (i))
918 ? TYPE_CANONICAL (TREE_TYPE (i))
919 : TREE_TYPE (i);
921 if (tree_fits_shwi_p (i))
922 pp_wide_integer (pp, tree_to_shwi (i));
923 else if (tree_fits_uhwi_p (i))
924 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
925 else
927 wide_int wi = i;
929 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
931 pp_minus (pp);
932 wi = -wi;
934 print_hex (wi, pp_buffer (pp)->digit_buffer);
935 pp_string (pp, pp_buffer (pp)->digit_buffer);
937 if (TYPE_UNSIGNED (type))
938 pp_character (pp, 'u');
939 if (type == long_integer_type_node || type == long_unsigned_type_node)
940 pp_character (pp, 'l');
941 else if (type == long_long_integer_type_node
942 || type == long_long_unsigned_type_node)
943 pp_string (pp, "ll");
944 else if (type == int128_integer_type_node
945 || type == int128_unsigned_type_node)
946 pp_string (pp, "I128");
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");
1050 /* Print out a FIXED value as a decimal-floating-constant. */
1052 static void
1053 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1055 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1056 sizeof (pp_buffer (pp)->digit_buffer));
1057 pp_string (pp, pp_buffer(pp)->digit_buffer);
1060 /* Pretty-print a compound literal expression. GNU extensions include
1061 vector constants. */
1063 static void
1064 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1066 tree type = TREE_TYPE (e);
1067 pp_c_type_cast (pp, type);
1069 switch (TREE_CODE (type))
1071 case RECORD_TYPE:
1072 case UNION_TYPE:
1073 case ARRAY_TYPE:
1074 case VECTOR_TYPE:
1075 case COMPLEX_TYPE:
1076 pp_c_brace_enclosed_initializer_list (pp, e);
1077 break;
1079 default:
1080 pp_unsupported_tree (pp, e);
1081 break;
1085 /* Pretty-print a COMPLEX_EXPR expression. */
1087 static void
1088 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1090 /* Handle a few common special cases, otherwise fallback
1091 to printing it as compound literal. */
1092 tree type = TREE_TYPE (e);
1093 tree realexpr = TREE_OPERAND (e, 0);
1094 tree imagexpr = TREE_OPERAND (e, 1);
1096 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1097 if (TREE_CODE (realexpr) == NOP_EXPR
1098 && TREE_CODE (imagexpr) == NOP_EXPR
1099 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1100 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1101 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1102 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1103 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1104 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1106 pp_c_type_cast (pp, type);
1107 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1108 return;
1111 /* Cast of an scalar expression to COMPLEX_TYPE. */
1112 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1113 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1115 pp_c_type_cast (pp, type);
1116 if (TREE_CODE (realexpr) == NOP_EXPR)
1117 realexpr = TREE_OPERAND (realexpr, 0);
1118 pp->expression (realexpr);
1119 return;
1122 pp_c_compound_literal (pp, e);
1125 /* constant:
1126 integer-constant
1127 floating-constant
1128 fixed-point-constant
1129 enumeration-constant
1130 character-constant */
1132 void
1133 c_pretty_printer::constant (tree e)
1135 const enum tree_code code = TREE_CODE (e);
1137 switch (code)
1139 case INTEGER_CST:
1141 tree type = TREE_TYPE (e);
1142 if (type == boolean_type_node)
1143 pp_c_bool_constant (this, e);
1144 else if (type == char_type_node)
1145 pp_c_character_constant (this, e);
1146 else if (TREE_CODE (type) == ENUMERAL_TYPE
1147 && pp_c_enumeration_constant (this, e))
1149 else
1150 pp_c_integer_constant (this, e);
1152 break;
1154 case REAL_CST:
1155 pp_c_floating_constant (this, e);
1156 break;
1158 case FIXED_CST:
1159 pp_c_fixed_constant (this, e);
1160 break;
1162 case STRING_CST:
1163 pp_c_string_literal (this, e);
1164 break;
1166 case COMPLEX_CST:
1167 /* Sometimes, we are confused and we think a complex literal
1168 is a constant. Such thing is a compound literal which
1169 grammatically belongs to postfix-expr production. */
1170 pp_c_compound_literal (this, e);
1171 break;
1173 default:
1174 pp_unsupported_tree (this, e);
1175 break;
1179 /* Pretty-print a string such as an identifier, without changing its
1180 encoding, preceded by whitespace is necessary. */
1182 void
1183 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1185 pp_c_maybe_whitespace (pp);
1186 pp_string (pp, str);
1187 pp->padding = pp_before;
1190 void
1191 c_pretty_printer::translate_string (const char *gmsgid)
1193 if (pp_translate_identifiers (this))
1194 pp_c_ws_string (this, _(gmsgid));
1195 else
1196 pp_c_ws_string (this, gmsgid);
1199 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1200 that need converting to the locale encoding, preceded by whitespace
1201 is necessary. */
1203 void
1204 pp_c_identifier (c_pretty_printer *pp, const char *id)
1206 pp_c_maybe_whitespace (pp);
1207 pp_identifier (pp, id);
1208 pp->padding = pp_before;
1211 /* Pretty-print a C primary-expression.
1212 primary-expression:
1213 identifier
1214 constant
1215 string-literal
1216 ( expression ) */
1218 void
1219 c_pretty_printer::primary_expression (tree e)
1221 switch (TREE_CODE (e))
1223 case VAR_DECL:
1224 case PARM_DECL:
1225 case FIELD_DECL:
1226 case CONST_DECL:
1227 case FUNCTION_DECL:
1228 case LABEL_DECL:
1229 pp_c_tree_decl_identifier (this, e);
1230 break;
1232 case IDENTIFIER_NODE:
1233 pp_c_tree_identifier (this, e);
1234 break;
1236 case ERROR_MARK:
1237 translate_string ("<erroneous-expression>");
1238 break;
1240 case RESULT_DECL:
1241 translate_string ("<return-value>");
1242 break;
1244 case INTEGER_CST:
1245 case REAL_CST:
1246 case FIXED_CST:
1247 case STRING_CST:
1248 constant (e);
1249 break;
1251 case TARGET_EXPR:
1252 pp_c_ws_string (this, "__builtin_memcpy");
1253 pp_c_left_paren (this);
1254 pp_ampersand (this);
1255 primary_expression (TREE_OPERAND (e, 0));
1256 pp_separate_with (this, ',');
1257 pp_ampersand (this);
1258 initializer (TREE_OPERAND (e, 1));
1259 if (TREE_OPERAND (e, 2))
1261 pp_separate_with (this, ',');
1262 expression (TREE_OPERAND (e, 2));
1264 pp_c_right_paren (this);
1265 break;
1267 default:
1268 /* FIXME: Make sure we won't get into an infinite loop. */
1269 pp_c_left_paren (this);
1270 expression (e);
1271 pp_c_right_paren (this);
1272 break;
1276 /* Print out a C initializer -- also support C compound-literals.
1277 initializer:
1278 assignment-expression:
1279 { initializer-list }
1280 { initializer-list , } */
1282 void
1283 c_pretty_printer::initializer (tree e)
1285 if (TREE_CODE (e) == CONSTRUCTOR)
1286 pp_c_brace_enclosed_initializer_list (this, e);
1287 else
1288 expression (e);
1291 /* init-declarator:
1292 declarator:
1293 declarator = initializer */
1295 void
1296 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1298 pp->declarator (t);
1299 /* We don't want to output function definitions here. There are handled
1300 elsewhere (and the syntactic form is bogus anyway). */
1301 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1303 tree init = DECL_INITIAL (t);
1304 /* This C++ bit is handled here because it is easier to do so.
1305 In templates, the C++ parser builds a TREE_LIST for a
1306 direct-initialization; the TREE_PURPOSE is the variable to
1307 initialize and the TREE_VALUE is the initializer. */
1308 if (TREE_CODE (init) == TREE_LIST)
1310 pp_c_left_paren (pp);
1311 pp->expression (TREE_VALUE (init));
1312 pp_right_paren (pp);
1314 else
1316 pp_space (pp);
1317 pp_equal (pp);
1318 pp_space (pp);
1319 pp->initializer (init);
1324 /* initializer-list:
1325 designation(opt) initializer
1326 initializer-list , designation(opt) initializer
1328 designation:
1329 designator-list =
1331 designator-list:
1332 designator
1333 designator-list designator
1335 designator:
1336 [ constant-expression ]
1337 identifier */
1339 static void
1340 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1342 tree type = TREE_TYPE (e);
1343 const enum tree_code code = TREE_CODE (type);
1345 if (TREE_CODE (e) == CONSTRUCTOR)
1347 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1348 return;
1351 switch (code)
1353 case RECORD_TYPE:
1354 case UNION_TYPE:
1355 case ARRAY_TYPE:
1357 tree init = TREE_OPERAND (e, 0);
1358 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1360 if (code == RECORD_TYPE || code == UNION_TYPE)
1362 pp_c_dot (pp);
1363 pp->primary_expression (TREE_PURPOSE (init));
1365 else
1367 pp_c_left_bracket (pp);
1368 if (TREE_PURPOSE (init))
1369 pp->constant (TREE_PURPOSE (init));
1370 pp_c_right_bracket (pp);
1372 pp_c_whitespace (pp);
1373 pp_equal (pp);
1374 pp_c_whitespace (pp);
1375 pp->initializer (TREE_VALUE (init));
1376 if (TREE_CHAIN (init))
1377 pp_separate_with (pp, ',');
1380 return;
1382 case VECTOR_TYPE:
1383 if (TREE_CODE (e) == VECTOR_CST)
1385 unsigned i;
1386 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1388 if (i > 0)
1389 pp_separate_with (pp, ',');
1390 pp->expression (VECTOR_CST_ELT (e, i));
1393 else
1394 break;
1395 return;
1397 case COMPLEX_TYPE:
1398 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1400 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1401 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1402 pp_separate_with (pp, ',');
1403 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1405 else
1406 break;
1407 return;
1409 default:
1410 break;
1413 pp_unsupported_tree (pp, type);
1416 /* Pretty-print a brace-enclosed initializer-list. */
1418 static void
1419 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1421 pp_c_left_brace (pp);
1422 pp_c_initializer_list (pp, l);
1423 pp_c_right_brace (pp);
1427 /* This is a convenient function, used to bridge gap between C and C++
1428 grammars.
1430 id-expression:
1431 identifier */
1433 void
1434 c_pretty_printer::id_expression (tree t)
1436 switch (TREE_CODE (t))
1438 case VAR_DECL:
1439 case PARM_DECL:
1440 case CONST_DECL:
1441 case TYPE_DECL:
1442 case FUNCTION_DECL:
1443 case FIELD_DECL:
1444 case LABEL_DECL:
1445 pp_c_tree_decl_identifier (this, t);
1446 break;
1448 case IDENTIFIER_NODE:
1449 pp_c_tree_identifier (this, t);
1450 break;
1452 default:
1453 pp_unsupported_tree (this, t);
1454 break;
1458 /* postfix-expression:
1459 primary-expression
1460 postfix-expression [ expression ]
1461 postfix-expression ( argument-expression-list(opt) )
1462 postfix-expression . identifier
1463 postfix-expression -> identifier
1464 postfix-expression ++
1465 postfix-expression --
1466 ( type-name ) { initializer-list }
1467 ( type-name ) { initializer-list , } */
1469 void
1470 c_pretty_printer::postfix_expression (tree e)
1472 enum tree_code code = TREE_CODE (e);
1473 switch (code)
1475 case POSTINCREMENT_EXPR:
1476 case POSTDECREMENT_EXPR:
1477 postfix_expression (TREE_OPERAND (e, 0));
1478 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1479 break;
1481 case ARRAY_REF:
1482 postfix_expression (TREE_OPERAND (e, 0));
1483 pp_c_left_bracket (this);
1484 expression (TREE_OPERAND (e, 1));
1485 pp_c_right_bracket (this);
1486 break;
1488 case ARRAY_NOTATION_REF:
1489 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1490 pp_c_left_bracket (this);
1491 expression (ARRAY_NOTATION_START (e));
1492 pp_colon (this);
1493 expression (ARRAY_NOTATION_LENGTH (e));
1494 pp_colon (this);
1495 expression (ARRAY_NOTATION_STRIDE (e));
1496 pp_c_right_bracket (this);
1497 break;
1499 case CALL_EXPR:
1501 call_expr_arg_iterator iter;
1502 tree arg;
1503 postfix_expression (CALL_EXPR_FN (e));
1504 pp_c_left_paren (this);
1505 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1507 expression (arg);
1508 if (more_call_expr_args_p (&iter))
1509 pp_separate_with (this, ',');
1511 pp_c_right_paren (this);
1512 break;
1515 case UNORDERED_EXPR:
1516 pp_c_ws_string (this, flag_isoc99
1517 ? "isunordered"
1518 : "__builtin_isunordered");
1519 goto two_args_fun;
1521 case ORDERED_EXPR:
1522 pp_c_ws_string (this, flag_isoc99
1523 ? "!isunordered"
1524 : "!__builtin_isunordered");
1525 goto two_args_fun;
1527 case UNLT_EXPR:
1528 pp_c_ws_string (this, flag_isoc99
1529 ? "!isgreaterequal"
1530 : "!__builtin_isgreaterequal");
1531 goto two_args_fun;
1533 case UNLE_EXPR:
1534 pp_c_ws_string (this, flag_isoc99
1535 ? "!isgreater"
1536 : "!__builtin_isgreater");
1537 goto two_args_fun;
1539 case UNGT_EXPR:
1540 pp_c_ws_string (this, flag_isoc99
1541 ? "!islessequal"
1542 : "!__builtin_islessequal");
1543 goto two_args_fun;
1545 case UNGE_EXPR:
1546 pp_c_ws_string (this, flag_isoc99
1547 ? "!isless"
1548 : "!__builtin_isless");
1549 goto two_args_fun;
1551 case UNEQ_EXPR:
1552 pp_c_ws_string (this, flag_isoc99
1553 ? "!islessgreater"
1554 : "!__builtin_islessgreater");
1555 goto two_args_fun;
1557 case LTGT_EXPR:
1558 pp_c_ws_string (this, flag_isoc99
1559 ? "islessgreater"
1560 : "__builtin_islessgreater");
1561 goto two_args_fun;
1563 two_args_fun:
1564 pp_c_left_paren (this);
1565 expression (TREE_OPERAND (e, 0));
1566 pp_separate_with (this, ',');
1567 expression (TREE_OPERAND (e, 1));
1568 pp_c_right_paren (this);
1569 break;
1571 case ABS_EXPR:
1572 pp_c_ws_string (this, "__builtin_abs");
1573 pp_c_left_paren (this);
1574 expression (TREE_OPERAND (e, 0));
1575 pp_c_right_paren (this);
1576 break;
1578 case COMPONENT_REF:
1580 tree object = TREE_OPERAND (e, 0);
1581 if (TREE_CODE (object) == INDIRECT_REF)
1583 postfix_expression (TREE_OPERAND (object, 0));
1584 pp_c_arrow (this);
1586 else
1588 postfix_expression (object);
1589 pp_c_dot (this);
1591 expression (TREE_OPERAND (e, 1));
1593 break;
1595 case BIT_FIELD_REF:
1597 tree type = TREE_TYPE (e);
1599 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1600 if (type
1601 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1603 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1604 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1605 if ((bitpos % size) == 0)
1607 pp_c_left_paren (this);
1608 pp_c_left_paren (this);
1609 type_id (type);
1610 pp_c_star (this);
1611 pp_c_right_paren (this);
1612 pp_c_ampersand (this);
1613 expression (TREE_OPERAND (e, 0));
1614 pp_c_right_paren (this);
1615 pp_c_left_bracket (this);
1616 pp_wide_integer (this, bitpos / size);
1617 pp_c_right_bracket (this);
1618 break;
1621 pp_unsupported_tree (this, e);
1623 break;
1625 case MEM_REF:
1626 expression (e);
1627 break;
1629 case COMPLEX_CST:
1630 case VECTOR_CST:
1631 pp_c_compound_literal (this, e);
1632 break;
1634 case COMPLEX_EXPR:
1635 pp_c_complex_expr (this, e);
1636 break;
1638 case COMPOUND_LITERAL_EXPR:
1639 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1640 /* Fall through. */
1641 case CONSTRUCTOR:
1642 initializer (e);
1643 break;
1645 case VA_ARG_EXPR:
1646 pp_c_ws_string (this, "__builtin_va_arg");
1647 pp_c_left_paren (this);
1648 assignment_expression (TREE_OPERAND (e, 0));
1649 pp_separate_with (this, ',');
1650 type_id (TREE_TYPE (e));
1651 pp_c_right_paren (this);
1652 break;
1654 case ADDR_EXPR:
1655 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1657 id_expression (TREE_OPERAND (e, 0));
1658 break;
1660 /* else fall through. */
1662 default:
1663 primary_expression (e);
1664 break;
1668 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1670 void
1671 pp_c_expression_list (c_pretty_printer *pp, tree e)
1673 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1675 pp->expression (TREE_VALUE (e));
1676 if (TREE_CHAIN (e))
1677 pp_separate_with (pp, ',');
1681 /* Print out V, which contains the elements of a constructor. */
1683 void
1684 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1686 unsigned HOST_WIDE_INT ix;
1687 tree value;
1689 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1691 pp->expression (value);
1692 if (ix != vec_safe_length (v) - 1)
1693 pp_separate_with (pp, ',');
1697 /* Print out an expression-list in parens, as if it were the argument
1698 list to a function. */
1700 void
1701 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1703 pp_c_left_paren (pp);
1704 if (t && TREE_CODE (t) == TREE_LIST)
1705 pp_c_expression_list (pp, t);
1706 pp_c_right_paren (pp);
1709 /* unary-expression:
1710 postfix-expression
1711 ++ cast-expression
1712 -- cast-expression
1713 unary-operator cast-expression
1714 sizeof unary-expression
1715 sizeof ( type-id )
1717 unary-operator: one of
1718 * & + - ! ~
1720 GNU extensions.
1721 unary-expression:
1722 __alignof__ unary-expression
1723 __alignof__ ( type-id )
1724 __real__ unary-expression
1725 __imag__ unary-expression */
1727 void
1728 c_pretty_printer::unary_expression (tree e)
1730 enum tree_code code = TREE_CODE (e);
1731 switch (code)
1733 case PREINCREMENT_EXPR:
1734 case PREDECREMENT_EXPR:
1735 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1736 unary_expression (TREE_OPERAND (e, 0));
1737 break;
1739 case ADDR_EXPR:
1740 case INDIRECT_REF:
1741 case NEGATE_EXPR:
1742 case BIT_NOT_EXPR:
1743 case TRUTH_NOT_EXPR:
1744 case CONJ_EXPR:
1745 /* String literal are used by address. */
1746 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1747 pp_ampersand (this);
1748 else if (code == INDIRECT_REF)
1749 pp_c_star (this);
1750 else if (code == NEGATE_EXPR)
1751 pp_minus (this);
1752 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1753 pp_complement (this);
1754 else if (code == TRUTH_NOT_EXPR)
1755 pp_exclamation (this);
1756 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1757 break;
1759 case MEM_REF:
1760 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1761 && integer_zerop (TREE_OPERAND (e, 1)))
1762 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1763 else
1765 pp_c_star (this);
1766 if (!integer_zerop (TREE_OPERAND (e, 1)))
1768 pp_c_left_paren (this);
1769 if (!integer_onep (TYPE_SIZE_UNIT
1770 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1771 pp_c_type_cast (this, ptr_type_node);
1773 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1774 if (!integer_zerop (TREE_OPERAND (e, 1)))
1776 pp_plus (this);
1777 pp_c_integer_constant (this,
1778 fold_convert (ssizetype,
1779 TREE_OPERAND (e, 1)));
1780 pp_c_right_paren (this);
1783 break;
1785 case REALPART_EXPR:
1786 case IMAGPART_EXPR:
1787 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1788 pp_c_whitespace (this);
1789 unary_expression (TREE_OPERAND (e, 0));
1790 break;
1792 default:
1793 postfix_expression (e);
1794 break;
1798 /* cast-expression:
1799 unary-expression
1800 ( type-name ) cast-expression */
1802 void
1803 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1805 switch (TREE_CODE (e))
1807 case FLOAT_EXPR:
1808 case FIX_TRUNC_EXPR:
1809 CASE_CONVERT:
1810 case VIEW_CONVERT_EXPR:
1811 pp_c_type_cast (pp, TREE_TYPE (e));
1812 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1813 break;
1815 default:
1816 pp->unary_expression (e);
1820 /* multiplicative-expression:
1821 cast-expression
1822 multiplicative-expression * cast-expression
1823 multiplicative-expression / cast-expression
1824 multiplicative-expression % cast-expression */
1826 void
1827 c_pretty_printer::multiplicative_expression (tree e)
1829 enum tree_code code = TREE_CODE (e);
1830 switch (code)
1832 case MULT_EXPR:
1833 case TRUNC_DIV_EXPR:
1834 case TRUNC_MOD_EXPR:
1835 multiplicative_expression (TREE_OPERAND (e, 0));
1836 pp_c_whitespace (this);
1837 if (code == MULT_EXPR)
1838 pp_c_star (this);
1839 else if (code == TRUNC_DIV_EXPR)
1840 pp_slash (this);
1841 else
1842 pp_modulo (this);
1843 pp_c_whitespace (this);
1844 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1845 break;
1847 default:
1848 pp_c_cast_expression (this, e);
1849 break;
1853 /* additive-expression:
1854 multiplicative-expression
1855 additive-expression + multiplicative-expression
1856 additive-expression - multiplicative-expression */
1858 static void
1859 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1861 enum tree_code code = TREE_CODE (e);
1862 switch (code)
1864 case POINTER_PLUS_EXPR:
1865 case PLUS_EXPR:
1866 case MINUS_EXPR:
1867 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1868 pp_c_whitespace (pp);
1869 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1870 pp_plus (pp);
1871 else
1872 pp_minus (pp);
1873 pp_c_whitespace (pp);
1874 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1875 break;
1877 default:
1878 pp->multiplicative_expression (e);
1879 break;
1883 /* additive-expression:
1884 additive-expression
1885 shift-expression << additive-expression
1886 shift-expression >> additive-expression */
1888 static void
1889 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1891 enum tree_code code = TREE_CODE (e);
1892 switch (code)
1894 case LSHIFT_EXPR:
1895 case RSHIFT_EXPR:
1896 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1897 pp_c_whitespace (pp);
1898 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1899 pp_c_whitespace (pp);
1900 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1901 break;
1903 default:
1904 pp_c_additive_expression (pp, e);
1908 /* relational-expression:
1909 shift-expression
1910 relational-expression < shift-expression
1911 relational-expression > shift-expression
1912 relational-expression <= shift-expression
1913 relational-expression >= shift-expression */
1915 static void
1916 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1918 enum tree_code code = TREE_CODE (e);
1919 switch (code)
1921 case LT_EXPR:
1922 case GT_EXPR:
1923 case LE_EXPR:
1924 case GE_EXPR:
1925 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1926 pp_c_whitespace (pp);
1927 if (code == LT_EXPR)
1928 pp_less (pp);
1929 else if (code == GT_EXPR)
1930 pp_greater (pp);
1931 else if (code == LE_EXPR)
1932 pp_less_equal (pp);
1933 else if (code == GE_EXPR)
1934 pp_greater_equal (pp);
1935 pp_c_whitespace (pp);
1936 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1937 break;
1939 default:
1940 pp_c_shift_expression (pp, e);
1941 break;
1945 /* equality-expression:
1946 relational-expression
1947 equality-expression == relational-expression
1948 equality-equality != relational-expression */
1950 static void
1951 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1953 enum tree_code code = TREE_CODE (e);
1954 switch (code)
1956 case EQ_EXPR:
1957 case NE_EXPR:
1958 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1959 pp_c_whitespace (pp);
1960 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1961 pp_c_whitespace (pp);
1962 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1963 break;
1965 default:
1966 pp_c_relational_expression (pp, e);
1967 break;
1971 /* AND-expression:
1972 equality-expression
1973 AND-expression & equality-equality */
1975 static void
1976 pp_c_and_expression (c_pretty_printer *pp, tree e)
1978 if (TREE_CODE (e) == BIT_AND_EXPR)
1980 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1981 pp_c_whitespace (pp);
1982 pp_ampersand (pp);
1983 pp_c_whitespace (pp);
1984 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1986 else
1987 pp_c_equality_expression (pp, e);
1990 /* exclusive-OR-expression:
1991 AND-expression
1992 exclusive-OR-expression ^ AND-expression */
1994 static void
1995 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1997 if (TREE_CODE (e) == BIT_XOR_EXPR
1998 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2000 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2001 if (TREE_CODE (e) == BIT_XOR_EXPR)
2002 pp_c_maybe_whitespace (pp);
2003 else
2004 pp_c_whitespace (pp);
2005 pp_carret (pp);
2006 pp_c_whitespace (pp);
2007 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2009 else
2010 pp_c_and_expression (pp, e);
2013 /* inclusive-OR-expression:
2014 exclusive-OR-expression
2015 inclusive-OR-expression | exclusive-OR-expression */
2017 static void
2018 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2020 if (TREE_CODE (e) == BIT_IOR_EXPR)
2022 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2023 pp_c_whitespace (pp);
2024 pp_bar (pp);
2025 pp_c_whitespace (pp);
2026 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2028 else
2029 pp_c_exclusive_or_expression (pp, e);
2032 /* logical-AND-expression:
2033 inclusive-OR-expression
2034 logical-AND-expression && inclusive-OR-expression */
2036 static void
2037 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2039 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2040 || TREE_CODE (e) == TRUTH_AND_EXPR)
2042 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2043 pp_c_whitespace (pp);
2044 pp_ampersand_ampersand (pp);
2045 pp_c_whitespace (pp);
2046 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2048 else
2049 pp_c_inclusive_or_expression (pp, e);
2052 /* logical-OR-expression:
2053 logical-AND-expression
2054 logical-OR-expression || logical-AND-expression */
2056 void
2057 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2059 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2060 || TREE_CODE (e) == TRUTH_OR_EXPR)
2062 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2063 pp_c_whitespace (pp);
2064 pp_bar_bar (pp);
2065 pp_c_whitespace (pp);
2066 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2068 else
2069 pp_c_logical_and_expression (pp, e);
2072 /* conditional-expression:
2073 logical-OR-expression
2074 logical-OR-expression ? expression : conditional-expression */
2076 void
2077 c_pretty_printer::conditional_expression (tree e)
2079 if (TREE_CODE (e) == COND_EXPR)
2081 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2082 pp_c_whitespace (this);
2083 pp_question (this);
2084 pp_c_whitespace (this);
2085 expression (TREE_OPERAND (e, 1));
2086 pp_c_whitespace (this);
2087 pp_colon (this);
2088 pp_c_whitespace (this);
2089 conditional_expression (TREE_OPERAND (e, 2));
2091 else
2092 pp_c_logical_or_expression (this, e);
2096 /* assignment-expression:
2097 conditional-expression
2098 unary-expression assignment-operator assignment-expression
2100 assignment-expression: one of
2101 = *= /= %= += -= >>= <<= &= ^= |= */
2103 void
2104 c_pretty_printer::assignment_expression (tree e)
2106 if (TREE_CODE (e) == MODIFY_EXPR
2107 || TREE_CODE (e) == INIT_EXPR)
2109 unary_expression (TREE_OPERAND (e, 0));
2110 pp_c_whitespace (this);
2111 pp_equal (this);
2112 pp_space (this);
2113 expression (TREE_OPERAND (e, 1));
2115 else
2116 conditional_expression (e);
2119 /* expression:
2120 assignment-expression
2121 expression , assignment-expression
2123 Implementation note: instead of going through the usual recursion
2124 chain, I take the liberty of dispatching nodes to the appropriate
2125 functions. This makes some redundancy, but it worths it. That also
2126 prevents a possible infinite recursion between primary_expression ()
2127 and expression (). */
2129 void
2130 c_pretty_printer::expression (tree e)
2132 switch (TREE_CODE (e))
2134 case INTEGER_CST:
2135 pp_c_integer_constant (this, e);
2136 break;
2138 case REAL_CST:
2139 pp_c_floating_constant (this, e);
2140 break;
2142 case FIXED_CST:
2143 pp_c_fixed_constant (this, e);
2144 break;
2146 case STRING_CST:
2147 pp_c_string_literal (this, e);
2148 break;
2150 case IDENTIFIER_NODE:
2151 case FUNCTION_DECL:
2152 case VAR_DECL:
2153 case CONST_DECL:
2154 case PARM_DECL:
2155 case RESULT_DECL:
2156 case FIELD_DECL:
2157 case LABEL_DECL:
2158 case ERROR_MARK:
2159 primary_expression (e);
2160 break;
2162 case SSA_NAME:
2163 if (SSA_NAME_VAR (e)
2164 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2165 expression (SSA_NAME_VAR (e));
2166 else
2167 translate_string ("<unknown>");
2168 break;
2170 case POSTINCREMENT_EXPR:
2171 case POSTDECREMENT_EXPR:
2172 case ARRAY_REF:
2173 case ARRAY_NOTATION_REF:
2174 case CALL_EXPR:
2175 case COMPONENT_REF:
2176 case BIT_FIELD_REF:
2177 case COMPLEX_CST:
2178 case COMPLEX_EXPR:
2179 case VECTOR_CST:
2180 case ORDERED_EXPR:
2181 case UNORDERED_EXPR:
2182 case LTGT_EXPR:
2183 case UNEQ_EXPR:
2184 case UNLE_EXPR:
2185 case UNLT_EXPR:
2186 case UNGE_EXPR:
2187 case UNGT_EXPR:
2188 case ABS_EXPR:
2189 case CONSTRUCTOR:
2190 case COMPOUND_LITERAL_EXPR:
2191 case VA_ARG_EXPR:
2192 postfix_expression (e);
2193 break;
2195 case CONJ_EXPR:
2196 case ADDR_EXPR:
2197 case INDIRECT_REF:
2198 case MEM_REF:
2199 case NEGATE_EXPR:
2200 case BIT_NOT_EXPR:
2201 case TRUTH_NOT_EXPR:
2202 case PREINCREMENT_EXPR:
2203 case PREDECREMENT_EXPR:
2204 case REALPART_EXPR:
2205 case IMAGPART_EXPR:
2206 unary_expression (e);
2207 break;
2209 case FLOAT_EXPR:
2210 case FIX_TRUNC_EXPR:
2211 CASE_CONVERT:
2212 case VIEW_CONVERT_EXPR:
2213 pp_c_cast_expression (this, e);
2214 break;
2216 case MULT_EXPR:
2217 case TRUNC_MOD_EXPR:
2218 case TRUNC_DIV_EXPR:
2219 multiplicative_expression (e);
2220 break;
2222 case LSHIFT_EXPR:
2223 case RSHIFT_EXPR:
2224 pp_c_shift_expression (this, e);
2225 break;
2227 case LT_EXPR:
2228 case GT_EXPR:
2229 case LE_EXPR:
2230 case GE_EXPR:
2231 pp_c_relational_expression (this, e);
2232 break;
2234 case BIT_AND_EXPR:
2235 pp_c_and_expression (this, e);
2236 break;
2238 case BIT_XOR_EXPR:
2239 case TRUTH_XOR_EXPR:
2240 pp_c_exclusive_or_expression (this, e);
2241 break;
2243 case BIT_IOR_EXPR:
2244 pp_c_inclusive_or_expression (this, e);
2245 break;
2247 case TRUTH_ANDIF_EXPR:
2248 case TRUTH_AND_EXPR:
2249 pp_c_logical_and_expression (this, e);
2250 break;
2252 case TRUTH_ORIF_EXPR:
2253 case TRUTH_OR_EXPR:
2254 pp_c_logical_or_expression (this, e);
2255 break;
2257 case EQ_EXPR:
2258 case NE_EXPR:
2259 pp_c_equality_expression (this, e);
2260 break;
2262 case COND_EXPR:
2263 conditional_expression (e);
2264 break;
2266 case POINTER_PLUS_EXPR:
2267 case PLUS_EXPR:
2268 case MINUS_EXPR:
2269 pp_c_additive_expression (this, e);
2270 break;
2272 case MODIFY_EXPR:
2273 case INIT_EXPR:
2274 assignment_expression (e);
2275 break;
2277 case COMPOUND_EXPR:
2278 pp_c_left_paren (this);
2279 expression (TREE_OPERAND (e, 0));
2280 pp_separate_with (this, ',');
2281 assignment_expression (TREE_OPERAND (e, 1));
2282 pp_c_right_paren (this);
2283 break;
2285 case NON_LVALUE_EXPR:
2286 case SAVE_EXPR:
2287 expression (TREE_OPERAND (e, 0));
2288 break;
2290 case TARGET_EXPR:
2291 postfix_expression (TREE_OPERAND (e, 1));
2292 break;
2294 case BIND_EXPR:
2295 case GOTO_EXPR:
2296 /* We don't yet have a way of dumping statements in a
2297 human-readable format. */
2298 pp_string (this, "({...})");
2299 break;
2301 case C_MAYBE_CONST_EXPR:
2302 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2303 break;
2305 default:
2306 pp_unsupported_tree (this, e);
2307 break;
2313 /* Statements. */
2315 void
2316 c_pretty_printer::statement (tree stmt)
2318 if (stmt == NULL)
2319 return;
2321 if (pp_needs_newline (this))
2322 pp_newline_and_indent (this, 0);
2324 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2328 /* Initialize the PRETTY-PRINTER for handling C codes. */
2330 c_pretty_printer::c_pretty_printer ()
2331 : pretty_printer (),
2332 offset_list (),
2333 flags ()
2335 type_specifier_seq = pp_c_specifier_qualifier_list;
2336 ptr_operator = pp_c_pointer;
2337 parameter_list = pp_c_parameter_type_list;
2341 /* Print the tree T in full, on file FILE. */
2343 void
2344 print_c_tree (FILE *file, tree t)
2346 c_pretty_printer pp;
2348 pp_needs_newline (&pp) = true;
2349 pp.buffer->stream = file;
2350 pp.statement (t);
2351 pp_newline_and_flush (&pp);
2354 /* Print the tree T in full, on stderr. */
2356 DEBUG_FUNCTION void
2357 debug_c_tree (tree t)
2359 print_c_tree (stderr, t);
2360 fputc ('\n', stderr);
2363 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2364 up of T's memory address. */
2366 void
2367 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2369 const char *name;
2371 gcc_assert (DECL_P (t));
2373 if (DECL_NAME (t))
2374 name = IDENTIFIER_POINTER (DECL_NAME (t));
2375 else
2377 static char xname[8];
2378 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2379 name = xname;
2382 pp_c_identifier (pp, name);