re PR inline-asm/61692 (ICE in extract_insn in recog.c for asm with many parameters)
[official-gcc.git] / gcc / c-family / c-pretty-print.c
bloba4dd93d2a7396df1a3eea092f74a38a799c72d2e
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 (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
420 /* Don't decorate the type if this is a typedef name. */;
421 else if (code == UNION_TYPE)
422 pp_c_ws_string (this, "union");
423 else if (code == RECORD_TYPE)
424 pp_c_ws_string (this, "struct");
425 else if (code == ENUMERAL_TYPE)
426 pp_c_ws_string (this, "enum");
427 else
428 translate_string ("<tag-error>");
430 if (TYPE_NAME (t))
431 id_expression (TYPE_NAME (t));
432 else
433 translate_string ("<anonymous>");
434 break;
436 default:
437 pp_unsupported_tree (this, t);
438 break;
442 /* specifier-qualifier-list:
443 type-specifier specifier-qualifier-list-opt
444 type-qualifier specifier-qualifier-list-opt
447 Implementation note: Because of the non-linearities in array or
448 function declarations, this routine prints not just the
449 specifier-qualifier-list of such entities or types of such entities,
450 but also the 'pointer' production part of their declarators. The
451 remaining part is done by declarator() or abstract_declarator(). */
453 void
454 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
456 const enum tree_code code = TREE_CODE (t);
458 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
459 pp_c_type_qualifier_list (pp, t);
460 switch (code)
462 case REFERENCE_TYPE:
463 case POINTER_TYPE:
465 /* Get the types-specifier of this type. */
466 tree pointee = strip_pointer_operator (TREE_TYPE (t));
467 pp_c_specifier_qualifier_list (pp, pointee);
468 if (TREE_CODE (pointee) == ARRAY_TYPE
469 || TREE_CODE (pointee) == FUNCTION_TYPE)
471 pp_c_whitespace (pp);
472 pp_c_left_paren (pp);
473 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
475 else if (!c_dialect_cxx ())
476 pp_c_whitespace (pp);
477 pp_ptr_operator (pp, t);
479 break;
481 case FUNCTION_TYPE:
482 case ARRAY_TYPE:
483 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
484 break;
486 case VECTOR_TYPE:
487 case COMPLEX_TYPE:
488 if (code == COMPLEX_TYPE)
489 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
490 ? "_Complex" : "__complex__"));
491 else if (code == VECTOR_TYPE)
493 pp_c_ws_string (pp, "__vector");
494 pp_c_left_paren (pp);
495 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
496 pp_c_right_paren (pp);
497 pp_c_whitespace (pp);
499 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
500 break;
502 default:
503 pp->simple_type_specifier (t);
504 break;
506 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
507 pp_c_type_qualifier_list (pp, t);
510 /* parameter-type-list:
511 parameter-list
512 parameter-list , ...
514 parameter-list:
515 parameter-declaration
516 parameter-list , parameter-declaration
518 parameter-declaration:
519 declaration-specifiers declarator
520 declaration-specifiers abstract-declarator(opt) */
522 void
523 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
525 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
526 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
527 pp_c_left_paren (pp);
528 if (parms == void_list_node)
529 pp_c_ws_string (pp, "void");
530 else
532 bool first = true;
533 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
535 if (!first)
536 pp_separate_with (pp, ',');
537 first = false;
538 pp->declaration_specifiers
539 (want_parm_decl ? parms : TREE_VALUE (parms));
540 if (want_parm_decl)
541 pp->declarator (parms);
542 else
543 pp->abstract_declarator (TREE_VALUE (parms));
546 pp_c_right_paren (pp);
549 /* abstract-declarator:
550 pointer
551 pointer(opt) direct-abstract-declarator */
553 void
554 c_pretty_printer::abstract_declarator (tree t)
556 if (TREE_CODE (t) == POINTER_TYPE)
558 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
559 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
560 pp_c_right_paren (this);
561 t = TREE_TYPE (t);
564 direct_abstract_declarator (t);
567 /* direct-abstract-declarator:
568 ( abstract-declarator )
569 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
570 direct-abstract-declarator(opt) [ * ]
571 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
573 void
574 c_pretty_printer::direct_abstract_declarator (tree t)
576 switch (TREE_CODE (t))
578 case POINTER_TYPE:
579 abstract_declarator (t);
580 break;
582 case FUNCTION_TYPE:
583 pp_c_parameter_type_list (this, t);
584 direct_abstract_declarator (TREE_TYPE (t));
585 break;
587 case ARRAY_TYPE:
588 pp_c_left_bracket (this);
589 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
591 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
592 tree type = TREE_TYPE (maxval);
594 if (tree_fits_shwi_p (maxval))
595 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
596 else
597 expression (fold_build2 (PLUS_EXPR, type, maxval,
598 build_int_cst (type, 1)));
600 pp_c_right_bracket (this);
601 direct_abstract_declarator (TREE_TYPE (t));
602 break;
604 case IDENTIFIER_NODE:
605 case VOID_TYPE:
606 case BOOLEAN_TYPE:
607 case INTEGER_TYPE:
608 case REAL_TYPE:
609 case FIXED_POINT_TYPE:
610 case ENUMERAL_TYPE:
611 case RECORD_TYPE:
612 case UNION_TYPE:
613 case VECTOR_TYPE:
614 case COMPLEX_TYPE:
615 case TYPE_DECL:
616 break;
618 default:
619 pp_unsupported_tree (this, t);
620 break;
624 /* type-name:
625 specifier-qualifier-list abstract-declarator(opt) */
627 void
628 c_pretty_printer::type_id (tree t)
630 pp_c_specifier_qualifier_list (this, t);
631 abstract_declarator (t);
634 /* storage-class-specifier:
635 typedef
636 extern
637 static
638 auto
639 register */
641 void
642 c_pretty_printer::storage_class_specifier (tree t)
644 if (TREE_CODE (t) == TYPE_DECL)
645 pp_c_ws_string (this, "typedef");
646 else if (DECL_P (t))
648 if (DECL_REGISTER (t))
649 pp_c_ws_string (this, "register");
650 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
651 pp_c_ws_string (this, "static");
655 /* function-specifier:
656 inline */
658 void
659 c_pretty_printer::function_specifier (tree t)
661 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
662 pp_c_ws_string (this, "inline");
665 /* declaration-specifiers:
666 storage-class-specifier declaration-specifiers(opt)
667 type-specifier declaration-specifiers(opt)
668 type-qualifier declaration-specifiers(opt)
669 function-specifier declaration-specifiers(opt) */
671 void
672 c_pretty_printer::declaration_specifiers (tree t)
674 storage_class_specifier (t);
675 function_specifier (t);
676 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
679 /* direct-declarator
680 identifier
681 ( declarator )
682 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
683 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
684 direct-declarator [ type-qualifier-list static assignment-expression ]
685 direct-declarator [ type-qualifier-list * ]
686 direct-declarator ( parameter-type-list )
687 direct-declarator ( identifier-list(opt) ) */
689 void
690 c_pretty_printer::direct_declarator (tree t)
692 switch (TREE_CODE (t))
694 case VAR_DECL:
695 case PARM_DECL:
696 case TYPE_DECL:
697 case FIELD_DECL:
698 case LABEL_DECL:
699 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
700 pp_c_tree_decl_identifier (this, t);
701 break;
703 case ARRAY_TYPE:
704 case POINTER_TYPE:
705 abstract_declarator (TREE_TYPE (t));
706 break;
708 case FUNCTION_TYPE:
709 pp_parameter_list (this, t);
710 abstract_declarator (TREE_TYPE (t));
711 break;
713 case FUNCTION_DECL:
714 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
715 pp_c_tree_decl_identifier (this, t);
716 if (flags & pp_c_flag_abstract)
717 abstract_declarator (TREE_TYPE (t));
718 else
720 pp_parameter_list (this, t);
721 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
723 break;
725 case INTEGER_TYPE:
726 case REAL_TYPE:
727 case FIXED_POINT_TYPE:
728 case ENUMERAL_TYPE:
729 case UNION_TYPE:
730 case RECORD_TYPE:
731 break;
733 default:
734 pp_unsupported_tree (this, t);
735 break;
740 /* declarator:
741 pointer(opt) direct-declarator */
743 void
744 c_pretty_printer::declarator (tree t)
746 switch (TREE_CODE (t))
748 case INTEGER_TYPE:
749 case REAL_TYPE:
750 case FIXED_POINT_TYPE:
751 case ENUMERAL_TYPE:
752 case UNION_TYPE:
753 case RECORD_TYPE:
754 break;
756 case VAR_DECL:
757 case PARM_DECL:
758 case FIELD_DECL:
759 case ARRAY_TYPE:
760 case FUNCTION_TYPE:
761 case FUNCTION_DECL:
762 case TYPE_DECL:
763 direct_declarator (t);
764 break;
767 default:
768 pp_unsupported_tree (this, t);
769 break;
773 /* declaration:
774 declaration-specifiers init-declarator-list(opt) ; */
776 void
777 c_pretty_printer::declaration (tree t)
779 declaration_specifiers (t);
780 pp_c_init_declarator (this, t);
783 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
785 void
786 pp_c_attributes (c_pretty_printer *pp, tree attributes)
788 if (attributes == NULL_TREE)
789 return;
791 pp_c_ws_string (pp, "__attribute__");
792 pp_c_left_paren (pp);
793 pp_c_left_paren (pp);
794 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
796 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
797 if (TREE_VALUE (attributes))
798 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
800 if (TREE_CHAIN (attributes))
801 pp_separate_with (pp, ',');
803 pp_c_right_paren (pp);
804 pp_c_right_paren (pp);
807 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
808 marked to be displayed on disgnostic. */
810 void
811 pp_c_attributes_display (c_pretty_printer *pp, tree a)
813 bool is_first = true;
815 if (a == NULL_TREE)
816 return;
818 for (; a != NULL_TREE; a = TREE_CHAIN (a))
820 const struct attribute_spec *as;
821 as = lookup_attribute_spec (TREE_PURPOSE (a));
822 if (!as || as->affects_type_identity == false)
823 continue;
824 if (is_first)
826 pp_c_ws_string (pp, "__attribute__");
827 pp_c_left_paren (pp);
828 pp_c_left_paren (pp);
829 is_first = false;
831 else
833 pp_separate_with (pp, ',');
835 pp_tree_identifier (pp, TREE_PURPOSE (a));
836 if (TREE_VALUE (a))
837 pp_c_call_argument_list (pp, TREE_VALUE (a));
840 if (!is_first)
842 pp_c_right_paren (pp);
843 pp_c_right_paren (pp);
844 pp_c_whitespace (pp);
848 /* function-definition:
849 declaration-specifiers declarator compound-statement */
851 void
852 pp_c_function_definition (c_pretty_printer *pp, tree t)
854 pp->declaration_specifiers (t);
855 pp->declarator (t);
856 pp_needs_newline (pp) = true;
857 pp->statement (DECL_SAVED_TREE (t));
858 pp_newline_and_flush (pp);
862 /* Expressions. */
864 /* Print out a c-char. This is called solely for characters which are
865 in the *target* execution character set. We ought to convert them
866 back to the *host* execution character set before printing, but we
867 have no way to do this at present. A decent compromise is to print
868 all characters as if they were in the host execution character set,
869 and not attempt to recover any named escape characters, but render
870 all unprintables as octal escapes. If the host and target character
871 sets are the same, this produces relatively readable output. If they
872 are not the same, strings may appear as gibberish, but that's okay
873 (in fact, it may well be what the reader wants, e.g. if they are looking
874 to see if conversion to the target character set happened correctly).
876 A special case: we need to prefix \, ", and ' with backslashes. It is
877 correct to do so for the *host*'s \, ", and ', because the rest of the
878 file appears in the host character set. */
880 static void
881 pp_c_char (c_pretty_printer *pp, int c)
883 if (ISPRINT (c))
885 switch (c)
887 case '\\': pp_string (pp, "\\\\"); break;
888 case '\'': pp_string (pp, "\\\'"); break;
889 case '\"': pp_string (pp, "\\\""); break;
890 default: pp_character (pp, c);
893 else
894 pp_scalar (pp, "\\%03o", (unsigned) c);
897 /* Print out a STRING literal. */
899 void
900 pp_c_string_literal (c_pretty_printer *pp, tree s)
902 const char *p = TREE_STRING_POINTER (s);
903 int n = TREE_STRING_LENGTH (s) - 1;
904 int i;
905 pp_doublequote (pp);
906 for (i = 0; i < n; ++i)
907 pp_c_char (pp, p[i]);
908 pp_doublequote (pp);
911 /* Pretty-print a VOID_CST (void_node). */
913 static void
914 pp_c_void_constant (c_pretty_printer *pp)
916 pp_c_type_cast (pp, void_type_node);
917 pp_string (pp, "0");
920 /* Pretty-print an INTEGER literal. */
922 static void
923 pp_c_integer_constant (c_pretty_printer *pp, tree i)
925 int idx;
927 /* We are going to compare the type of I to other types using
928 pointer comparison so we need to use its canonical type. */
929 tree type =
930 TYPE_CANONICAL (TREE_TYPE (i))
931 ? TYPE_CANONICAL (TREE_TYPE (i))
932 : TREE_TYPE (i);
934 if (tree_fits_shwi_p (i))
935 pp_wide_integer (pp, tree_to_shwi (i));
936 else if (tree_fits_uhwi_p (i))
937 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
938 else
940 wide_int wi = i;
942 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
944 pp_minus (pp);
945 wi = -wi;
947 print_hex (wi, pp_buffer (pp)->digit_buffer);
948 pp_string (pp, pp_buffer (pp)->digit_buffer);
950 if (TYPE_UNSIGNED (type))
951 pp_character (pp, 'u');
952 if (type == long_integer_type_node || type == long_unsigned_type_node)
953 pp_character (pp, 'l');
954 else if (type == long_long_integer_type_node
955 || type == long_long_unsigned_type_node)
956 pp_string (pp, "ll");
957 else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++)
958 if (int_n_enabled_p[idx])
960 char buf[2+20];
961 if (type == int_n_trees[idx].signed_type
962 || type == int_n_trees[idx].unsigned_type)
964 sprintf (buf, "I%d", int_n_data[idx].bitsize);
965 pp_string (pp, buf);
970 /* Print out a CHARACTER literal. */
972 static void
973 pp_c_character_constant (c_pretty_printer *pp, tree c)
975 pp_quote (pp);
976 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
977 pp_quote (pp);
980 /* Print out a BOOLEAN literal. */
982 static void
983 pp_c_bool_constant (c_pretty_printer *pp, tree b)
985 if (b == boolean_false_node)
987 if (c_dialect_cxx ())
988 pp_c_ws_string (pp, "false");
989 else if (flag_isoc99)
990 pp_c_ws_string (pp, "_False");
991 else
992 pp_unsupported_tree (pp, b);
994 else if (b == boolean_true_node)
996 if (c_dialect_cxx ())
997 pp_c_ws_string (pp, "true");
998 else if (flag_isoc99)
999 pp_c_ws_string (pp, "_True");
1000 else
1001 pp_unsupported_tree (pp, b);
1003 else if (TREE_CODE (b) == INTEGER_CST)
1004 pp_c_integer_constant (pp, b);
1005 else
1006 pp_unsupported_tree (pp, b);
1009 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
1010 false; that means the value was obtained by a cast, in which case
1011 print out the type-id part of the cast-expression -- the casted value
1012 is then printed by pp_c_integer_literal. */
1014 static bool
1015 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1017 bool value_is_named = true;
1018 tree type = TREE_TYPE (e);
1019 tree value;
1021 /* Find the name of this constant. */
1022 for (value = TYPE_VALUES (type);
1023 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1024 value = TREE_CHAIN (value))
1027 if (value != NULL_TREE)
1028 pp->id_expression (TREE_PURPOSE (value));
1029 else
1031 /* Value must have been cast. */
1032 pp_c_type_cast (pp, type);
1033 value_is_named = false;
1036 return value_is_named;
1039 /* Print out a REAL value as a decimal-floating-constant. */
1041 static void
1042 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1044 const struct real_format *fmt
1045 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1047 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1048 bool is_decimal = floating_cst.decimal;
1050 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1051 log10(2) to 7 significant digits. */
1052 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1054 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1055 sizeof (pp_buffer (pp)->digit_buffer),
1056 max_digits10, 1);
1058 pp_string (pp, pp_buffer(pp)->digit_buffer);
1059 if (TREE_TYPE (r) == float_type_node)
1060 pp_character (pp, 'f');
1061 else if (TREE_TYPE (r) == long_double_type_node)
1062 pp_character (pp, 'l');
1063 else if (TREE_TYPE (r) == dfloat128_type_node)
1064 pp_string (pp, "dl");
1065 else if (TREE_TYPE (r) == dfloat64_type_node)
1066 pp_string (pp, "dd");
1067 else if (TREE_TYPE (r) == dfloat32_type_node)
1068 pp_string (pp, "df");
1071 /* Print out a FIXED value as a decimal-floating-constant. */
1073 static void
1074 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1076 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1077 sizeof (pp_buffer (pp)->digit_buffer));
1078 pp_string (pp, pp_buffer(pp)->digit_buffer);
1081 /* Pretty-print a compound literal expression. GNU extensions include
1082 vector constants. */
1084 static void
1085 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1087 tree type = TREE_TYPE (e);
1088 pp_c_type_cast (pp, type);
1090 switch (TREE_CODE (type))
1092 case RECORD_TYPE:
1093 case UNION_TYPE:
1094 case ARRAY_TYPE:
1095 case VECTOR_TYPE:
1096 case COMPLEX_TYPE:
1097 pp_c_brace_enclosed_initializer_list (pp, e);
1098 break;
1100 default:
1101 pp_unsupported_tree (pp, e);
1102 break;
1106 /* Pretty-print a COMPLEX_EXPR expression. */
1108 static void
1109 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1111 /* Handle a few common special cases, otherwise fallback
1112 to printing it as compound literal. */
1113 tree type = TREE_TYPE (e);
1114 tree realexpr = TREE_OPERAND (e, 0);
1115 tree imagexpr = TREE_OPERAND (e, 1);
1117 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1118 if (TREE_CODE (realexpr) == NOP_EXPR
1119 && TREE_CODE (imagexpr) == NOP_EXPR
1120 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1121 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1122 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1123 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1124 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1125 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1127 pp_c_type_cast (pp, type);
1128 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1129 return;
1132 /* Cast of an scalar expression to COMPLEX_TYPE. */
1133 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1134 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1136 pp_c_type_cast (pp, type);
1137 if (TREE_CODE (realexpr) == NOP_EXPR)
1138 realexpr = TREE_OPERAND (realexpr, 0);
1139 pp->expression (realexpr);
1140 return;
1143 pp_c_compound_literal (pp, e);
1146 /* constant:
1147 integer-constant
1148 floating-constant
1149 fixed-point-constant
1150 enumeration-constant
1151 character-constant */
1153 void
1154 c_pretty_printer::constant (tree e)
1156 const enum tree_code code = TREE_CODE (e);
1158 switch (code)
1160 case VOID_CST:
1161 pp_c_void_constant (this);
1162 break;
1164 case INTEGER_CST:
1166 tree type = TREE_TYPE (e);
1167 if (type == boolean_type_node)
1168 pp_c_bool_constant (this, e);
1169 else if (type == char_type_node)
1170 pp_c_character_constant (this, e);
1171 else if (TREE_CODE (type) == ENUMERAL_TYPE
1172 && pp_c_enumeration_constant (this, e))
1174 else
1175 pp_c_integer_constant (this, e);
1177 break;
1179 case REAL_CST:
1180 pp_c_floating_constant (this, e);
1181 break;
1183 case FIXED_CST:
1184 pp_c_fixed_constant (this, e);
1185 break;
1187 case STRING_CST:
1188 pp_c_string_literal (this, e);
1189 break;
1191 case COMPLEX_CST:
1192 /* Sometimes, we are confused and we think a complex literal
1193 is a constant. Such thing is a compound literal which
1194 grammatically belongs to postfix-expr production. */
1195 pp_c_compound_literal (this, e);
1196 break;
1198 default:
1199 pp_unsupported_tree (this, e);
1200 break;
1204 /* Pretty-print a string such as an identifier, without changing its
1205 encoding, preceded by whitespace is necessary. */
1207 void
1208 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1210 pp_c_maybe_whitespace (pp);
1211 pp_string (pp, str);
1212 pp->padding = pp_before;
1215 void
1216 c_pretty_printer::translate_string (const char *gmsgid)
1218 if (pp_translate_identifiers (this))
1219 pp_c_ws_string (this, _(gmsgid));
1220 else
1221 pp_c_ws_string (this, gmsgid);
1224 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1225 that need converting to the locale encoding, preceded by whitespace
1226 is necessary. */
1228 void
1229 pp_c_identifier (c_pretty_printer *pp, const char *id)
1231 pp_c_maybe_whitespace (pp);
1232 pp_identifier (pp, id);
1233 pp->padding = pp_before;
1236 /* Pretty-print a C primary-expression.
1237 primary-expression:
1238 identifier
1239 constant
1240 string-literal
1241 ( expression ) */
1243 void
1244 c_pretty_printer::primary_expression (tree e)
1246 switch (TREE_CODE (e))
1248 case VAR_DECL:
1249 case PARM_DECL:
1250 case FIELD_DECL:
1251 case CONST_DECL:
1252 case FUNCTION_DECL:
1253 case LABEL_DECL:
1254 pp_c_tree_decl_identifier (this, e);
1255 break;
1257 case IDENTIFIER_NODE:
1258 pp_c_tree_identifier (this, e);
1259 break;
1261 case ERROR_MARK:
1262 translate_string ("<erroneous-expression>");
1263 break;
1265 case RESULT_DECL:
1266 translate_string ("<return-value>");
1267 break;
1269 case VOID_CST:
1270 case INTEGER_CST:
1271 case REAL_CST:
1272 case FIXED_CST:
1273 case STRING_CST:
1274 constant (e);
1275 break;
1277 case TARGET_EXPR:
1278 pp_c_ws_string (this, "__builtin_memcpy");
1279 pp_c_left_paren (this);
1280 pp_ampersand (this);
1281 primary_expression (TREE_OPERAND (e, 0));
1282 pp_separate_with (this, ',');
1283 pp_ampersand (this);
1284 initializer (TREE_OPERAND (e, 1));
1285 if (TREE_OPERAND (e, 2))
1287 pp_separate_with (this, ',');
1288 expression (TREE_OPERAND (e, 2));
1290 pp_c_right_paren (this);
1291 break;
1293 default:
1294 /* FIXME: Make sure we won't get into an infinite loop. */
1295 pp_c_left_paren (this);
1296 expression (e);
1297 pp_c_right_paren (this);
1298 break;
1302 /* Print out a C initializer -- also support C compound-literals.
1303 initializer:
1304 assignment-expression:
1305 { initializer-list }
1306 { initializer-list , } */
1308 void
1309 c_pretty_printer::initializer (tree e)
1311 if (TREE_CODE (e) == CONSTRUCTOR)
1312 pp_c_brace_enclosed_initializer_list (this, e);
1313 else
1314 expression (e);
1317 /* init-declarator:
1318 declarator:
1319 declarator = initializer */
1321 void
1322 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1324 pp->declarator (t);
1325 /* We don't want to output function definitions here. There are handled
1326 elsewhere (and the syntactic form is bogus anyway). */
1327 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1329 tree init = DECL_INITIAL (t);
1330 /* This C++ bit is handled here because it is easier to do so.
1331 In templates, the C++ parser builds a TREE_LIST for a
1332 direct-initialization; the TREE_PURPOSE is the variable to
1333 initialize and the TREE_VALUE is the initializer. */
1334 if (TREE_CODE (init) == TREE_LIST)
1336 pp_c_left_paren (pp);
1337 pp->expression (TREE_VALUE (init));
1338 pp_right_paren (pp);
1340 else
1342 pp_space (pp);
1343 pp_equal (pp);
1344 pp_space (pp);
1345 pp->initializer (init);
1350 /* initializer-list:
1351 designation(opt) initializer
1352 initializer-list , designation(opt) initializer
1354 designation:
1355 designator-list =
1357 designator-list:
1358 designator
1359 designator-list designator
1361 designator:
1362 [ constant-expression ]
1363 identifier */
1365 static void
1366 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1368 tree type = TREE_TYPE (e);
1369 const enum tree_code code = TREE_CODE (type);
1371 if (TREE_CODE (e) == CONSTRUCTOR)
1373 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1374 return;
1377 switch (code)
1379 case RECORD_TYPE:
1380 case UNION_TYPE:
1381 case ARRAY_TYPE:
1383 tree init = TREE_OPERAND (e, 0);
1384 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1386 if (code == RECORD_TYPE || code == UNION_TYPE)
1388 pp_c_dot (pp);
1389 pp->primary_expression (TREE_PURPOSE (init));
1391 else
1393 pp_c_left_bracket (pp);
1394 if (TREE_PURPOSE (init))
1395 pp->constant (TREE_PURPOSE (init));
1396 pp_c_right_bracket (pp);
1398 pp_c_whitespace (pp);
1399 pp_equal (pp);
1400 pp_c_whitespace (pp);
1401 pp->initializer (TREE_VALUE (init));
1402 if (TREE_CHAIN (init))
1403 pp_separate_with (pp, ',');
1406 return;
1408 case VECTOR_TYPE:
1409 if (TREE_CODE (e) == VECTOR_CST)
1411 unsigned i;
1412 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1414 if (i > 0)
1415 pp_separate_with (pp, ',');
1416 pp->expression (VECTOR_CST_ELT (e, i));
1419 else
1420 break;
1421 return;
1423 case COMPLEX_TYPE:
1424 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1426 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1427 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1428 pp_separate_with (pp, ',');
1429 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1431 else
1432 break;
1433 return;
1435 default:
1436 break;
1439 pp_unsupported_tree (pp, type);
1442 /* Pretty-print a brace-enclosed initializer-list. */
1444 static void
1445 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1447 pp_c_left_brace (pp);
1448 pp_c_initializer_list (pp, l);
1449 pp_c_right_brace (pp);
1453 /* This is a convenient function, used to bridge gap between C and C++
1454 grammars.
1456 id-expression:
1457 identifier */
1459 void
1460 c_pretty_printer::id_expression (tree t)
1462 switch (TREE_CODE (t))
1464 case VAR_DECL:
1465 case PARM_DECL:
1466 case CONST_DECL:
1467 case TYPE_DECL:
1468 case FUNCTION_DECL:
1469 case FIELD_DECL:
1470 case LABEL_DECL:
1471 pp_c_tree_decl_identifier (this, t);
1472 break;
1474 case IDENTIFIER_NODE:
1475 pp_c_tree_identifier (this, t);
1476 break;
1478 default:
1479 pp_unsupported_tree (this, t);
1480 break;
1484 /* postfix-expression:
1485 primary-expression
1486 postfix-expression [ expression ]
1487 postfix-expression ( argument-expression-list(opt) )
1488 postfix-expression . identifier
1489 postfix-expression -> identifier
1490 postfix-expression ++
1491 postfix-expression --
1492 ( type-name ) { initializer-list }
1493 ( type-name ) { initializer-list , } */
1495 void
1496 c_pretty_printer::postfix_expression (tree e)
1498 enum tree_code code = TREE_CODE (e);
1499 switch (code)
1501 case POSTINCREMENT_EXPR:
1502 case POSTDECREMENT_EXPR:
1503 postfix_expression (TREE_OPERAND (e, 0));
1504 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1505 break;
1507 case ARRAY_REF:
1508 postfix_expression (TREE_OPERAND (e, 0));
1509 pp_c_left_bracket (this);
1510 expression (TREE_OPERAND (e, 1));
1511 pp_c_right_bracket (this);
1512 break;
1514 case ARRAY_NOTATION_REF:
1515 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1516 pp_c_left_bracket (this);
1517 expression (ARRAY_NOTATION_START (e));
1518 pp_colon (this);
1519 expression (ARRAY_NOTATION_LENGTH (e));
1520 pp_colon (this);
1521 expression (ARRAY_NOTATION_STRIDE (e));
1522 pp_c_right_bracket (this);
1523 break;
1525 case CALL_EXPR:
1527 call_expr_arg_iterator iter;
1528 tree arg;
1529 postfix_expression (CALL_EXPR_FN (e));
1530 pp_c_left_paren (this);
1531 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1533 expression (arg);
1534 if (more_call_expr_args_p (&iter))
1535 pp_separate_with (this, ',');
1537 pp_c_right_paren (this);
1538 break;
1541 case UNORDERED_EXPR:
1542 pp_c_ws_string (this, flag_isoc99
1543 ? "isunordered"
1544 : "__builtin_isunordered");
1545 goto two_args_fun;
1547 case ORDERED_EXPR:
1548 pp_c_ws_string (this, flag_isoc99
1549 ? "!isunordered"
1550 : "!__builtin_isunordered");
1551 goto two_args_fun;
1553 case UNLT_EXPR:
1554 pp_c_ws_string (this, flag_isoc99
1555 ? "!isgreaterequal"
1556 : "!__builtin_isgreaterequal");
1557 goto two_args_fun;
1559 case UNLE_EXPR:
1560 pp_c_ws_string (this, flag_isoc99
1561 ? "!isgreater"
1562 : "!__builtin_isgreater");
1563 goto two_args_fun;
1565 case UNGT_EXPR:
1566 pp_c_ws_string (this, flag_isoc99
1567 ? "!islessequal"
1568 : "!__builtin_islessequal");
1569 goto two_args_fun;
1571 case UNGE_EXPR:
1572 pp_c_ws_string (this, flag_isoc99
1573 ? "!isless"
1574 : "!__builtin_isless");
1575 goto two_args_fun;
1577 case UNEQ_EXPR:
1578 pp_c_ws_string (this, flag_isoc99
1579 ? "!islessgreater"
1580 : "!__builtin_islessgreater");
1581 goto two_args_fun;
1583 case LTGT_EXPR:
1584 pp_c_ws_string (this, flag_isoc99
1585 ? "islessgreater"
1586 : "__builtin_islessgreater");
1587 goto two_args_fun;
1589 two_args_fun:
1590 pp_c_left_paren (this);
1591 expression (TREE_OPERAND (e, 0));
1592 pp_separate_with (this, ',');
1593 expression (TREE_OPERAND (e, 1));
1594 pp_c_right_paren (this);
1595 break;
1597 case ABS_EXPR:
1598 pp_c_ws_string (this, "__builtin_abs");
1599 pp_c_left_paren (this);
1600 expression (TREE_OPERAND (e, 0));
1601 pp_c_right_paren (this);
1602 break;
1604 case COMPONENT_REF:
1606 tree object = TREE_OPERAND (e, 0);
1607 if (TREE_CODE (object) == INDIRECT_REF)
1609 postfix_expression (TREE_OPERAND (object, 0));
1610 pp_c_arrow (this);
1612 else
1614 postfix_expression (object);
1615 pp_c_dot (this);
1617 expression (TREE_OPERAND (e, 1));
1619 break;
1621 case BIT_FIELD_REF:
1623 tree type = TREE_TYPE (e);
1625 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1626 if (type
1627 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1629 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1630 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1631 if ((bitpos % size) == 0)
1633 pp_c_left_paren (this);
1634 pp_c_left_paren (this);
1635 type_id (type);
1636 pp_c_star (this);
1637 pp_c_right_paren (this);
1638 pp_c_ampersand (this);
1639 expression (TREE_OPERAND (e, 0));
1640 pp_c_right_paren (this);
1641 pp_c_left_bracket (this);
1642 pp_wide_integer (this, bitpos / size);
1643 pp_c_right_bracket (this);
1644 break;
1647 pp_unsupported_tree (this, e);
1649 break;
1651 case MEM_REF:
1652 expression (e);
1653 break;
1655 case COMPLEX_CST:
1656 case VECTOR_CST:
1657 pp_c_compound_literal (this, e);
1658 break;
1660 case COMPLEX_EXPR:
1661 pp_c_complex_expr (this, e);
1662 break;
1664 case COMPOUND_LITERAL_EXPR:
1665 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1666 /* Fall through. */
1667 case CONSTRUCTOR:
1668 initializer (e);
1669 break;
1671 case VA_ARG_EXPR:
1672 pp_c_ws_string (this, "__builtin_va_arg");
1673 pp_c_left_paren (this);
1674 assignment_expression (TREE_OPERAND (e, 0));
1675 pp_separate_with (this, ',');
1676 type_id (TREE_TYPE (e));
1677 pp_c_right_paren (this);
1678 break;
1680 case ADDR_EXPR:
1681 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1683 id_expression (TREE_OPERAND (e, 0));
1684 break;
1686 /* else fall through. */
1688 default:
1689 primary_expression (e);
1690 break;
1694 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1696 void
1697 pp_c_expression_list (c_pretty_printer *pp, tree e)
1699 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1701 pp->expression (TREE_VALUE (e));
1702 if (TREE_CHAIN (e))
1703 pp_separate_with (pp, ',');
1707 /* Print out V, which contains the elements of a constructor. */
1709 void
1710 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1712 unsigned HOST_WIDE_INT ix;
1713 tree value;
1715 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1717 pp->expression (value);
1718 if (ix != vec_safe_length (v) - 1)
1719 pp_separate_with (pp, ',');
1723 /* Print out an expression-list in parens, as if it were the argument
1724 list to a function. */
1726 void
1727 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1729 pp_c_left_paren (pp);
1730 if (t && TREE_CODE (t) == TREE_LIST)
1731 pp_c_expression_list (pp, t);
1732 pp_c_right_paren (pp);
1735 /* unary-expression:
1736 postfix-expression
1737 ++ cast-expression
1738 -- cast-expression
1739 unary-operator cast-expression
1740 sizeof unary-expression
1741 sizeof ( type-id )
1743 unary-operator: one of
1744 * & + - ! ~
1746 GNU extensions.
1747 unary-expression:
1748 __alignof__ unary-expression
1749 __alignof__ ( type-id )
1750 __real__ unary-expression
1751 __imag__ unary-expression */
1753 void
1754 c_pretty_printer::unary_expression (tree e)
1756 enum tree_code code = TREE_CODE (e);
1757 switch (code)
1759 case PREINCREMENT_EXPR:
1760 case PREDECREMENT_EXPR:
1761 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1762 unary_expression (TREE_OPERAND (e, 0));
1763 break;
1765 case ADDR_EXPR:
1766 case INDIRECT_REF:
1767 case NEGATE_EXPR:
1768 case BIT_NOT_EXPR:
1769 case TRUTH_NOT_EXPR:
1770 case CONJ_EXPR:
1771 /* String literal are used by address. */
1772 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1773 pp_ampersand (this);
1774 else if (code == INDIRECT_REF)
1775 pp_c_star (this);
1776 else if (code == NEGATE_EXPR)
1777 pp_minus (this);
1778 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1779 pp_complement (this);
1780 else if (code == TRUTH_NOT_EXPR)
1781 pp_exclamation (this);
1782 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1783 break;
1785 case MEM_REF:
1786 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1787 && integer_zerop (TREE_OPERAND (e, 1)))
1788 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1789 else
1791 pp_c_star (this);
1792 if (!integer_zerop (TREE_OPERAND (e, 1)))
1794 pp_c_left_paren (this);
1795 if (!integer_onep (TYPE_SIZE_UNIT
1796 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1797 pp_c_type_cast (this, ptr_type_node);
1799 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1800 if (!integer_zerop (TREE_OPERAND (e, 1)))
1802 pp_plus (this);
1803 pp_c_integer_constant (this,
1804 fold_convert (ssizetype,
1805 TREE_OPERAND (e, 1)));
1806 pp_c_right_paren (this);
1809 break;
1811 case REALPART_EXPR:
1812 case IMAGPART_EXPR:
1813 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1814 pp_c_whitespace (this);
1815 unary_expression (TREE_OPERAND (e, 0));
1816 break;
1818 default:
1819 postfix_expression (e);
1820 break;
1824 /* cast-expression:
1825 unary-expression
1826 ( type-name ) cast-expression */
1828 void
1829 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1831 switch (TREE_CODE (e))
1833 case FLOAT_EXPR:
1834 case FIX_TRUNC_EXPR:
1835 CASE_CONVERT:
1836 case VIEW_CONVERT_EXPR:
1837 pp_c_type_cast (pp, TREE_TYPE (e));
1838 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1839 break;
1841 default:
1842 pp->unary_expression (e);
1846 /* multiplicative-expression:
1847 cast-expression
1848 multiplicative-expression * cast-expression
1849 multiplicative-expression / cast-expression
1850 multiplicative-expression % cast-expression */
1852 void
1853 c_pretty_printer::multiplicative_expression (tree e)
1855 enum tree_code code = TREE_CODE (e);
1856 switch (code)
1858 case MULT_EXPR:
1859 case TRUNC_DIV_EXPR:
1860 case TRUNC_MOD_EXPR:
1861 multiplicative_expression (TREE_OPERAND (e, 0));
1862 pp_c_whitespace (this);
1863 if (code == MULT_EXPR)
1864 pp_c_star (this);
1865 else if (code == TRUNC_DIV_EXPR)
1866 pp_slash (this);
1867 else
1868 pp_modulo (this);
1869 pp_c_whitespace (this);
1870 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1871 break;
1873 default:
1874 pp_c_cast_expression (this, e);
1875 break;
1879 /* additive-expression:
1880 multiplicative-expression
1881 additive-expression + multiplicative-expression
1882 additive-expression - multiplicative-expression */
1884 static void
1885 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1887 enum tree_code code = TREE_CODE (e);
1888 switch (code)
1890 case POINTER_PLUS_EXPR:
1891 case PLUS_EXPR:
1892 case MINUS_EXPR:
1893 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1894 pp_c_whitespace (pp);
1895 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1896 pp_plus (pp);
1897 else
1898 pp_minus (pp);
1899 pp_c_whitespace (pp);
1900 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1901 break;
1903 default:
1904 pp->multiplicative_expression (e);
1905 break;
1909 /* additive-expression:
1910 additive-expression
1911 shift-expression << additive-expression
1912 shift-expression >> additive-expression */
1914 static void
1915 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1917 enum tree_code code = TREE_CODE (e);
1918 switch (code)
1920 case LSHIFT_EXPR:
1921 case RSHIFT_EXPR:
1922 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1923 pp_c_whitespace (pp);
1924 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1925 pp_c_whitespace (pp);
1926 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1927 break;
1929 default:
1930 pp_c_additive_expression (pp, e);
1934 /* relational-expression:
1935 shift-expression
1936 relational-expression < shift-expression
1937 relational-expression > shift-expression
1938 relational-expression <= shift-expression
1939 relational-expression >= shift-expression */
1941 static void
1942 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1944 enum tree_code code = TREE_CODE (e);
1945 switch (code)
1947 case LT_EXPR:
1948 case GT_EXPR:
1949 case LE_EXPR:
1950 case GE_EXPR:
1951 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1952 pp_c_whitespace (pp);
1953 if (code == LT_EXPR)
1954 pp_less (pp);
1955 else if (code == GT_EXPR)
1956 pp_greater (pp);
1957 else if (code == LE_EXPR)
1958 pp_less_equal (pp);
1959 else if (code == GE_EXPR)
1960 pp_greater_equal (pp);
1961 pp_c_whitespace (pp);
1962 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1963 break;
1965 default:
1966 pp_c_shift_expression (pp, e);
1967 break;
1971 /* equality-expression:
1972 relational-expression
1973 equality-expression == relational-expression
1974 equality-equality != relational-expression */
1976 static void
1977 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1979 enum tree_code code = TREE_CODE (e);
1980 switch (code)
1982 case EQ_EXPR:
1983 case NE_EXPR:
1984 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1985 pp_c_whitespace (pp);
1986 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1987 pp_c_whitespace (pp);
1988 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1989 break;
1991 default:
1992 pp_c_relational_expression (pp, e);
1993 break;
1997 /* AND-expression:
1998 equality-expression
1999 AND-expression & equality-equality */
2001 static void
2002 pp_c_and_expression (c_pretty_printer *pp, tree e)
2004 if (TREE_CODE (e) == BIT_AND_EXPR)
2006 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2007 pp_c_whitespace (pp);
2008 pp_ampersand (pp);
2009 pp_c_whitespace (pp);
2010 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2012 else
2013 pp_c_equality_expression (pp, e);
2016 /* exclusive-OR-expression:
2017 AND-expression
2018 exclusive-OR-expression ^ AND-expression */
2020 static void
2021 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2023 if (TREE_CODE (e) == BIT_XOR_EXPR
2024 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2026 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2027 if (TREE_CODE (e) == BIT_XOR_EXPR)
2028 pp_c_maybe_whitespace (pp);
2029 else
2030 pp_c_whitespace (pp);
2031 pp_carret (pp);
2032 pp_c_whitespace (pp);
2033 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2035 else
2036 pp_c_and_expression (pp, e);
2039 /* inclusive-OR-expression:
2040 exclusive-OR-expression
2041 inclusive-OR-expression | exclusive-OR-expression */
2043 static void
2044 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2046 if (TREE_CODE (e) == BIT_IOR_EXPR)
2048 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2049 pp_c_whitespace (pp);
2050 pp_bar (pp);
2051 pp_c_whitespace (pp);
2052 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2054 else
2055 pp_c_exclusive_or_expression (pp, e);
2058 /* logical-AND-expression:
2059 inclusive-OR-expression
2060 logical-AND-expression && inclusive-OR-expression */
2062 static void
2063 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2065 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2066 || TREE_CODE (e) == TRUTH_AND_EXPR)
2068 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2069 pp_c_whitespace (pp);
2070 pp_ampersand_ampersand (pp);
2071 pp_c_whitespace (pp);
2072 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2074 else
2075 pp_c_inclusive_or_expression (pp, e);
2078 /* logical-OR-expression:
2079 logical-AND-expression
2080 logical-OR-expression || logical-AND-expression */
2082 void
2083 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2085 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2086 || TREE_CODE (e) == TRUTH_OR_EXPR)
2088 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2089 pp_c_whitespace (pp);
2090 pp_bar_bar (pp);
2091 pp_c_whitespace (pp);
2092 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2094 else
2095 pp_c_logical_and_expression (pp, e);
2098 /* conditional-expression:
2099 logical-OR-expression
2100 logical-OR-expression ? expression : conditional-expression */
2102 void
2103 c_pretty_printer::conditional_expression (tree e)
2105 if (TREE_CODE (e) == COND_EXPR)
2107 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2108 pp_c_whitespace (this);
2109 pp_question (this);
2110 pp_c_whitespace (this);
2111 expression (TREE_OPERAND (e, 1));
2112 pp_c_whitespace (this);
2113 pp_colon (this);
2114 pp_c_whitespace (this);
2115 conditional_expression (TREE_OPERAND (e, 2));
2117 else
2118 pp_c_logical_or_expression (this, e);
2122 /* assignment-expression:
2123 conditional-expression
2124 unary-expression assignment-operator assignment-expression
2126 assignment-expression: one of
2127 = *= /= %= += -= >>= <<= &= ^= |= */
2129 void
2130 c_pretty_printer::assignment_expression (tree e)
2132 if (TREE_CODE (e) == MODIFY_EXPR
2133 || TREE_CODE (e) == INIT_EXPR)
2135 unary_expression (TREE_OPERAND (e, 0));
2136 pp_c_whitespace (this);
2137 pp_equal (this);
2138 pp_space (this);
2139 expression (TREE_OPERAND (e, 1));
2141 else
2142 conditional_expression (e);
2145 /* expression:
2146 assignment-expression
2147 expression , assignment-expression
2149 Implementation note: instead of going through the usual recursion
2150 chain, I take the liberty of dispatching nodes to the appropriate
2151 functions. This makes some redundancy, but it worths it. That also
2152 prevents a possible infinite recursion between primary_expression ()
2153 and expression (). */
2155 void
2156 c_pretty_printer::expression (tree e)
2158 switch (TREE_CODE (e))
2160 case VOID_CST:
2161 pp_c_void_constant (this);
2162 break;
2164 case INTEGER_CST:
2165 pp_c_integer_constant (this, e);
2166 break;
2168 case REAL_CST:
2169 pp_c_floating_constant (this, e);
2170 break;
2172 case FIXED_CST:
2173 pp_c_fixed_constant (this, e);
2174 break;
2176 case STRING_CST:
2177 pp_c_string_literal (this, e);
2178 break;
2180 case IDENTIFIER_NODE:
2181 case FUNCTION_DECL:
2182 case VAR_DECL:
2183 case CONST_DECL:
2184 case PARM_DECL:
2185 case RESULT_DECL:
2186 case FIELD_DECL:
2187 case LABEL_DECL:
2188 case ERROR_MARK:
2189 primary_expression (e);
2190 break;
2192 case SSA_NAME:
2193 if (SSA_NAME_VAR (e)
2194 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2195 expression (SSA_NAME_VAR (e));
2196 else
2197 translate_string ("<unknown>");
2198 break;
2200 case POSTINCREMENT_EXPR:
2201 case POSTDECREMENT_EXPR:
2202 case ARRAY_REF:
2203 case ARRAY_NOTATION_REF:
2204 case CALL_EXPR:
2205 case COMPONENT_REF:
2206 case BIT_FIELD_REF:
2207 case COMPLEX_CST:
2208 case COMPLEX_EXPR:
2209 case VECTOR_CST:
2210 case ORDERED_EXPR:
2211 case UNORDERED_EXPR:
2212 case LTGT_EXPR:
2213 case UNEQ_EXPR:
2214 case UNLE_EXPR:
2215 case UNLT_EXPR:
2216 case UNGE_EXPR:
2217 case UNGT_EXPR:
2218 case ABS_EXPR:
2219 case CONSTRUCTOR:
2220 case COMPOUND_LITERAL_EXPR:
2221 case VA_ARG_EXPR:
2222 postfix_expression (e);
2223 break;
2225 case CONJ_EXPR:
2226 case ADDR_EXPR:
2227 case INDIRECT_REF:
2228 case MEM_REF:
2229 case NEGATE_EXPR:
2230 case BIT_NOT_EXPR:
2231 case TRUTH_NOT_EXPR:
2232 case PREINCREMENT_EXPR:
2233 case PREDECREMENT_EXPR:
2234 case REALPART_EXPR:
2235 case IMAGPART_EXPR:
2236 unary_expression (e);
2237 break;
2239 case FLOAT_EXPR:
2240 case FIX_TRUNC_EXPR:
2241 CASE_CONVERT:
2242 case VIEW_CONVERT_EXPR:
2243 pp_c_cast_expression (this, e);
2244 break;
2246 case MULT_EXPR:
2247 case TRUNC_MOD_EXPR:
2248 case TRUNC_DIV_EXPR:
2249 multiplicative_expression (e);
2250 break;
2252 case LSHIFT_EXPR:
2253 case RSHIFT_EXPR:
2254 pp_c_shift_expression (this, e);
2255 break;
2257 case LT_EXPR:
2258 case GT_EXPR:
2259 case LE_EXPR:
2260 case GE_EXPR:
2261 pp_c_relational_expression (this, e);
2262 break;
2264 case BIT_AND_EXPR:
2265 pp_c_and_expression (this, e);
2266 break;
2268 case BIT_XOR_EXPR:
2269 case TRUTH_XOR_EXPR:
2270 pp_c_exclusive_or_expression (this, e);
2271 break;
2273 case BIT_IOR_EXPR:
2274 pp_c_inclusive_or_expression (this, e);
2275 break;
2277 case TRUTH_ANDIF_EXPR:
2278 case TRUTH_AND_EXPR:
2279 pp_c_logical_and_expression (this, e);
2280 break;
2282 case TRUTH_ORIF_EXPR:
2283 case TRUTH_OR_EXPR:
2284 pp_c_logical_or_expression (this, e);
2285 break;
2287 case EQ_EXPR:
2288 case NE_EXPR:
2289 pp_c_equality_expression (this, e);
2290 break;
2292 case COND_EXPR:
2293 conditional_expression (e);
2294 break;
2296 case POINTER_PLUS_EXPR:
2297 case PLUS_EXPR:
2298 case MINUS_EXPR:
2299 pp_c_additive_expression (this, e);
2300 break;
2302 case MODIFY_EXPR:
2303 case INIT_EXPR:
2304 assignment_expression (e);
2305 break;
2307 case COMPOUND_EXPR:
2308 pp_c_left_paren (this);
2309 expression (TREE_OPERAND (e, 0));
2310 pp_separate_with (this, ',');
2311 assignment_expression (TREE_OPERAND (e, 1));
2312 pp_c_right_paren (this);
2313 break;
2315 case NON_LVALUE_EXPR:
2316 case SAVE_EXPR:
2317 expression (TREE_OPERAND (e, 0));
2318 break;
2320 case TARGET_EXPR:
2321 postfix_expression (TREE_OPERAND (e, 1));
2322 break;
2324 case BIND_EXPR:
2325 case GOTO_EXPR:
2326 /* We don't yet have a way of dumping statements in a
2327 human-readable format. */
2328 pp_string (this, "({...})");
2329 break;
2331 case C_MAYBE_CONST_EXPR:
2332 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2333 break;
2335 default:
2336 pp_unsupported_tree (this, e);
2337 break;
2343 /* Statements. */
2345 void
2346 c_pretty_printer::statement (tree stmt)
2348 if (stmt == NULL)
2349 return;
2351 if (pp_needs_newline (this))
2352 pp_newline_and_indent (this, 0);
2354 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2358 /* Initialize the PRETTY-PRINTER for handling C codes. */
2360 c_pretty_printer::c_pretty_printer ()
2361 : pretty_printer (),
2362 offset_list (),
2363 flags ()
2365 type_specifier_seq = pp_c_specifier_qualifier_list;
2366 ptr_operator = pp_c_pointer;
2367 parameter_list = pp_c_parameter_type_list;
2371 /* Print the tree T in full, on file FILE. */
2373 void
2374 print_c_tree (FILE *file, tree t)
2376 c_pretty_printer pp;
2378 pp_needs_newline (&pp) = true;
2379 pp.buffer->stream = file;
2380 pp.statement (t);
2381 pp_newline_and_flush (&pp);
2384 /* Print the tree T in full, on stderr. */
2386 DEBUG_FUNCTION void
2387 debug_c_tree (tree t)
2389 print_c_tree (stderr, t);
2390 fputc ('\n', stderr);
2393 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2394 up of T's memory address. */
2396 void
2397 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2399 const char *name;
2401 gcc_assert (DECL_P (t));
2403 if (DECL_NAME (t))
2404 name = IDENTIFIER_POINTER (DECL_NAME (t));
2405 else
2407 static char xname[8];
2408 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2409 name = xname;
2412 pp_c_identifier (pp, name);