2014-09-12 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob9ac8cbaa2a9a18a42275d95e64a36d2596e72015
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 a VOID_CST (void_node). */
911 static void
912 pp_c_void_constant (c_pretty_printer *pp)
914 pp_c_type_cast (pp, void_type_node);
915 pp_string (pp, "0");
918 /* Pretty-print an INTEGER literal. */
920 static void
921 pp_c_integer_constant (c_pretty_printer *pp, tree i)
923 /* We are going to compare the type of I to other types using
924 pointer comparison so we need to use its canonical type. */
925 tree type =
926 TYPE_CANONICAL (TREE_TYPE (i))
927 ? TYPE_CANONICAL (TREE_TYPE (i))
928 : TREE_TYPE (i);
930 if (tree_fits_shwi_p (i))
931 pp_wide_integer (pp, tree_to_shwi (i));
932 else if (tree_fits_uhwi_p (i))
933 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
934 else
936 wide_int wi = i;
938 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
940 pp_minus (pp);
941 wi = -wi;
943 print_hex (wi, pp_buffer (pp)->digit_buffer);
944 pp_string (pp, pp_buffer (pp)->digit_buffer);
946 if (TYPE_UNSIGNED (type))
947 pp_character (pp, 'u');
948 if (type == long_integer_type_node || type == long_unsigned_type_node)
949 pp_character (pp, 'l');
950 else if (type == long_long_integer_type_node
951 || type == long_long_unsigned_type_node)
952 pp_string (pp, "ll");
953 else if (type == int128_integer_type_node
954 || type == int128_unsigned_type_node)
955 pp_string (pp, "I128");
958 /* Print out a CHARACTER literal. */
960 static void
961 pp_c_character_constant (c_pretty_printer *pp, tree c)
963 pp_quote (pp);
964 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
965 pp_quote (pp);
968 /* Print out a BOOLEAN literal. */
970 static void
971 pp_c_bool_constant (c_pretty_printer *pp, tree b)
973 if (b == boolean_false_node)
975 if (c_dialect_cxx ())
976 pp_c_ws_string (pp, "false");
977 else if (flag_isoc99)
978 pp_c_ws_string (pp, "_False");
979 else
980 pp_unsupported_tree (pp, b);
982 else if (b == boolean_true_node)
984 if (c_dialect_cxx ())
985 pp_c_ws_string (pp, "true");
986 else if (flag_isoc99)
987 pp_c_ws_string (pp, "_True");
988 else
989 pp_unsupported_tree (pp, b);
991 else if (TREE_CODE (b) == INTEGER_CST)
992 pp_c_integer_constant (pp, b);
993 else
994 pp_unsupported_tree (pp, b);
997 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
998 false; that means the value was obtained by a cast, in which case
999 print out the type-id part of the cast-expression -- the casted value
1000 is then printed by pp_c_integer_literal. */
1002 static bool
1003 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1005 bool value_is_named = true;
1006 tree type = TREE_TYPE (e);
1007 tree value;
1009 /* Find the name of this constant. */
1010 for (value = TYPE_VALUES (type);
1011 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1012 value = TREE_CHAIN (value))
1015 if (value != NULL_TREE)
1016 pp->id_expression (TREE_PURPOSE (value));
1017 else
1019 /* Value must have been cast. */
1020 pp_c_type_cast (pp, type);
1021 value_is_named = false;
1024 return value_is_named;
1027 /* Print out a REAL value as a decimal-floating-constant. */
1029 static void
1030 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1032 const struct real_format *fmt
1033 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1035 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1036 bool is_decimal = floating_cst.decimal;
1038 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1039 log10(2) to 7 significant digits. */
1040 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1042 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1043 sizeof (pp_buffer (pp)->digit_buffer),
1044 max_digits10, 1);
1046 pp_string (pp, pp_buffer(pp)->digit_buffer);
1047 if (TREE_TYPE (r) == float_type_node)
1048 pp_character (pp, 'f');
1049 else if (TREE_TYPE (r) == long_double_type_node)
1050 pp_character (pp, 'l');
1051 else if (TREE_TYPE (r) == dfloat128_type_node)
1052 pp_string (pp, "dl");
1053 else if (TREE_TYPE (r) == dfloat64_type_node)
1054 pp_string (pp, "dd");
1055 else if (TREE_TYPE (r) == dfloat32_type_node)
1056 pp_string (pp, "df");
1059 /* Print out a FIXED value as a decimal-floating-constant. */
1061 static void
1062 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1064 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1065 sizeof (pp_buffer (pp)->digit_buffer));
1066 pp_string (pp, pp_buffer(pp)->digit_buffer);
1069 /* Pretty-print a compound literal expression. GNU extensions include
1070 vector constants. */
1072 static void
1073 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1075 tree type = TREE_TYPE (e);
1076 pp_c_type_cast (pp, type);
1078 switch (TREE_CODE (type))
1080 case RECORD_TYPE:
1081 case UNION_TYPE:
1082 case ARRAY_TYPE:
1083 case VECTOR_TYPE:
1084 case COMPLEX_TYPE:
1085 pp_c_brace_enclosed_initializer_list (pp, e);
1086 break;
1088 default:
1089 pp_unsupported_tree (pp, e);
1090 break;
1094 /* Pretty-print a COMPLEX_EXPR expression. */
1096 static void
1097 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1099 /* Handle a few common special cases, otherwise fallback
1100 to printing it as compound literal. */
1101 tree type = TREE_TYPE (e);
1102 tree realexpr = TREE_OPERAND (e, 0);
1103 tree imagexpr = TREE_OPERAND (e, 1);
1105 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1106 if (TREE_CODE (realexpr) == NOP_EXPR
1107 && TREE_CODE (imagexpr) == NOP_EXPR
1108 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1109 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1110 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1111 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1112 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1113 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1115 pp_c_type_cast (pp, type);
1116 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1117 return;
1120 /* Cast of an scalar expression to COMPLEX_TYPE. */
1121 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1122 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1124 pp_c_type_cast (pp, type);
1125 if (TREE_CODE (realexpr) == NOP_EXPR)
1126 realexpr = TREE_OPERAND (realexpr, 0);
1127 pp->expression (realexpr);
1128 return;
1131 pp_c_compound_literal (pp, e);
1134 /* constant:
1135 integer-constant
1136 floating-constant
1137 fixed-point-constant
1138 enumeration-constant
1139 character-constant */
1141 void
1142 c_pretty_printer::constant (tree e)
1144 const enum tree_code code = TREE_CODE (e);
1146 switch (code)
1148 case VOID_CST:
1149 pp_c_void_constant (this);
1150 break;
1152 case INTEGER_CST:
1154 tree type = TREE_TYPE (e);
1155 if (type == boolean_type_node)
1156 pp_c_bool_constant (this, e);
1157 else if (type == char_type_node)
1158 pp_c_character_constant (this, e);
1159 else if (TREE_CODE (type) == ENUMERAL_TYPE
1160 && pp_c_enumeration_constant (this, e))
1162 else
1163 pp_c_integer_constant (this, e);
1165 break;
1167 case REAL_CST:
1168 pp_c_floating_constant (this, e);
1169 break;
1171 case FIXED_CST:
1172 pp_c_fixed_constant (this, e);
1173 break;
1175 case STRING_CST:
1176 pp_c_string_literal (this, e);
1177 break;
1179 case COMPLEX_CST:
1180 /* Sometimes, we are confused and we think a complex literal
1181 is a constant. Such thing is a compound literal which
1182 grammatically belongs to postfix-expr production. */
1183 pp_c_compound_literal (this, e);
1184 break;
1186 default:
1187 pp_unsupported_tree (this, e);
1188 break;
1192 /* Pretty-print a string such as an identifier, without changing its
1193 encoding, preceded by whitespace is necessary. */
1195 void
1196 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1198 pp_c_maybe_whitespace (pp);
1199 pp_string (pp, str);
1200 pp->padding = pp_before;
1203 void
1204 c_pretty_printer::translate_string (const char *gmsgid)
1206 if (pp_translate_identifiers (this))
1207 pp_c_ws_string (this, _(gmsgid));
1208 else
1209 pp_c_ws_string (this, gmsgid);
1212 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1213 that need converting to the locale encoding, preceded by whitespace
1214 is necessary. */
1216 void
1217 pp_c_identifier (c_pretty_printer *pp, const char *id)
1219 pp_c_maybe_whitespace (pp);
1220 pp_identifier (pp, id);
1221 pp->padding = pp_before;
1224 /* Pretty-print a C primary-expression.
1225 primary-expression:
1226 identifier
1227 constant
1228 string-literal
1229 ( expression ) */
1231 void
1232 c_pretty_printer::primary_expression (tree e)
1234 switch (TREE_CODE (e))
1236 case VAR_DECL:
1237 case PARM_DECL:
1238 case FIELD_DECL:
1239 case CONST_DECL:
1240 case FUNCTION_DECL:
1241 case LABEL_DECL:
1242 pp_c_tree_decl_identifier (this, e);
1243 break;
1245 case IDENTIFIER_NODE:
1246 pp_c_tree_identifier (this, e);
1247 break;
1249 case ERROR_MARK:
1250 translate_string ("<erroneous-expression>");
1251 break;
1253 case RESULT_DECL:
1254 translate_string ("<return-value>");
1255 break;
1257 case VOID_CST:
1258 case INTEGER_CST:
1259 case REAL_CST:
1260 case FIXED_CST:
1261 case STRING_CST:
1262 constant (e);
1263 break;
1265 case TARGET_EXPR:
1266 pp_c_ws_string (this, "__builtin_memcpy");
1267 pp_c_left_paren (this);
1268 pp_ampersand (this);
1269 primary_expression (TREE_OPERAND (e, 0));
1270 pp_separate_with (this, ',');
1271 pp_ampersand (this);
1272 initializer (TREE_OPERAND (e, 1));
1273 if (TREE_OPERAND (e, 2))
1275 pp_separate_with (this, ',');
1276 expression (TREE_OPERAND (e, 2));
1278 pp_c_right_paren (this);
1279 break;
1281 default:
1282 /* FIXME: Make sure we won't get into an infinite loop. */
1283 pp_c_left_paren (this);
1284 expression (e);
1285 pp_c_right_paren (this);
1286 break;
1290 /* Print out a C initializer -- also support C compound-literals.
1291 initializer:
1292 assignment-expression:
1293 { initializer-list }
1294 { initializer-list , } */
1296 void
1297 c_pretty_printer::initializer (tree e)
1299 if (TREE_CODE (e) == CONSTRUCTOR)
1300 pp_c_brace_enclosed_initializer_list (this, e);
1301 else
1302 expression (e);
1305 /* init-declarator:
1306 declarator:
1307 declarator = initializer */
1309 void
1310 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1312 pp->declarator (t);
1313 /* We don't want to output function definitions here. There are handled
1314 elsewhere (and the syntactic form is bogus anyway). */
1315 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1317 tree init = DECL_INITIAL (t);
1318 /* This C++ bit is handled here because it is easier to do so.
1319 In templates, the C++ parser builds a TREE_LIST for a
1320 direct-initialization; the TREE_PURPOSE is the variable to
1321 initialize and the TREE_VALUE is the initializer. */
1322 if (TREE_CODE (init) == TREE_LIST)
1324 pp_c_left_paren (pp);
1325 pp->expression (TREE_VALUE (init));
1326 pp_right_paren (pp);
1328 else
1330 pp_space (pp);
1331 pp_equal (pp);
1332 pp_space (pp);
1333 pp->initializer (init);
1338 /* initializer-list:
1339 designation(opt) initializer
1340 initializer-list , designation(opt) initializer
1342 designation:
1343 designator-list =
1345 designator-list:
1346 designator
1347 designator-list designator
1349 designator:
1350 [ constant-expression ]
1351 identifier */
1353 static void
1354 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1356 tree type = TREE_TYPE (e);
1357 const enum tree_code code = TREE_CODE (type);
1359 if (TREE_CODE (e) == CONSTRUCTOR)
1361 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1362 return;
1365 switch (code)
1367 case RECORD_TYPE:
1368 case UNION_TYPE:
1369 case ARRAY_TYPE:
1371 tree init = TREE_OPERAND (e, 0);
1372 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1374 if (code == RECORD_TYPE || code == UNION_TYPE)
1376 pp_c_dot (pp);
1377 pp->primary_expression (TREE_PURPOSE (init));
1379 else
1381 pp_c_left_bracket (pp);
1382 if (TREE_PURPOSE (init))
1383 pp->constant (TREE_PURPOSE (init));
1384 pp_c_right_bracket (pp);
1386 pp_c_whitespace (pp);
1387 pp_equal (pp);
1388 pp_c_whitespace (pp);
1389 pp->initializer (TREE_VALUE (init));
1390 if (TREE_CHAIN (init))
1391 pp_separate_with (pp, ',');
1394 return;
1396 case VECTOR_TYPE:
1397 if (TREE_CODE (e) == VECTOR_CST)
1399 unsigned i;
1400 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1402 if (i > 0)
1403 pp_separate_with (pp, ',');
1404 pp->expression (VECTOR_CST_ELT (e, i));
1407 else
1408 break;
1409 return;
1411 case COMPLEX_TYPE:
1412 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1414 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1415 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1416 pp_separate_with (pp, ',');
1417 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1419 else
1420 break;
1421 return;
1423 default:
1424 break;
1427 pp_unsupported_tree (pp, type);
1430 /* Pretty-print a brace-enclosed initializer-list. */
1432 static void
1433 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1435 pp_c_left_brace (pp);
1436 pp_c_initializer_list (pp, l);
1437 pp_c_right_brace (pp);
1441 /* This is a convenient function, used to bridge gap between C and C++
1442 grammars.
1444 id-expression:
1445 identifier */
1447 void
1448 c_pretty_printer::id_expression (tree t)
1450 switch (TREE_CODE (t))
1452 case VAR_DECL:
1453 case PARM_DECL:
1454 case CONST_DECL:
1455 case TYPE_DECL:
1456 case FUNCTION_DECL:
1457 case FIELD_DECL:
1458 case LABEL_DECL:
1459 pp_c_tree_decl_identifier (this, t);
1460 break;
1462 case IDENTIFIER_NODE:
1463 pp_c_tree_identifier (this, t);
1464 break;
1466 default:
1467 pp_unsupported_tree (this, t);
1468 break;
1472 /* postfix-expression:
1473 primary-expression
1474 postfix-expression [ expression ]
1475 postfix-expression ( argument-expression-list(opt) )
1476 postfix-expression . identifier
1477 postfix-expression -> identifier
1478 postfix-expression ++
1479 postfix-expression --
1480 ( type-name ) { initializer-list }
1481 ( type-name ) { initializer-list , } */
1483 void
1484 c_pretty_printer::postfix_expression (tree e)
1486 enum tree_code code = TREE_CODE (e);
1487 switch (code)
1489 case POSTINCREMENT_EXPR:
1490 case POSTDECREMENT_EXPR:
1491 postfix_expression (TREE_OPERAND (e, 0));
1492 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1493 break;
1495 case ARRAY_REF:
1496 postfix_expression (TREE_OPERAND (e, 0));
1497 pp_c_left_bracket (this);
1498 expression (TREE_OPERAND (e, 1));
1499 pp_c_right_bracket (this);
1500 break;
1502 case ARRAY_NOTATION_REF:
1503 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1504 pp_c_left_bracket (this);
1505 expression (ARRAY_NOTATION_START (e));
1506 pp_colon (this);
1507 expression (ARRAY_NOTATION_LENGTH (e));
1508 pp_colon (this);
1509 expression (ARRAY_NOTATION_STRIDE (e));
1510 pp_c_right_bracket (this);
1511 break;
1513 case CALL_EXPR:
1515 call_expr_arg_iterator iter;
1516 tree arg;
1517 postfix_expression (CALL_EXPR_FN (e));
1518 pp_c_left_paren (this);
1519 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1521 expression (arg);
1522 if (more_call_expr_args_p (&iter))
1523 pp_separate_with (this, ',');
1525 pp_c_right_paren (this);
1526 break;
1529 case UNORDERED_EXPR:
1530 pp_c_ws_string (this, flag_isoc99
1531 ? "isunordered"
1532 : "__builtin_isunordered");
1533 goto two_args_fun;
1535 case ORDERED_EXPR:
1536 pp_c_ws_string (this, flag_isoc99
1537 ? "!isunordered"
1538 : "!__builtin_isunordered");
1539 goto two_args_fun;
1541 case UNLT_EXPR:
1542 pp_c_ws_string (this, flag_isoc99
1543 ? "!isgreaterequal"
1544 : "!__builtin_isgreaterequal");
1545 goto two_args_fun;
1547 case UNLE_EXPR:
1548 pp_c_ws_string (this, flag_isoc99
1549 ? "!isgreater"
1550 : "!__builtin_isgreater");
1551 goto two_args_fun;
1553 case UNGT_EXPR:
1554 pp_c_ws_string (this, flag_isoc99
1555 ? "!islessequal"
1556 : "!__builtin_islessequal");
1557 goto two_args_fun;
1559 case UNGE_EXPR:
1560 pp_c_ws_string (this, flag_isoc99
1561 ? "!isless"
1562 : "!__builtin_isless");
1563 goto two_args_fun;
1565 case UNEQ_EXPR:
1566 pp_c_ws_string (this, flag_isoc99
1567 ? "!islessgreater"
1568 : "!__builtin_islessgreater");
1569 goto two_args_fun;
1571 case LTGT_EXPR:
1572 pp_c_ws_string (this, flag_isoc99
1573 ? "islessgreater"
1574 : "__builtin_islessgreater");
1575 goto two_args_fun;
1577 two_args_fun:
1578 pp_c_left_paren (this);
1579 expression (TREE_OPERAND (e, 0));
1580 pp_separate_with (this, ',');
1581 expression (TREE_OPERAND (e, 1));
1582 pp_c_right_paren (this);
1583 break;
1585 case ABS_EXPR:
1586 pp_c_ws_string (this, "__builtin_abs");
1587 pp_c_left_paren (this);
1588 expression (TREE_OPERAND (e, 0));
1589 pp_c_right_paren (this);
1590 break;
1592 case COMPONENT_REF:
1594 tree object = TREE_OPERAND (e, 0);
1595 if (TREE_CODE (object) == INDIRECT_REF)
1597 postfix_expression (TREE_OPERAND (object, 0));
1598 pp_c_arrow (this);
1600 else
1602 postfix_expression (object);
1603 pp_c_dot (this);
1605 expression (TREE_OPERAND (e, 1));
1607 break;
1609 case BIT_FIELD_REF:
1611 tree type = TREE_TYPE (e);
1613 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1614 if (type
1615 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1617 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1618 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1619 if ((bitpos % size) == 0)
1621 pp_c_left_paren (this);
1622 pp_c_left_paren (this);
1623 type_id (type);
1624 pp_c_star (this);
1625 pp_c_right_paren (this);
1626 pp_c_ampersand (this);
1627 expression (TREE_OPERAND (e, 0));
1628 pp_c_right_paren (this);
1629 pp_c_left_bracket (this);
1630 pp_wide_integer (this, bitpos / size);
1631 pp_c_right_bracket (this);
1632 break;
1635 pp_unsupported_tree (this, e);
1637 break;
1639 case MEM_REF:
1640 expression (e);
1641 break;
1643 case COMPLEX_CST:
1644 case VECTOR_CST:
1645 pp_c_compound_literal (this, e);
1646 break;
1648 case COMPLEX_EXPR:
1649 pp_c_complex_expr (this, e);
1650 break;
1652 case COMPOUND_LITERAL_EXPR:
1653 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1654 /* Fall through. */
1655 case CONSTRUCTOR:
1656 initializer (e);
1657 break;
1659 case VA_ARG_EXPR:
1660 pp_c_ws_string (this, "__builtin_va_arg");
1661 pp_c_left_paren (this);
1662 assignment_expression (TREE_OPERAND (e, 0));
1663 pp_separate_with (this, ',');
1664 type_id (TREE_TYPE (e));
1665 pp_c_right_paren (this);
1666 break;
1668 case ADDR_EXPR:
1669 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1671 id_expression (TREE_OPERAND (e, 0));
1672 break;
1674 /* else fall through. */
1676 default:
1677 primary_expression (e);
1678 break;
1682 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1684 void
1685 pp_c_expression_list (c_pretty_printer *pp, tree e)
1687 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1689 pp->expression (TREE_VALUE (e));
1690 if (TREE_CHAIN (e))
1691 pp_separate_with (pp, ',');
1695 /* Print out V, which contains the elements of a constructor. */
1697 void
1698 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1700 unsigned HOST_WIDE_INT ix;
1701 tree value;
1703 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1705 pp->expression (value);
1706 if (ix != vec_safe_length (v) - 1)
1707 pp_separate_with (pp, ',');
1711 /* Print out an expression-list in parens, as if it were the argument
1712 list to a function. */
1714 void
1715 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1717 pp_c_left_paren (pp);
1718 if (t && TREE_CODE (t) == TREE_LIST)
1719 pp_c_expression_list (pp, t);
1720 pp_c_right_paren (pp);
1723 /* unary-expression:
1724 postfix-expression
1725 ++ cast-expression
1726 -- cast-expression
1727 unary-operator cast-expression
1728 sizeof unary-expression
1729 sizeof ( type-id )
1731 unary-operator: one of
1732 * & + - ! ~
1734 GNU extensions.
1735 unary-expression:
1736 __alignof__ unary-expression
1737 __alignof__ ( type-id )
1738 __real__ unary-expression
1739 __imag__ unary-expression */
1741 void
1742 c_pretty_printer::unary_expression (tree e)
1744 enum tree_code code = TREE_CODE (e);
1745 switch (code)
1747 case PREINCREMENT_EXPR:
1748 case PREDECREMENT_EXPR:
1749 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1750 unary_expression (TREE_OPERAND (e, 0));
1751 break;
1753 case ADDR_EXPR:
1754 case INDIRECT_REF:
1755 case NEGATE_EXPR:
1756 case BIT_NOT_EXPR:
1757 case TRUTH_NOT_EXPR:
1758 case CONJ_EXPR:
1759 /* String literal are used by address. */
1760 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1761 pp_ampersand (this);
1762 else if (code == INDIRECT_REF)
1763 pp_c_star (this);
1764 else if (code == NEGATE_EXPR)
1765 pp_minus (this);
1766 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1767 pp_complement (this);
1768 else if (code == TRUTH_NOT_EXPR)
1769 pp_exclamation (this);
1770 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1771 break;
1773 case MEM_REF:
1774 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1775 && integer_zerop (TREE_OPERAND (e, 1)))
1776 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1777 else
1779 pp_c_star (this);
1780 if (!integer_zerop (TREE_OPERAND (e, 1)))
1782 pp_c_left_paren (this);
1783 if (!integer_onep (TYPE_SIZE_UNIT
1784 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1785 pp_c_type_cast (this, ptr_type_node);
1787 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1788 if (!integer_zerop (TREE_OPERAND (e, 1)))
1790 pp_plus (this);
1791 pp_c_integer_constant (this,
1792 fold_convert (ssizetype,
1793 TREE_OPERAND (e, 1)));
1794 pp_c_right_paren (this);
1797 break;
1799 case REALPART_EXPR:
1800 case IMAGPART_EXPR:
1801 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1802 pp_c_whitespace (this);
1803 unary_expression (TREE_OPERAND (e, 0));
1804 break;
1806 default:
1807 postfix_expression (e);
1808 break;
1812 /* cast-expression:
1813 unary-expression
1814 ( type-name ) cast-expression */
1816 void
1817 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1819 switch (TREE_CODE (e))
1821 case FLOAT_EXPR:
1822 case FIX_TRUNC_EXPR:
1823 CASE_CONVERT:
1824 case VIEW_CONVERT_EXPR:
1825 pp_c_type_cast (pp, TREE_TYPE (e));
1826 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1827 break;
1829 default:
1830 pp->unary_expression (e);
1834 /* multiplicative-expression:
1835 cast-expression
1836 multiplicative-expression * cast-expression
1837 multiplicative-expression / cast-expression
1838 multiplicative-expression % cast-expression */
1840 void
1841 c_pretty_printer::multiplicative_expression (tree e)
1843 enum tree_code code = TREE_CODE (e);
1844 switch (code)
1846 case MULT_EXPR:
1847 case TRUNC_DIV_EXPR:
1848 case TRUNC_MOD_EXPR:
1849 multiplicative_expression (TREE_OPERAND (e, 0));
1850 pp_c_whitespace (this);
1851 if (code == MULT_EXPR)
1852 pp_c_star (this);
1853 else if (code == TRUNC_DIV_EXPR)
1854 pp_slash (this);
1855 else
1856 pp_modulo (this);
1857 pp_c_whitespace (this);
1858 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1859 break;
1861 default:
1862 pp_c_cast_expression (this, e);
1863 break;
1867 /* additive-expression:
1868 multiplicative-expression
1869 additive-expression + multiplicative-expression
1870 additive-expression - multiplicative-expression */
1872 static void
1873 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1875 enum tree_code code = TREE_CODE (e);
1876 switch (code)
1878 case POINTER_PLUS_EXPR:
1879 case PLUS_EXPR:
1880 case MINUS_EXPR:
1881 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1882 pp_c_whitespace (pp);
1883 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1884 pp_plus (pp);
1885 else
1886 pp_minus (pp);
1887 pp_c_whitespace (pp);
1888 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1889 break;
1891 default:
1892 pp->multiplicative_expression (e);
1893 break;
1897 /* additive-expression:
1898 additive-expression
1899 shift-expression << additive-expression
1900 shift-expression >> additive-expression */
1902 static void
1903 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1905 enum tree_code code = TREE_CODE (e);
1906 switch (code)
1908 case LSHIFT_EXPR:
1909 case RSHIFT_EXPR:
1910 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1911 pp_c_whitespace (pp);
1912 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1913 pp_c_whitespace (pp);
1914 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1915 break;
1917 default:
1918 pp_c_additive_expression (pp, e);
1922 /* relational-expression:
1923 shift-expression
1924 relational-expression < shift-expression
1925 relational-expression > shift-expression
1926 relational-expression <= shift-expression
1927 relational-expression >= shift-expression */
1929 static void
1930 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1932 enum tree_code code = TREE_CODE (e);
1933 switch (code)
1935 case LT_EXPR:
1936 case GT_EXPR:
1937 case LE_EXPR:
1938 case GE_EXPR:
1939 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1940 pp_c_whitespace (pp);
1941 if (code == LT_EXPR)
1942 pp_less (pp);
1943 else if (code == GT_EXPR)
1944 pp_greater (pp);
1945 else if (code == LE_EXPR)
1946 pp_less_equal (pp);
1947 else if (code == GE_EXPR)
1948 pp_greater_equal (pp);
1949 pp_c_whitespace (pp);
1950 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1951 break;
1953 default:
1954 pp_c_shift_expression (pp, e);
1955 break;
1959 /* equality-expression:
1960 relational-expression
1961 equality-expression == relational-expression
1962 equality-equality != relational-expression */
1964 static void
1965 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1967 enum tree_code code = TREE_CODE (e);
1968 switch (code)
1970 case EQ_EXPR:
1971 case NE_EXPR:
1972 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1973 pp_c_whitespace (pp);
1974 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1975 pp_c_whitespace (pp);
1976 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1977 break;
1979 default:
1980 pp_c_relational_expression (pp, e);
1981 break;
1985 /* AND-expression:
1986 equality-expression
1987 AND-expression & equality-equality */
1989 static void
1990 pp_c_and_expression (c_pretty_printer *pp, tree e)
1992 if (TREE_CODE (e) == BIT_AND_EXPR)
1994 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1995 pp_c_whitespace (pp);
1996 pp_ampersand (pp);
1997 pp_c_whitespace (pp);
1998 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2000 else
2001 pp_c_equality_expression (pp, e);
2004 /* exclusive-OR-expression:
2005 AND-expression
2006 exclusive-OR-expression ^ AND-expression */
2008 static void
2009 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2011 if (TREE_CODE (e) == BIT_XOR_EXPR
2012 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2014 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2015 if (TREE_CODE (e) == BIT_XOR_EXPR)
2016 pp_c_maybe_whitespace (pp);
2017 else
2018 pp_c_whitespace (pp);
2019 pp_carret (pp);
2020 pp_c_whitespace (pp);
2021 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2023 else
2024 pp_c_and_expression (pp, e);
2027 /* inclusive-OR-expression:
2028 exclusive-OR-expression
2029 inclusive-OR-expression | exclusive-OR-expression */
2031 static void
2032 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2034 if (TREE_CODE (e) == BIT_IOR_EXPR)
2036 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2037 pp_c_whitespace (pp);
2038 pp_bar (pp);
2039 pp_c_whitespace (pp);
2040 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2042 else
2043 pp_c_exclusive_or_expression (pp, e);
2046 /* logical-AND-expression:
2047 inclusive-OR-expression
2048 logical-AND-expression && inclusive-OR-expression */
2050 static void
2051 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2053 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2054 || TREE_CODE (e) == TRUTH_AND_EXPR)
2056 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2057 pp_c_whitespace (pp);
2058 pp_ampersand_ampersand (pp);
2059 pp_c_whitespace (pp);
2060 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2062 else
2063 pp_c_inclusive_or_expression (pp, e);
2066 /* logical-OR-expression:
2067 logical-AND-expression
2068 logical-OR-expression || logical-AND-expression */
2070 void
2071 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2073 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2074 || TREE_CODE (e) == TRUTH_OR_EXPR)
2076 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2077 pp_c_whitespace (pp);
2078 pp_bar_bar (pp);
2079 pp_c_whitespace (pp);
2080 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2082 else
2083 pp_c_logical_and_expression (pp, e);
2086 /* conditional-expression:
2087 logical-OR-expression
2088 logical-OR-expression ? expression : conditional-expression */
2090 void
2091 c_pretty_printer::conditional_expression (tree e)
2093 if (TREE_CODE (e) == COND_EXPR)
2095 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2096 pp_c_whitespace (this);
2097 pp_question (this);
2098 pp_c_whitespace (this);
2099 expression (TREE_OPERAND (e, 1));
2100 pp_c_whitespace (this);
2101 pp_colon (this);
2102 pp_c_whitespace (this);
2103 conditional_expression (TREE_OPERAND (e, 2));
2105 else
2106 pp_c_logical_or_expression (this, e);
2110 /* assignment-expression:
2111 conditional-expression
2112 unary-expression assignment-operator assignment-expression
2114 assignment-expression: one of
2115 = *= /= %= += -= >>= <<= &= ^= |= */
2117 void
2118 c_pretty_printer::assignment_expression (tree e)
2120 if (TREE_CODE (e) == MODIFY_EXPR
2121 || TREE_CODE (e) == INIT_EXPR)
2123 unary_expression (TREE_OPERAND (e, 0));
2124 pp_c_whitespace (this);
2125 pp_equal (this);
2126 pp_space (this);
2127 expression (TREE_OPERAND (e, 1));
2129 else
2130 conditional_expression (e);
2133 /* expression:
2134 assignment-expression
2135 expression , assignment-expression
2137 Implementation note: instead of going through the usual recursion
2138 chain, I take the liberty of dispatching nodes to the appropriate
2139 functions. This makes some redundancy, but it worths it. That also
2140 prevents a possible infinite recursion between primary_expression ()
2141 and expression (). */
2143 void
2144 c_pretty_printer::expression (tree e)
2146 switch (TREE_CODE (e))
2148 case VOID_CST:
2149 pp_c_void_constant (this);
2150 break;
2152 case INTEGER_CST:
2153 pp_c_integer_constant (this, e);
2154 break;
2156 case REAL_CST:
2157 pp_c_floating_constant (this, e);
2158 break;
2160 case FIXED_CST:
2161 pp_c_fixed_constant (this, e);
2162 break;
2164 case STRING_CST:
2165 pp_c_string_literal (this, e);
2166 break;
2168 case IDENTIFIER_NODE:
2169 case FUNCTION_DECL:
2170 case VAR_DECL:
2171 case CONST_DECL:
2172 case PARM_DECL:
2173 case RESULT_DECL:
2174 case FIELD_DECL:
2175 case LABEL_DECL:
2176 case ERROR_MARK:
2177 primary_expression (e);
2178 break;
2180 case SSA_NAME:
2181 if (SSA_NAME_VAR (e)
2182 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2183 expression (SSA_NAME_VAR (e));
2184 else
2185 translate_string ("<unknown>");
2186 break;
2188 case POSTINCREMENT_EXPR:
2189 case POSTDECREMENT_EXPR:
2190 case ARRAY_REF:
2191 case ARRAY_NOTATION_REF:
2192 case CALL_EXPR:
2193 case COMPONENT_REF:
2194 case BIT_FIELD_REF:
2195 case COMPLEX_CST:
2196 case COMPLEX_EXPR:
2197 case VECTOR_CST:
2198 case ORDERED_EXPR:
2199 case UNORDERED_EXPR:
2200 case LTGT_EXPR:
2201 case UNEQ_EXPR:
2202 case UNLE_EXPR:
2203 case UNLT_EXPR:
2204 case UNGE_EXPR:
2205 case UNGT_EXPR:
2206 case ABS_EXPR:
2207 case CONSTRUCTOR:
2208 case COMPOUND_LITERAL_EXPR:
2209 case VA_ARG_EXPR:
2210 postfix_expression (e);
2211 break;
2213 case CONJ_EXPR:
2214 case ADDR_EXPR:
2215 case INDIRECT_REF:
2216 case MEM_REF:
2217 case NEGATE_EXPR:
2218 case BIT_NOT_EXPR:
2219 case TRUTH_NOT_EXPR:
2220 case PREINCREMENT_EXPR:
2221 case PREDECREMENT_EXPR:
2222 case REALPART_EXPR:
2223 case IMAGPART_EXPR:
2224 unary_expression (e);
2225 break;
2227 case FLOAT_EXPR:
2228 case FIX_TRUNC_EXPR:
2229 CASE_CONVERT:
2230 case VIEW_CONVERT_EXPR:
2231 pp_c_cast_expression (this, e);
2232 break;
2234 case MULT_EXPR:
2235 case TRUNC_MOD_EXPR:
2236 case TRUNC_DIV_EXPR:
2237 multiplicative_expression (e);
2238 break;
2240 case LSHIFT_EXPR:
2241 case RSHIFT_EXPR:
2242 pp_c_shift_expression (this, e);
2243 break;
2245 case LT_EXPR:
2246 case GT_EXPR:
2247 case LE_EXPR:
2248 case GE_EXPR:
2249 pp_c_relational_expression (this, e);
2250 break;
2252 case BIT_AND_EXPR:
2253 pp_c_and_expression (this, e);
2254 break;
2256 case BIT_XOR_EXPR:
2257 case TRUTH_XOR_EXPR:
2258 pp_c_exclusive_or_expression (this, e);
2259 break;
2261 case BIT_IOR_EXPR:
2262 pp_c_inclusive_or_expression (this, e);
2263 break;
2265 case TRUTH_ANDIF_EXPR:
2266 case TRUTH_AND_EXPR:
2267 pp_c_logical_and_expression (this, e);
2268 break;
2270 case TRUTH_ORIF_EXPR:
2271 case TRUTH_OR_EXPR:
2272 pp_c_logical_or_expression (this, e);
2273 break;
2275 case EQ_EXPR:
2276 case NE_EXPR:
2277 pp_c_equality_expression (this, e);
2278 break;
2280 case COND_EXPR:
2281 conditional_expression (e);
2282 break;
2284 case POINTER_PLUS_EXPR:
2285 case PLUS_EXPR:
2286 case MINUS_EXPR:
2287 pp_c_additive_expression (this, e);
2288 break;
2290 case MODIFY_EXPR:
2291 case INIT_EXPR:
2292 assignment_expression (e);
2293 break;
2295 case COMPOUND_EXPR:
2296 pp_c_left_paren (this);
2297 expression (TREE_OPERAND (e, 0));
2298 pp_separate_with (this, ',');
2299 assignment_expression (TREE_OPERAND (e, 1));
2300 pp_c_right_paren (this);
2301 break;
2303 case NON_LVALUE_EXPR:
2304 case SAVE_EXPR:
2305 expression (TREE_OPERAND (e, 0));
2306 break;
2308 case TARGET_EXPR:
2309 postfix_expression (TREE_OPERAND (e, 1));
2310 break;
2312 case BIND_EXPR:
2313 case GOTO_EXPR:
2314 /* We don't yet have a way of dumping statements in a
2315 human-readable format. */
2316 pp_string (this, "({...})");
2317 break;
2319 case C_MAYBE_CONST_EXPR:
2320 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2321 break;
2323 default:
2324 pp_unsupported_tree (this, e);
2325 break;
2331 /* Statements. */
2333 void
2334 c_pretty_printer::statement (tree stmt)
2336 if (stmt == NULL)
2337 return;
2339 if (pp_needs_newline (this))
2340 pp_newline_and_indent (this, 0);
2342 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2346 /* Initialize the PRETTY-PRINTER for handling C codes. */
2348 c_pretty_printer::c_pretty_printer ()
2349 : pretty_printer (),
2350 offset_list (),
2351 flags ()
2353 type_specifier_seq = pp_c_specifier_qualifier_list;
2354 ptr_operator = pp_c_pointer;
2355 parameter_list = pp_c_parameter_type_list;
2359 /* Print the tree T in full, on file FILE. */
2361 void
2362 print_c_tree (FILE *file, tree t)
2364 c_pretty_printer pp;
2366 pp_needs_newline (&pp) = true;
2367 pp.buffer->stream = file;
2368 pp.statement (t);
2369 pp_newline_and_flush (&pp);
2372 /* Print the tree T in full, on stderr. */
2374 DEBUG_FUNCTION void
2375 debug_c_tree (tree t)
2377 print_c_tree (stderr, t);
2378 fputc ('\n', stderr);
2381 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2382 up of T's memory address. */
2384 void
2385 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2387 const char *name;
2389 gcc_assert (DECL_P (t));
2391 if (DECL_NAME (t))
2392 name = IDENTIFIER_POINTER (DECL_NAME (t));
2393 else
2395 static char xname[8];
2396 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2397 name = xname;
2400 pp_c_identifier (pp, name);