2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob482509b34c50b94c31d4bb28818bbe7dbd5b56a5
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2015 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 "alias.h"
26 #include "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "stor-layout.h"
30 #include "attribs.h"
31 #include "intl.h"
32 #include "c-pretty-print.h"
33 #include "tree-pretty-print.h"
34 #include "tree-iterator.h"
35 #include "diagnostic.h"
36 #include "wide-int-print.h"
38 /* The pretty-printer code is primarily designed to closely follow
39 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
40 codes we used to have in the past. Following a structured
41 approach (preferably the official grammars) is believed to make it
42 much easier to add extensions and nifty pretty-printing effects that
43 takes expression or declaration contexts into account. */
46 #define pp_c_maybe_whitespace(PP) \
47 do { \
48 if ((PP)->padding == pp_before) \
49 pp_c_whitespace (PP); \
50 } while (0)
52 /* literal */
53 static void pp_c_char (c_pretty_printer *, int);
55 /* postfix-expression */
56 static void pp_c_initializer_list (c_pretty_printer *, tree);
57 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
59 static void pp_c_additive_expression (c_pretty_printer *, tree);
60 static void pp_c_shift_expression (c_pretty_printer *, tree);
61 static void pp_c_relational_expression (c_pretty_printer *, tree);
62 static void pp_c_equality_expression (c_pretty_printer *, tree);
63 static void pp_c_and_expression (c_pretty_printer *, tree);
64 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
65 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
66 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
68 /* declarations. */
71 /* Helper functions. */
73 void
74 pp_c_whitespace (c_pretty_printer *pp)
76 pp_space (pp);
77 pp->padding = pp_none;
80 void
81 pp_c_left_paren (c_pretty_printer *pp)
83 pp_left_paren (pp);
84 pp->padding = pp_none;
87 void
88 pp_c_right_paren (c_pretty_printer *pp)
90 pp_right_paren (pp);
91 pp->padding = pp_none;
94 void
95 pp_c_left_brace (c_pretty_printer *pp)
97 pp_left_brace (pp);
98 pp->padding = pp_none;
101 void
102 pp_c_right_brace (c_pretty_printer *pp)
104 pp_right_brace (pp);
105 pp->padding = pp_none;
108 void
109 pp_c_left_bracket (c_pretty_printer *pp)
111 pp_left_bracket (pp);
112 pp->padding = pp_none;
115 void
116 pp_c_right_bracket (c_pretty_printer *pp)
118 pp_right_bracket (pp);
119 pp->padding = pp_none;
122 void
123 pp_c_dot (c_pretty_printer *pp)
125 pp_dot (pp);
126 pp->padding = pp_none;
129 void
130 pp_c_ampersand (c_pretty_printer *pp)
132 pp_ampersand (pp);
133 pp->padding = pp_none;
136 void
137 pp_c_star (c_pretty_printer *pp)
139 pp_star (pp);
140 pp->padding = pp_none;
143 void
144 pp_c_arrow (c_pretty_printer *pp)
146 pp_arrow (pp);
147 pp->padding = pp_none;
150 void
151 pp_c_semicolon (c_pretty_printer *pp)
153 pp_semicolon (pp);
154 pp->padding = pp_none;
157 void
158 pp_c_complement (c_pretty_printer *pp)
160 pp_complement (pp);
161 pp->padding = pp_none;
164 void
165 pp_c_exclamation (c_pretty_printer *pp)
167 pp_exclamation (pp);
168 pp->padding = pp_none;
171 /* Print out the external representation of QUALIFIERS. */
173 void
174 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
176 const char *p = pp_last_position_in_text (pp);
177 bool previous = false;
179 if (!qualifiers)
180 return;
182 /* The C programming language does not have references, but it is much
183 simpler to handle those here rather than going through the same
184 logic in the C++ pretty-printer. */
185 if (p != NULL && (*p == '*' || *p == '&'))
186 pp_c_whitespace (pp);
188 if (qualifiers & TYPE_QUAL_ATOMIC)
190 pp_c_ws_string (pp, "_Atomic");
191 previous = true;
194 if (qualifiers & TYPE_QUAL_CONST)
196 if (previous)
197 pp_c_whitespace (pp);
198 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
199 previous = true;
202 if (qualifiers & TYPE_QUAL_VOLATILE)
204 if (previous)
205 pp_c_whitespace (pp);
206 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
207 previous = true;
210 if (qualifiers & TYPE_QUAL_RESTRICT)
212 if (previous)
213 pp_c_whitespace (pp);
214 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
215 ? "restrict" : "__restrict__"));
219 /* Pretty-print T using the type-cast notation '( type-name )'. */
221 static void
222 pp_c_type_cast (c_pretty_printer *pp, tree t)
224 pp_c_left_paren (pp);
225 pp->type_id (t);
226 pp_c_right_paren (pp);
229 /* We're about to pretty-print a pointer type as indicated by T.
230 Output a whitespace, if needed, preparing for subsequent output. */
232 void
233 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
235 if (POINTER_TYPE_P (t))
237 tree pointee = strip_pointer_operator (TREE_TYPE (t));
238 if (TREE_CODE (pointee) != ARRAY_TYPE
239 && TREE_CODE (pointee) != FUNCTION_TYPE)
240 pp_c_whitespace (pp);
245 /* Declarations. */
247 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
248 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
249 of its type. Take care of possible extensions.
251 type-qualifier-list:
252 type-qualifier
253 type-qualifier-list type-qualifier
255 type-qualifier:
256 const
257 restrict -- C99
258 __restrict__ -- GNU C
259 address-space-qualifier -- GNU C
260 volatile
261 _Atomic -- C11
263 address-space-qualifier:
264 identifier -- GNU C */
266 void
267 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
269 int qualifiers;
271 if (!t || t == error_mark_node)
272 return;
274 if (!TYPE_P (t))
275 t = TREE_TYPE (t);
277 qualifiers = TYPE_QUALS (t);
278 pp_c_cv_qualifiers (pp, qualifiers,
279 TREE_CODE (t) == FUNCTION_TYPE);
281 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
283 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
284 pp_c_identifier (pp, as);
288 /* pointer:
289 * type-qualifier-list(opt)
290 * type-qualifier-list(opt) pointer */
292 static void
293 pp_c_pointer (c_pretty_printer *pp, tree t)
295 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
296 t = TREE_TYPE (t);
297 switch (TREE_CODE (t))
299 case POINTER_TYPE:
300 /* It is easier to handle C++ reference types here. */
301 case REFERENCE_TYPE:
302 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
303 pp_c_pointer (pp, TREE_TYPE (t));
304 if (TREE_CODE (t) == POINTER_TYPE)
305 pp_c_star (pp);
306 else
307 pp_c_ampersand (pp);
308 pp_c_type_qualifier_list (pp, t);
309 break;
311 /* ??? This node is now in GENERIC and so shouldn't be here. But
312 we'll fix that later. */
313 case DECL_EXPR:
314 pp->declaration (DECL_EXPR_DECL (t));
315 pp_needs_newline (pp) = true;
316 break;
318 default:
319 pp_unsupported_tree (pp, t);
323 /* simple-type-specifier:
324 type-specifier
326 type-specifier:
327 void
328 char
329 short
331 long
332 float
333 double
334 signed
335 unsigned
336 _Bool -- C99
337 _Complex -- C99
338 _Imaginary -- C99
339 struct-or-union-specifier
340 enum-specifier
341 typedef-name.
343 GNU extensions.
344 simple-type-specifier:
345 __complex__
346 __vector__ */
348 void
349 c_pretty_printer::simple_type_specifier (tree t)
351 const enum tree_code code = TREE_CODE (t);
352 switch (code)
354 case ERROR_MARK:
355 translate_string ("<type-error>");
356 break;
358 case IDENTIFIER_NODE:
359 pp_c_identifier (this, IDENTIFIER_POINTER (t));
360 break;
362 case VOID_TYPE:
363 case BOOLEAN_TYPE:
364 case INTEGER_TYPE:
365 case REAL_TYPE:
366 case FIXED_POINT_TYPE:
367 if (TYPE_NAME (t))
369 t = TYPE_NAME (t);
370 simple_type_specifier (t);
372 else
374 int prec = TYPE_PRECISION (t);
375 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
376 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
377 else
378 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
379 if (TYPE_NAME (t))
381 simple_type_specifier (t);
382 if (TYPE_PRECISION (t) != prec)
384 pp_colon (this);
385 pp_decimal_int (this, prec);
388 else
390 switch (code)
392 case INTEGER_TYPE:
393 translate_string (TYPE_UNSIGNED (t)
394 ? "<unnamed-unsigned:"
395 : "<unnamed-signed:");
396 break;
397 case REAL_TYPE:
398 translate_string ("<unnamed-float:");
399 break;
400 case FIXED_POINT_TYPE:
401 translate_string ("<unnamed-fixed:");
402 break;
403 default:
404 gcc_unreachable ();
406 pp_decimal_int (this, prec);
407 pp_greater (this);
410 break;
412 case TYPE_DECL:
413 if (DECL_NAME (t))
414 id_expression (t);
415 else
416 translate_string ("<typedef-error>");
417 break;
419 case UNION_TYPE:
420 case RECORD_TYPE:
421 case ENUMERAL_TYPE:
422 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
423 /* Don't decorate the type if this is a typedef name. */;
424 else if (code == UNION_TYPE)
425 pp_c_ws_string (this, "union");
426 else if (code == RECORD_TYPE)
427 pp_c_ws_string (this, "struct");
428 else if (code == ENUMERAL_TYPE)
429 pp_c_ws_string (this, "enum");
430 else
431 translate_string ("<tag-error>");
433 if (TYPE_NAME (t))
434 id_expression (TYPE_NAME (t));
435 else
436 translate_string ("<anonymous>");
437 break;
439 default:
440 pp_unsupported_tree (this, t);
441 break;
445 /* specifier-qualifier-list:
446 type-specifier specifier-qualifier-list-opt
447 type-qualifier specifier-qualifier-list-opt
450 Implementation note: Because of the non-linearities in array or
451 function declarations, this routine prints not just the
452 specifier-qualifier-list of such entities or types of such entities,
453 but also the 'pointer' production part of their declarators. The
454 remaining part is done by declarator() or abstract_declarator(). */
456 void
457 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
459 const enum tree_code code = TREE_CODE (t);
461 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
462 pp_c_type_qualifier_list (pp, t);
463 switch (code)
465 case REFERENCE_TYPE:
466 case POINTER_TYPE:
468 /* Get the types-specifier of this type. */
469 tree pointee = strip_pointer_operator (TREE_TYPE (t));
470 pp_c_specifier_qualifier_list (pp, pointee);
471 if (TREE_CODE (pointee) == ARRAY_TYPE
472 || TREE_CODE (pointee) == FUNCTION_TYPE)
474 pp_c_whitespace (pp);
475 pp_c_left_paren (pp);
476 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
478 else if (!c_dialect_cxx ())
479 pp_c_whitespace (pp);
480 pp_ptr_operator (pp, t);
482 break;
484 case FUNCTION_TYPE:
485 case ARRAY_TYPE:
486 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
487 break;
489 case VECTOR_TYPE:
490 case COMPLEX_TYPE:
491 if (code == COMPLEX_TYPE)
492 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
493 ? "_Complex" : "__complex__"));
494 else if (code == VECTOR_TYPE)
496 pp_c_ws_string (pp, "__vector");
497 pp_c_left_paren (pp);
498 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
499 pp_c_right_paren (pp);
500 pp_c_whitespace (pp);
502 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
503 break;
505 default:
506 pp->simple_type_specifier (t);
507 break;
509 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
510 pp_c_type_qualifier_list (pp, t);
513 /* parameter-type-list:
514 parameter-list
515 parameter-list , ...
517 parameter-list:
518 parameter-declaration
519 parameter-list , parameter-declaration
521 parameter-declaration:
522 declaration-specifiers declarator
523 declaration-specifiers abstract-declarator(opt) */
525 void
526 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
528 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
529 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
530 pp_c_left_paren (pp);
531 if (parms == void_list_node)
532 pp_c_ws_string (pp, "void");
533 else
535 bool first = true;
536 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
538 if (!first)
539 pp_separate_with (pp, ',');
540 first = false;
541 pp->declaration_specifiers
542 (want_parm_decl ? parms : TREE_VALUE (parms));
543 if (want_parm_decl)
544 pp->declarator (parms);
545 else
546 pp->abstract_declarator (TREE_VALUE (parms));
549 pp_c_right_paren (pp);
552 /* abstract-declarator:
553 pointer
554 pointer(opt) direct-abstract-declarator */
556 void
557 c_pretty_printer::abstract_declarator (tree t)
559 if (TREE_CODE (t) == POINTER_TYPE)
561 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
562 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
563 pp_c_right_paren (this);
564 t = TREE_TYPE (t);
567 direct_abstract_declarator (t);
570 /* direct-abstract-declarator:
571 ( abstract-declarator )
572 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
573 direct-abstract-declarator(opt) [ * ]
574 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
576 void
577 c_pretty_printer::direct_abstract_declarator (tree t)
579 switch (TREE_CODE (t))
581 case POINTER_TYPE:
582 abstract_declarator (t);
583 break;
585 case FUNCTION_TYPE:
586 pp_c_parameter_type_list (this, t);
587 direct_abstract_declarator (TREE_TYPE (t));
588 break;
590 case ARRAY_TYPE:
591 pp_c_left_bracket (this);
592 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
594 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
595 tree type = TREE_TYPE (maxval);
597 if (tree_fits_shwi_p (maxval))
598 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
599 else
600 expression (fold_build2 (PLUS_EXPR, type, maxval,
601 build_int_cst (type, 1)));
603 pp_c_right_bracket (this);
604 direct_abstract_declarator (TREE_TYPE (t));
605 break;
607 case IDENTIFIER_NODE:
608 case VOID_TYPE:
609 case BOOLEAN_TYPE:
610 case INTEGER_TYPE:
611 case REAL_TYPE:
612 case FIXED_POINT_TYPE:
613 case ENUMERAL_TYPE:
614 case RECORD_TYPE:
615 case UNION_TYPE:
616 case VECTOR_TYPE:
617 case COMPLEX_TYPE:
618 case TYPE_DECL:
619 break;
621 default:
622 pp_unsupported_tree (this, t);
623 break;
627 /* type-name:
628 specifier-qualifier-list abstract-declarator(opt) */
630 void
631 c_pretty_printer::type_id (tree t)
633 pp_c_specifier_qualifier_list (this, t);
634 abstract_declarator (t);
637 /* storage-class-specifier:
638 typedef
639 extern
640 static
641 auto
642 register */
644 void
645 c_pretty_printer::storage_class_specifier (tree t)
647 if (TREE_CODE (t) == TYPE_DECL)
648 pp_c_ws_string (this, "typedef");
649 else if (DECL_P (t))
651 if (DECL_REGISTER (t))
652 pp_c_ws_string (this, "register");
653 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
654 pp_c_ws_string (this, "static");
658 /* function-specifier:
659 inline */
661 void
662 c_pretty_printer::function_specifier (tree t)
664 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
665 pp_c_ws_string (this, "inline");
668 /* declaration-specifiers:
669 storage-class-specifier declaration-specifiers(opt)
670 type-specifier declaration-specifiers(opt)
671 type-qualifier declaration-specifiers(opt)
672 function-specifier declaration-specifiers(opt) */
674 void
675 c_pretty_printer::declaration_specifiers (tree t)
677 storage_class_specifier (t);
678 function_specifier (t);
679 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
682 /* direct-declarator
683 identifier
684 ( declarator )
685 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
686 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
687 direct-declarator [ type-qualifier-list static assignment-expression ]
688 direct-declarator [ type-qualifier-list * ]
689 direct-declarator ( parameter-type-list )
690 direct-declarator ( identifier-list(opt) ) */
692 void
693 c_pretty_printer::direct_declarator (tree t)
695 switch (TREE_CODE (t))
697 case VAR_DECL:
698 case PARM_DECL:
699 case TYPE_DECL:
700 case FIELD_DECL:
701 case LABEL_DECL:
702 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
703 pp_c_tree_decl_identifier (this, t);
704 break;
706 case ARRAY_TYPE:
707 case POINTER_TYPE:
708 abstract_declarator (TREE_TYPE (t));
709 break;
711 case FUNCTION_TYPE:
712 pp_parameter_list (this, t);
713 abstract_declarator (TREE_TYPE (t));
714 break;
716 case FUNCTION_DECL:
717 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
718 pp_c_tree_decl_identifier (this, t);
719 if (flags & pp_c_flag_abstract)
720 abstract_declarator (TREE_TYPE (t));
721 else
723 pp_parameter_list (this, t);
724 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
726 break;
728 case INTEGER_TYPE:
729 case REAL_TYPE:
730 case FIXED_POINT_TYPE:
731 case ENUMERAL_TYPE:
732 case UNION_TYPE:
733 case RECORD_TYPE:
734 break;
736 default:
737 pp_unsupported_tree (this, t);
738 break;
743 /* declarator:
744 pointer(opt) direct-declarator */
746 void
747 c_pretty_printer::declarator (tree t)
749 switch (TREE_CODE (t))
751 case INTEGER_TYPE:
752 case REAL_TYPE:
753 case FIXED_POINT_TYPE:
754 case ENUMERAL_TYPE:
755 case UNION_TYPE:
756 case RECORD_TYPE:
757 break;
759 case VAR_DECL:
760 case PARM_DECL:
761 case FIELD_DECL:
762 case ARRAY_TYPE:
763 case FUNCTION_TYPE:
764 case FUNCTION_DECL:
765 case TYPE_DECL:
766 direct_declarator (t);
767 break;
770 default:
771 pp_unsupported_tree (this, t);
772 break;
776 /* declaration:
777 declaration-specifiers init-declarator-list(opt) ; */
779 void
780 c_pretty_printer::declaration (tree t)
782 declaration_specifiers (t);
783 pp_c_init_declarator (this, t);
786 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
788 void
789 pp_c_attributes (c_pretty_printer *pp, tree attributes)
791 if (attributes == NULL_TREE)
792 return;
794 pp_c_ws_string (pp, "__attribute__");
795 pp_c_left_paren (pp);
796 pp_c_left_paren (pp);
797 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
799 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
800 if (TREE_VALUE (attributes))
801 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
803 if (TREE_CHAIN (attributes))
804 pp_separate_with (pp, ',');
806 pp_c_right_paren (pp);
807 pp_c_right_paren (pp);
810 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
811 marked to be displayed on disgnostic. */
813 void
814 pp_c_attributes_display (c_pretty_printer *pp, tree a)
816 bool is_first = true;
818 if (a == NULL_TREE)
819 return;
821 for (; a != NULL_TREE; a = TREE_CHAIN (a))
823 const struct attribute_spec *as;
824 as = lookup_attribute_spec (TREE_PURPOSE (a));
825 if (!as || as->affects_type_identity == false)
826 continue;
827 if (is_first)
829 pp_c_ws_string (pp, "__attribute__");
830 pp_c_left_paren (pp);
831 pp_c_left_paren (pp);
832 is_first = false;
834 else
836 pp_separate_with (pp, ',');
838 pp_tree_identifier (pp, TREE_PURPOSE (a));
839 if (TREE_VALUE (a))
840 pp_c_call_argument_list (pp, TREE_VALUE (a));
843 if (!is_first)
845 pp_c_right_paren (pp);
846 pp_c_right_paren (pp);
847 pp_c_whitespace (pp);
851 /* function-definition:
852 declaration-specifiers declarator compound-statement */
854 void
855 pp_c_function_definition (c_pretty_printer *pp, tree t)
857 pp->declaration_specifiers (t);
858 pp->declarator (t);
859 pp_needs_newline (pp) = true;
860 pp->statement (DECL_SAVED_TREE (t));
861 pp_newline_and_flush (pp);
865 /* Expressions. */
867 /* Print out a c-char. This is called solely for characters which are
868 in the *target* execution character set. We ought to convert them
869 back to the *host* execution character set before printing, but we
870 have no way to do this at present. A decent compromise is to print
871 all characters as if they were in the host execution character set,
872 and not attempt to recover any named escape characters, but render
873 all unprintables as octal escapes. If the host and target character
874 sets are the same, this produces relatively readable output. If they
875 are not the same, strings may appear as gibberish, but that's okay
876 (in fact, it may well be what the reader wants, e.g. if they are looking
877 to see if conversion to the target character set happened correctly).
879 A special case: we need to prefix \, ", and ' with backslashes. It is
880 correct to do so for the *host*'s \, ", and ', because the rest of the
881 file appears in the host character set. */
883 static void
884 pp_c_char (c_pretty_printer *pp, int c)
886 if (ISPRINT (c))
888 switch (c)
890 case '\\': pp_string (pp, "\\\\"); break;
891 case '\'': pp_string (pp, "\\\'"); break;
892 case '\"': pp_string (pp, "\\\""); break;
893 default: pp_character (pp, c);
896 else
897 pp_scalar (pp, "\\%03o", (unsigned) c);
900 /* Print out a STRING literal. */
902 void
903 pp_c_string_literal (c_pretty_printer *pp, tree s)
905 const char *p = TREE_STRING_POINTER (s);
906 int n = TREE_STRING_LENGTH (s) - 1;
907 int i;
908 pp_doublequote (pp);
909 for (i = 0; i < n; ++i)
910 pp_c_char (pp, p[i]);
911 pp_doublequote (pp);
914 /* Pretty-print a VOID_CST (void_node). */
916 static void
917 pp_c_void_constant (c_pretty_printer *pp)
919 pp_c_type_cast (pp, void_type_node);
920 pp_string (pp, "0");
923 /* Pretty-print an INTEGER literal. */
925 static void
926 pp_c_integer_constant (c_pretty_printer *pp, tree i)
928 int idx;
930 /* We are going to compare the type of I to other types using
931 pointer comparison so we need to use its canonical type. */
932 tree type =
933 TYPE_CANONICAL (TREE_TYPE (i))
934 ? TYPE_CANONICAL (TREE_TYPE (i))
935 : TREE_TYPE (i);
937 if (tree_fits_shwi_p (i))
938 pp_wide_integer (pp, tree_to_shwi (i));
939 else if (tree_fits_uhwi_p (i))
940 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
941 else
943 wide_int wi = i;
945 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
947 pp_minus (pp);
948 wi = -wi;
950 print_hex (wi, pp_buffer (pp)->digit_buffer);
951 pp_string (pp, pp_buffer (pp)->digit_buffer);
953 if (TYPE_UNSIGNED (type))
954 pp_character (pp, 'u');
955 if (type == long_integer_type_node || type == long_unsigned_type_node)
956 pp_character (pp, 'l');
957 else if (type == long_long_integer_type_node
958 || type == long_long_unsigned_type_node)
959 pp_string (pp, "ll");
960 else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++)
961 if (int_n_enabled_p[idx])
963 char buf[2+20];
964 if (type == int_n_trees[idx].signed_type
965 || type == int_n_trees[idx].unsigned_type)
967 sprintf (buf, "I%d", int_n_data[idx].bitsize);
968 pp_string (pp, buf);
973 /* Print out a CHARACTER literal. */
975 static void
976 pp_c_character_constant (c_pretty_printer *pp, tree c)
978 pp_quote (pp);
979 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
980 pp_quote (pp);
983 /* Print out a BOOLEAN literal. */
985 static void
986 pp_c_bool_constant (c_pretty_printer *pp, tree b)
988 if (b == boolean_false_node)
990 if (c_dialect_cxx ())
991 pp_c_ws_string (pp, "false");
992 else if (flag_isoc99)
993 pp_c_ws_string (pp, "_False");
994 else
995 pp_unsupported_tree (pp, b);
997 else if (b == boolean_true_node)
999 if (c_dialect_cxx ())
1000 pp_c_ws_string (pp, "true");
1001 else if (flag_isoc99)
1002 pp_c_ws_string (pp, "_True");
1003 else
1004 pp_unsupported_tree (pp, b);
1006 else if (TREE_CODE (b) == INTEGER_CST)
1007 pp_c_integer_constant (pp, b);
1008 else
1009 pp_unsupported_tree (pp, b);
1012 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
1013 false; that means the value was obtained by a cast, in which case
1014 print out the type-id part of the cast-expression -- the casted value
1015 is then printed by pp_c_integer_literal. */
1017 static bool
1018 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1020 bool value_is_named = true;
1021 tree type = TREE_TYPE (e);
1022 tree value;
1024 /* Find the name of this constant. */
1025 for (value = TYPE_VALUES (type);
1026 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1027 value = TREE_CHAIN (value))
1030 if (value != NULL_TREE)
1031 pp->id_expression (TREE_PURPOSE (value));
1032 else
1034 /* Value must have been cast. */
1035 pp_c_type_cast (pp, type);
1036 value_is_named = false;
1039 return value_is_named;
1042 /* Print out a REAL value as a decimal-floating-constant. */
1044 static void
1045 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1047 const struct real_format *fmt
1048 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1050 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1051 bool is_decimal = floating_cst.decimal;
1053 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1054 log10(2) to 7 significant digits. */
1055 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1057 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1058 sizeof (pp_buffer (pp)->digit_buffer),
1059 max_digits10, 1);
1061 pp_string (pp, pp_buffer(pp)->digit_buffer);
1062 if (TREE_TYPE (r) == float_type_node)
1063 pp_character (pp, 'f');
1064 else if (TREE_TYPE (r) == long_double_type_node)
1065 pp_character (pp, 'l');
1066 else if (TREE_TYPE (r) == dfloat128_type_node)
1067 pp_string (pp, "dl");
1068 else if (TREE_TYPE (r) == dfloat64_type_node)
1069 pp_string (pp, "dd");
1070 else if (TREE_TYPE (r) == dfloat32_type_node)
1071 pp_string (pp, "df");
1074 /* Print out a FIXED value as a decimal-floating-constant. */
1076 static void
1077 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1079 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1080 sizeof (pp_buffer (pp)->digit_buffer));
1081 pp_string (pp, pp_buffer(pp)->digit_buffer);
1084 /* Pretty-print a compound literal expression. GNU extensions include
1085 vector constants. */
1087 static void
1088 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1090 tree type = TREE_TYPE (e);
1091 pp_c_type_cast (pp, type);
1093 switch (TREE_CODE (type))
1095 case RECORD_TYPE:
1096 case UNION_TYPE:
1097 case ARRAY_TYPE:
1098 case VECTOR_TYPE:
1099 case COMPLEX_TYPE:
1100 pp_c_brace_enclosed_initializer_list (pp, e);
1101 break;
1103 default:
1104 pp_unsupported_tree (pp, e);
1105 break;
1109 /* Pretty-print a COMPLEX_EXPR expression. */
1111 static void
1112 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1114 /* Handle a few common special cases, otherwise fallback
1115 to printing it as compound literal. */
1116 tree type = TREE_TYPE (e);
1117 tree realexpr = TREE_OPERAND (e, 0);
1118 tree imagexpr = TREE_OPERAND (e, 1);
1120 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1121 if (TREE_CODE (realexpr) == NOP_EXPR
1122 && TREE_CODE (imagexpr) == NOP_EXPR
1123 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1124 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1125 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1126 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1127 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1128 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1130 pp_c_type_cast (pp, type);
1131 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1132 return;
1135 /* Cast of an scalar expression to COMPLEX_TYPE. */
1136 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1137 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1139 pp_c_type_cast (pp, type);
1140 if (TREE_CODE (realexpr) == NOP_EXPR)
1141 realexpr = TREE_OPERAND (realexpr, 0);
1142 pp->expression (realexpr);
1143 return;
1146 pp_c_compound_literal (pp, e);
1149 /* constant:
1150 integer-constant
1151 floating-constant
1152 fixed-point-constant
1153 enumeration-constant
1154 character-constant */
1156 void
1157 c_pretty_printer::constant (tree e)
1159 const enum tree_code code = TREE_CODE (e);
1161 switch (code)
1163 case VOID_CST:
1164 pp_c_void_constant (this);
1165 break;
1167 case INTEGER_CST:
1169 tree type = TREE_TYPE (e);
1170 if (type == boolean_type_node)
1171 pp_c_bool_constant (this, e);
1172 else if (type == char_type_node)
1173 pp_c_character_constant (this, e);
1174 else if (TREE_CODE (type) == ENUMERAL_TYPE
1175 && pp_c_enumeration_constant (this, e))
1177 else
1178 pp_c_integer_constant (this, e);
1180 break;
1182 case REAL_CST:
1183 pp_c_floating_constant (this, e);
1184 break;
1186 case FIXED_CST:
1187 pp_c_fixed_constant (this, e);
1188 break;
1190 case STRING_CST:
1191 pp_c_string_literal (this, e);
1192 break;
1194 case COMPLEX_CST:
1195 /* Sometimes, we are confused and we think a complex literal
1196 is a constant. Such thing is a compound literal which
1197 grammatically belongs to postfix-expr production. */
1198 pp_c_compound_literal (this, e);
1199 break;
1201 default:
1202 pp_unsupported_tree (this, e);
1203 break;
1207 /* Pretty-print a string such as an identifier, without changing its
1208 encoding, preceded by whitespace is necessary. */
1210 void
1211 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1213 pp_c_maybe_whitespace (pp);
1214 pp_string (pp, str);
1215 pp->padding = pp_before;
1218 void
1219 c_pretty_printer::translate_string (const char *gmsgid)
1221 if (pp_translate_identifiers (this))
1222 pp_c_ws_string (this, _(gmsgid));
1223 else
1224 pp_c_ws_string (this, gmsgid);
1227 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1228 that need converting to the locale encoding, preceded by whitespace
1229 is necessary. */
1231 void
1232 pp_c_identifier (c_pretty_printer *pp, const char *id)
1234 pp_c_maybe_whitespace (pp);
1235 pp_identifier (pp, id);
1236 pp->padding = pp_before;
1239 /* Pretty-print a C primary-expression.
1240 primary-expression:
1241 identifier
1242 constant
1243 string-literal
1244 ( expression ) */
1246 void
1247 c_pretty_printer::primary_expression (tree e)
1249 switch (TREE_CODE (e))
1251 case VAR_DECL:
1252 case PARM_DECL:
1253 case FIELD_DECL:
1254 case CONST_DECL:
1255 case FUNCTION_DECL:
1256 case LABEL_DECL:
1257 pp_c_tree_decl_identifier (this, e);
1258 break;
1260 case IDENTIFIER_NODE:
1261 pp_c_tree_identifier (this, e);
1262 break;
1264 case ERROR_MARK:
1265 translate_string ("<erroneous-expression>");
1266 break;
1268 case RESULT_DECL:
1269 translate_string ("<return-value>");
1270 break;
1272 case VOID_CST:
1273 case INTEGER_CST:
1274 case REAL_CST:
1275 case FIXED_CST:
1276 case STRING_CST:
1277 constant (e);
1278 break;
1280 case TARGET_EXPR:
1281 pp_c_ws_string (this, "__builtin_memcpy");
1282 pp_c_left_paren (this);
1283 pp_ampersand (this);
1284 primary_expression (TREE_OPERAND (e, 0));
1285 pp_separate_with (this, ',');
1286 pp_ampersand (this);
1287 initializer (TREE_OPERAND (e, 1));
1288 if (TREE_OPERAND (e, 2))
1290 pp_separate_with (this, ',');
1291 expression (TREE_OPERAND (e, 2));
1293 pp_c_right_paren (this);
1294 break;
1296 default:
1297 /* FIXME: Make sure we won't get into an infinite loop. */
1298 pp_c_left_paren (this);
1299 expression (e);
1300 pp_c_right_paren (this);
1301 break;
1305 /* Print out a C initializer -- also support C compound-literals.
1306 initializer:
1307 assignment-expression:
1308 { initializer-list }
1309 { initializer-list , } */
1311 void
1312 c_pretty_printer::initializer (tree e)
1314 if (TREE_CODE (e) == CONSTRUCTOR)
1315 pp_c_brace_enclosed_initializer_list (this, e);
1316 else
1317 expression (e);
1320 /* init-declarator:
1321 declarator:
1322 declarator = initializer */
1324 void
1325 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1327 pp->declarator (t);
1328 /* We don't want to output function definitions here. There are handled
1329 elsewhere (and the syntactic form is bogus anyway). */
1330 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1332 tree init = DECL_INITIAL (t);
1333 /* This C++ bit is handled here because it is easier to do so.
1334 In templates, the C++ parser builds a TREE_LIST for a
1335 direct-initialization; the TREE_PURPOSE is the variable to
1336 initialize and the TREE_VALUE is the initializer. */
1337 if (TREE_CODE (init) == TREE_LIST)
1339 pp_c_left_paren (pp);
1340 pp->expression (TREE_VALUE (init));
1341 pp_right_paren (pp);
1343 else
1345 pp_space (pp);
1346 pp_equal (pp);
1347 pp_space (pp);
1348 pp->initializer (init);
1353 /* initializer-list:
1354 designation(opt) initializer
1355 initializer-list , designation(opt) initializer
1357 designation:
1358 designator-list =
1360 designator-list:
1361 designator
1362 designator-list designator
1364 designator:
1365 [ constant-expression ]
1366 identifier */
1368 static void
1369 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1371 tree type = TREE_TYPE (e);
1372 const enum tree_code code = TREE_CODE (type);
1374 if (TREE_CODE (e) == CONSTRUCTOR)
1376 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1377 return;
1380 switch (code)
1382 case RECORD_TYPE:
1383 case UNION_TYPE:
1384 case ARRAY_TYPE:
1386 tree init = TREE_OPERAND (e, 0);
1387 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1389 if (code == RECORD_TYPE || code == UNION_TYPE)
1391 pp_c_dot (pp);
1392 pp->primary_expression (TREE_PURPOSE (init));
1394 else
1396 pp_c_left_bracket (pp);
1397 if (TREE_PURPOSE (init))
1398 pp->constant (TREE_PURPOSE (init));
1399 pp_c_right_bracket (pp);
1401 pp_c_whitespace (pp);
1402 pp_equal (pp);
1403 pp_c_whitespace (pp);
1404 pp->initializer (TREE_VALUE (init));
1405 if (TREE_CHAIN (init))
1406 pp_separate_with (pp, ',');
1409 return;
1411 case VECTOR_TYPE:
1412 if (TREE_CODE (e) == VECTOR_CST)
1414 unsigned i;
1415 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1417 if (i > 0)
1418 pp_separate_with (pp, ',');
1419 pp->expression (VECTOR_CST_ELT (e, i));
1422 else
1423 break;
1424 return;
1426 case COMPLEX_TYPE:
1427 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1429 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1430 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1431 pp_separate_with (pp, ',');
1432 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1434 else
1435 break;
1436 return;
1438 default:
1439 break;
1442 pp_unsupported_tree (pp, type);
1445 /* Pretty-print a brace-enclosed initializer-list. */
1447 static void
1448 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1450 pp_c_left_brace (pp);
1451 pp_c_initializer_list (pp, l);
1452 pp_c_right_brace (pp);
1456 /* This is a convenient function, used to bridge gap between C and C++
1457 grammars.
1459 id-expression:
1460 identifier */
1462 void
1463 c_pretty_printer::id_expression (tree t)
1465 switch (TREE_CODE (t))
1467 case VAR_DECL:
1468 case PARM_DECL:
1469 case CONST_DECL:
1470 case TYPE_DECL:
1471 case FUNCTION_DECL:
1472 case FIELD_DECL:
1473 case LABEL_DECL:
1474 pp_c_tree_decl_identifier (this, t);
1475 break;
1477 case IDENTIFIER_NODE:
1478 pp_c_tree_identifier (this, t);
1479 break;
1481 default:
1482 pp_unsupported_tree (this, t);
1483 break;
1487 /* postfix-expression:
1488 primary-expression
1489 postfix-expression [ expression ]
1490 postfix-expression ( argument-expression-list(opt) )
1491 postfix-expression . identifier
1492 postfix-expression -> identifier
1493 postfix-expression ++
1494 postfix-expression --
1495 ( type-name ) { initializer-list }
1496 ( type-name ) { initializer-list , } */
1498 void
1499 c_pretty_printer::postfix_expression (tree e)
1501 enum tree_code code = TREE_CODE (e);
1502 switch (code)
1504 case POSTINCREMENT_EXPR:
1505 case POSTDECREMENT_EXPR:
1506 postfix_expression (TREE_OPERAND (e, 0));
1507 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1508 break;
1510 case ARRAY_REF:
1511 postfix_expression (TREE_OPERAND (e, 0));
1512 pp_c_left_bracket (this);
1513 expression (TREE_OPERAND (e, 1));
1514 pp_c_right_bracket (this);
1515 break;
1517 case ARRAY_NOTATION_REF:
1518 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1519 pp_c_left_bracket (this);
1520 expression (ARRAY_NOTATION_START (e));
1521 pp_colon (this);
1522 expression (ARRAY_NOTATION_LENGTH (e));
1523 pp_colon (this);
1524 expression (ARRAY_NOTATION_STRIDE (e));
1525 pp_c_right_bracket (this);
1526 break;
1528 case CALL_EXPR:
1530 call_expr_arg_iterator iter;
1531 tree arg;
1532 postfix_expression (CALL_EXPR_FN (e));
1533 pp_c_left_paren (this);
1534 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1536 expression (arg);
1537 if (more_call_expr_args_p (&iter))
1538 pp_separate_with (this, ',');
1540 pp_c_right_paren (this);
1541 break;
1544 case UNORDERED_EXPR:
1545 pp_c_ws_string (this, flag_isoc99
1546 ? "isunordered"
1547 : "__builtin_isunordered");
1548 goto two_args_fun;
1550 case ORDERED_EXPR:
1551 pp_c_ws_string (this, flag_isoc99
1552 ? "!isunordered"
1553 : "!__builtin_isunordered");
1554 goto two_args_fun;
1556 case UNLT_EXPR:
1557 pp_c_ws_string (this, flag_isoc99
1558 ? "!isgreaterequal"
1559 : "!__builtin_isgreaterequal");
1560 goto two_args_fun;
1562 case UNLE_EXPR:
1563 pp_c_ws_string (this, flag_isoc99
1564 ? "!isgreater"
1565 : "!__builtin_isgreater");
1566 goto two_args_fun;
1568 case UNGT_EXPR:
1569 pp_c_ws_string (this, flag_isoc99
1570 ? "!islessequal"
1571 : "!__builtin_islessequal");
1572 goto two_args_fun;
1574 case UNGE_EXPR:
1575 pp_c_ws_string (this, flag_isoc99
1576 ? "!isless"
1577 : "!__builtin_isless");
1578 goto two_args_fun;
1580 case UNEQ_EXPR:
1581 pp_c_ws_string (this, flag_isoc99
1582 ? "!islessgreater"
1583 : "!__builtin_islessgreater");
1584 goto two_args_fun;
1586 case LTGT_EXPR:
1587 pp_c_ws_string (this, flag_isoc99
1588 ? "islessgreater"
1589 : "__builtin_islessgreater");
1590 goto two_args_fun;
1592 two_args_fun:
1593 pp_c_left_paren (this);
1594 expression (TREE_OPERAND (e, 0));
1595 pp_separate_with (this, ',');
1596 expression (TREE_OPERAND (e, 1));
1597 pp_c_right_paren (this);
1598 break;
1600 case ABS_EXPR:
1601 pp_c_ws_string (this, "__builtin_abs");
1602 pp_c_left_paren (this);
1603 expression (TREE_OPERAND (e, 0));
1604 pp_c_right_paren (this);
1605 break;
1607 case COMPONENT_REF:
1609 tree object = TREE_OPERAND (e, 0);
1610 if (TREE_CODE (object) == INDIRECT_REF)
1612 postfix_expression (TREE_OPERAND (object, 0));
1613 pp_c_arrow (this);
1615 else
1617 postfix_expression (object);
1618 pp_c_dot (this);
1620 expression (TREE_OPERAND (e, 1));
1622 break;
1624 case BIT_FIELD_REF:
1626 tree type = TREE_TYPE (e);
1628 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1629 if (type
1630 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1632 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1633 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1634 if ((bitpos % size) == 0)
1636 pp_c_left_paren (this);
1637 pp_c_left_paren (this);
1638 type_id (type);
1639 pp_c_star (this);
1640 pp_c_right_paren (this);
1641 pp_c_ampersand (this);
1642 expression (TREE_OPERAND (e, 0));
1643 pp_c_right_paren (this);
1644 pp_c_left_bracket (this);
1645 pp_wide_integer (this, bitpos / size);
1646 pp_c_right_bracket (this);
1647 break;
1650 pp_unsupported_tree (this, e);
1652 break;
1654 case MEM_REF:
1655 expression (e);
1656 break;
1658 case COMPLEX_CST:
1659 case VECTOR_CST:
1660 pp_c_compound_literal (this, e);
1661 break;
1663 case COMPLEX_EXPR:
1664 pp_c_complex_expr (this, e);
1665 break;
1667 case COMPOUND_LITERAL_EXPR:
1668 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1669 /* Fall through. */
1670 case CONSTRUCTOR:
1671 initializer (e);
1672 break;
1674 case VA_ARG_EXPR:
1675 pp_c_ws_string (this, "__builtin_va_arg");
1676 pp_c_left_paren (this);
1677 assignment_expression (TREE_OPERAND (e, 0));
1678 pp_separate_with (this, ',');
1679 type_id (TREE_TYPE (e));
1680 pp_c_right_paren (this);
1681 break;
1683 case ADDR_EXPR:
1684 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1686 id_expression (TREE_OPERAND (e, 0));
1687 break;
1689 /* else fall through. */
1691 default:
1692 primary_expression (e);
1693 break;
1697 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1699 void
1700 pp_c_expression_list (c_pretty_printer *pp, tree e)
1702 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1704 pp->expression (TREE_VALUE (e));
1705 if (TREE_CHAIN (e))
1706 pp_separate_with (pp, ',');
1710 /* Print out V, which contains the elements of a constructor. */
1712 void
1713 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1715 unsigned HOST_WIDE_INT ix;
1716 tree value;
1718 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1720 pp->expression (value);
1721 if (ix != vec_safe_length (v) - 1)
1722 pp_separate_with (pp, ',');
1726 /* Print out an expression-list in parens, as if it were the argument
1727 list to a function. */
1729 void
1730 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1732 pp_c_left_paren (pp);
1733 if (t && TREE_CODE (t) == TREE_LIST)
1734 pp_c_expression_list (pp, t);
1735 pp_c_right_paren (pp);
1738 /* unary-expression:
1739 postfix-expression
1740 ++ cast-expression
1741 -- cast-expression
1742 unary-operator cast-expression
1743 sizeof unary-expression
1744 sizeof ( type-id )
1746 unary-operator: one of
1747 * & + - ! ~
1749 GNU extensions.
1750 unary-expression:
1751 __alignof__ unary-expression
1752 __alignof__ ( type-id )
1753 __real__ unary-expression
1754 __imag__ unary-expression */
1756 void
1757 c_pretty_printer::unary_expression (tree e)
1759 enum tree_code code = TREE_CODE (e);
1760 switch (code)
1762 case PREINCREMENT_EXPR:
1763 case PREDECREMENT_EXPR:
1764 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1765 unary_expression (TREE_OPERAND (e, 0));
1766 break;
1768 case ADDR_EXPR:
1769 case INDIRECT_REF:
1770 case NEGATE_EXPR:
1771 case BIT_NOT_EXPR:
1772 case TRUTH_NOT_EXPR:
1773 case CONJ_EXPR:
1774 /* String literal are used by address. */
1775 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1776 pp_ampersand (this);
1777 else if (code == INDIRECT_REF)
1778 pp_c_star (this);
1779 else if (code == NEGATE_EXPR)
1780 pp_minus (this);
1781 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1782 pp_complement (this);
1783 else if (code == TRUTH_NOT_EXPR)
1784 pp_exclamation (this);
1785 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1786 break;
1788 case MEM_REF:
1789 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1790 && integer_zerop (TREE_OPERAND (e, 1)))
1791 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1792 else
1794 pp_c_star (this);
1795 if (!integer_zerop (TREE_OPERAND (e, 1)))
1797 pp_c_left_paren (this);
1798 if (!integer_onep (TYPE_SIZE_UNIT
1799 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1800 pp_c_type_cast (this, ptr_type_node);
1802 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1803 if (!integer_zerop (TREE_OPERAND (e, 1)))
1805 pp_plus (this);
1806 pp_c_integer_constant (this,
1807 fold_convert (ssizetype,
1808 TREE_OPERAND (e, 1)));
1809 pp_c_right_paren (this);
1812 break;
1814 case REALPART_EXPR:
1815 case IMAGPART_EXPR:
1816 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1817 pp_c_whitespace (this);
1818 unary_expression (TREE_OPERAND (e, 0));
1819 break;
1821 default:
1822 postfix_expression (e);
1823 break;
1827 /* cast-expression:
1828 unary-expression
1829 ( type-name ) cast-expression */
1831 void
1832 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1834 switch (TREE_CODE (e))
1836 case FLOAT_EXPR:
1837 case FIX_TRUNC_EXPR:
1838 CASE_CONVERT:
1839 case VIEW_CONVERT_EXPR:
1840 pp_c_type_cast (pp, TREE_TYPE (e));
1841 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1842 break;
1844 default:
1845 pp->unary_expression (e);
1849 /* multiplicative-expression:
1850 cast-expression
1851 multiplicative-expression * cast-expression
1852 multiplicative-expression / cast-expression
1853 multiplicative-expression % cast-expression */
1855 void
1856 c_pretty_printer::multiplicative_expression (tree e)
1858 enum tree_code code = TREE_CODE (e);
1859 switch (code)
1861 case MULT_EXPR:
1862 case TRUNC_DIV_EXPR:
1863 case TRUNC_MOD_EXPR:
1864 multiplicative_expression (TREE_OPERAND (e, 0));
1865 pp_c_whitespace (this);
1866 if (code == MULT_EXPR)
1867 pp_c_star (this);
1868 else if (code == TRUNC_DIV_EXPR)
1869 pp_slash (this);
1870 else
1871 pp_modulo (this);
1872 pp_c_whitespace (this);
1873 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1874 break;
1876 default:
1877 pp_c_cast_expression (this, e);
1878 break;
1882 /* additive-expression:
1883 multiplicative-expression
1884 additive-expression + multiplicative-expression
1885 additive-expression - multiplicative-expression */
1887 static void
1888 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1890 enum tree_code code = TREE_CODE (e);
1891 switch (code)
1893 case POINTER_PLUS_EXPR:
1894 case PLUS_EXPR:
1895 case MINUS_EXPR:
1896 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1897 pp_c_whitespace (pp);
1898 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1899 pp_plus (pp);
1900 else
1901 pp_minus (pp);
1902 pp_c_whitespace (pp);
1903 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1904 break;
1906 default:
1907 pp->multiplicative_expression (e);
1908 break;
1912 /* additive-expression:
1913 additive-expression
1914 shift-expression << additive-expression
1915 shift-expression >> additive-expression */
1917 static void
1918 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1920 enum tree_code code = TREE_CODE (e);
1921 switch (code)
1923 case LSHIFT_EXPR:
1924 case RSHIFT_EXPR:
1925 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1926 pp_c_whitespace (pp);
1927 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1928 pp_c_whitespace (pp);
1929 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1930 break;
1932 default:
1933 pp_c_additive_expression (pp, e);
1937 /* relational-expression:
1938 shift-expression
1939 relational-expression < shift-expression
1940 relational-expression > shift-expression
1941 relational-expression <= shift-expression
1942 relational-expression >= shift-expression */
1944 static void
1945 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1947 enum tree_code code = TREE_CODE (e);
1948 switch (code)
1950 case LT_EXPR:
1951 case GT_EXPR:
1952 case LE_EXPR:
1953 case GE_EXPR:
1954 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1955 pp_c_whitespace (pp);
1956 if (code == LT_EXPR)
1957 pp_less (pp);
1958 else if (code == GT_EXPR)
1959 pp_greater (pp);
1960 else if (code == LE_EXPR)
1961 pp_less_equal (pp);
1962 else if (code == GE_EXPR)
1963 pp_greater_equal (pp);
1964 pp_c_whitespace (pp);
1965 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1966 break;
1968 default:
1969 pp_c_shift_expression (pp, e);
1970 break;
1974 /* equality-expression:
1975 relational-expression
1976 equality-expression == relational-expression
1977 equality-equality != relational-expression */
1979 static void
1980 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1982 enum tree_code code = TREE_CODE (e);
1983 switch (code)
1985 case EQ_EXPR:
1986 case NE_EXPR:
1987 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1988 pp_c_whitespace (pp);
1989 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1990 pp_c_whitespace (pp);
1991 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1992 break;
1994 default:
1995 pp_c_relational_expression (pp, e);
1996 break;
2000 /* AND-expression:
2001 equality-expression
2002 AND-expression & equality-equality */
2004 static void
2005 pp_c_and_expression (c_pretty_printer *pp, tree e)
2007 if (TREE_CODE (e) == BIT_AND_EXPR)
2009 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2010 pp_c_whitespace (pp);
2011 pp_ampersand (pp);
2012 pp_c_whitespace (pp);
2013 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2015 else
2016 pp_c_equality_expression (pp, e);
2019 /* exclusive-OR-expression:
2020 AND-expression
2021 exclusive-OR-expression ^ AND-expression */
2023 static void
2024 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2026 if (TREE_CODE (e) == BIT_XOR_EXPR
2027 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2029 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2030 if (TREE_CODE (e) == BIT_XOR_EXPR)
2031 pp_c_maybe_whitespace (pp);
2032 else
2033 pp_c_whitespace (pp);
2034 pp_carret (pp);
2035 pp_c_whitespace (pp);
2036 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2038 else
2039 pp_c_and_expression (pp, e);
2042 /* inclusive-OR-expression:
2043 exclusive-OR-expression
2044 inclusive-OR-expression | exclusive-OR-expression */
2046 static void
2047 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2049 if (TREE_CODE (e) == BIT_IOR_EXPR)
2051 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2052 pp_c_whitespace (pp);
2053 pp_bar (pp);
2054 pp_c_whitespace (pp);
2055 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2057 else
2058 pp_c_exclusive_or_expression (pp, e);
2061 /* logical-AND-expression:
2062 inclusive-OR-expression
2063 logical-AND-expression && inclusive-OR-expression */
2065 static void
2066 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2068 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2069 || TREE_CODE (e) == TRUTH_AND_EXPR)
2071 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2072 pp_c_whitespace (pp);
2073 pp_ampersand_ampersand (pp);
2074 pp_c_whitespace (pp);
2075 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2077 else
2078 pp_c_inclusive_or_expression (pp, e);
2081 /* logical-OR-expression:
2082 logical-AND-expression
2083 logical-OR-expression || logical-AND-expression */
2085 void
2086 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2088 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2089 || TREE_CODE (e) == TRUTH_OR_EXPR)
2091 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2092 pp_c_whitespace (pp);
2093 pp_bar_bar (pp);
2094 pp_c_whitespace (pp);
2095 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2097 else
2098 pp_c_logical_and_expression (pp, e);
2101 /* conditional-expression:
2102 logical-OR-expression
2103 logical-OR-expression ? expression : conditional-expression */
2105 void
2106 c_pretty_printer::conditional_expression (tree e)
2108 if (TREE_CODE (e) == COND_EXPR)
2110 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2111 pp_c_whitespace (this);
2112 pp_question (this);
2113 pp_c_whitespace (this);
2114 expression (TREE_OPERAND (e, 1));
2115 pp_c_whitespace (this);
2116 pp_colon (this);
2117 pp_c_whitespace (this);
2118 conditional_expression (TREE_OPERAND (e, 2));
2120 else
2121 pp_c_logical_or_expression (this, e);
2125 /* assignment-expression:
2126 conditional-expression
2127 unary-expression assignment-operator assignment-expression
2129 assignment-expression: one of
2130 = *= /= %= += -= >>= <<= &= ^= |= */
2132 void
2133 c_pretty_printer::assignment_expression (tree e)
2135 if (TREE_CODE (e) == MODIFY_EXPR
2136 || TREE_CODE (e) == INIT_EXPR)
2138 unary_expression (TREE_OPERAND (e, 0));
2139 pp_c_whitespace (this);
2140 pp_equal (this);
2141 pp_space (this);
2142 expression (TREE_OPERAND (e, 1));
2144 else
2145 conditional_expression (e);
2148 /* expression:
2149 assignment-expression
2150 expression , assignment-expression
2152 Implementation note: instead of going through the usual recursion
2153 chain, I take the liberty of dispatching nodes to the appropriate
2154 functions. This makes some redundancy, but it worths it. That also
2155 prevents a possible infinite recursion between primary_expression ()
2156 and expression (). */
2158 void
2159 c_pretty_printer::expression (tree e)
2161 switch (TREE_CODE (e))
2163 case VOID_CST:
2164 pp_c_void_constant (this);
2165 break;
2167 case INTEGER_CST:
2168 pp_c_integer_constant (this, e);
2169 break;
2171 case REAL_CST:
2172 pp_c_floating_constant (this, e);
2173 break;
2175 case FIXED_CST:
2176 pp_c_fixed_constant (this, e);
2177 break;
2179 case STRING_CST:
2180 pp_c_string_literal (this, e);
2181 break;
2183 case IDENTIFIER_NODE:
2184 case FUNCTION_DECL:
2185 case VAR_DECL:
2186 case CONST_DECL:
2187 case PARM_DECL:
2188 case RESULT_DECL:
2189 case FIELD_DECL:
2190 case LABEL_DECL:
2191 case ERROR_MARK:
2192 primary_expression (e);
2193 break;
2195 case SSA_NAME:
2196 if (SSA_NAME_VAR (e)
2197 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2198 expression (SSA_NAME_VAR (e));
2199 else
2200 translate_string ("<unknown>");
2201 break;
2203 case POSTINCREMENT_EXPR:
2204 case POSTDECREMENT_EXPR:
2205 case ARRAY_REF:
2206 case ARRAY_NOTATION_REF:
2207 case CALL_EXPR:
2208 case COMPONENT_REF:
2209 case BIT_FIELD_REF:
2210 case COMPLEX_CST:
2211 case COMPLEX_EXPR:
2212 case VECTOR_CST:
2213 case ORDERED_EXPR:
2214 case UNORDERED_EXPR:
2215 case LTGT_EXPR:
2216 case UNEQ_EXPR:
2217 case UNLE_EXPR:
2218 case UNLT_EXPR:
2219 case UNGE_EXPR:
2220 case UNGT_EXPR:
2221 case ABS_EXPR:
2222 case CONSTRUCTOR:
2223 case COMPOUND_LITERAL_EXPR:
2224 case VA_ARG_EXPR:
2225 postfix_expression (e);
2226 break;
2228 case CONJ_EXPR:
2229 case ADDR_EXPR:
2230 case INDIRECT_REF:
2231 case MEM_REF:
2232 case NEGATE_EXPR:
2233 case BIT_NOT_EXPR:
2234 case TRUTH_NOT_EXPR:
2235 case PREINCREMENT_EXPR:
2236 case PREDECREMENT_EXPR:
2237 case REALPART_EXPR:
2238 case IMAGPART_EXPR:
2239 unary_expression (e);
2240 break;
2242 case FLOAT_EXPR:
2243 case FIX_TRUNC_EXPR:
2244 CASE_CONVERT:
2245 case VIEW_CONVERT_EXPR:
2246 pp_c_cast_expression (this, e);
2247 break;
2249 case MULT_EXPR:
2250 case TRUNC_MOD_EXPR:
2251 case TRUNC_DIV_EXPR:
2252 multiplicative_expression (e);
2253 break;
2255 case LSHIFT_EXPR:
2256 case RSHIFT_EXPR:
2257 pp_c_shift_expression (this, e);
2258 break;
2260 case LT_EXPR:
2261 case GT_EXPR:
2262 case LE_EXPR:
2263 case GE_EXPR:
2264 pp_c_relational_expression (this, e);
2265 break;
2267 case BIT_AND_EXPR:
2268 pp_c_and_expression (this, e);
2269 break;
2271 case BIT_XOR_EXPR:
2272 case TRUTH_XOR_EXPR:
2273 pp_c_exclusive_or_expression (this, e);
2274 break;
2276 case BIT_IOR_EXPR:
2277 pp_c_inclusive_or_expression (this, e);
2278 break;
2280 case TRUTH_ANDIF_EXPR:
2281 case TRUTH_AND_EXPR:
2282 pp_c_logical_and_expression (this, e);
2283 break;
2285 case TRUTH_ORIF_EXPR:
2286 case TRUTH_OR_EXPR:
2287 pp_c_logical_or_expression (this, e);
2288 break;
2290 case EQ_EXPR:
2291 case NE_EXPR:
2292 pp_c_equality_expression (this, e);
2293 break;
2295 case COND_EXPR:
2296 conditional_expression (e);
2297 break;
2299 case POINTER_PLUS_EXPR:
2300 case PLUS_EXPR:
2301 case MINUS_EXPR:
2302 pp_c_additive_expression (this, e);
2303 break;
2305 case MODIFY_EXPR:
2306 case INIT_EXPR:
2307 assignment_expression (e);
2308 break;
2310 case COMPOUND_EXPR:
2311 pp_c_left_paren (this);
2312 expression (TREE_OPERAND (e, 0));
2313 pp_separate_with (this, ',');
2314 assignment_expression (TREE_OPERAND (e, 1));
2315 pp_c_right_paren (this);
2316 break;
2318 case NON_LVALUE_EXPR:
2319 case SAVE_EXPR:
2320 expression (TREE_OPERAND (e, 0));
2321 break;
2323 case TARGET_EXPR:
2324 postfix_expression (TREE_OPERAND (e, 1));
2325 break;
2327 case BIND_EXPR:
2328 case GOTO_EXPR:
2329 /* We don't yet have a way of dumping statements in a
2330 human-readable format. */
2331 pp_string (this, "({...})");
2332 break;
2334 case C_MAYBE_CONST_EXPR:
2335 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2336 break;
2338 default:
2339 pp_unsupported_tree (this, e);
2340 break;
2346 /* Statements. */
2348 void
2349 c_pretty_printer::statement (tree stmt)
2351 if (stmt == NULL)
2352 return;
2354 if (pp_needs_newline (this))
2355 pp_newline_and_indent (this, 0);
2357 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2361 /* Initialize the PRETTY-PRINTER for handling C codes. */
2363 c_pretty_printer::c_pretty_printer ()
2364 : pretty_printer (),
2365 offset_list (),
2366 flags ()
2368 type_specifier_seq = pp_c_specifier_qualifier_list;
2369 ptr_operator = pp_c_pointer;
2370 parameter_list = pp_c_parameter_type_list;
2374 /* Print the tree T in full, on file FILE. */
2376 void
2377 print_c_tree (FILE *file, tree t)
2379 c_pretty_printer pp;
2381 pp_needs_newline (&pp) = true;
2382 pp.buffer->stream = file;
2383 pp.statement (t);
2384 pp_newline_and_flush (&pp);
2387 /* Print the tree T in full, on stderr. */
2389 DEBUG_FUNCTION void
2390 debug_c_tree (tree t)
2392 print_c_tree (stderr, t);
2393 fputc ('\n', stderr);
2396 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2397 up of T's memory address. */
2399 void
2400 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2402 const char *name;
2404 gcc_assert (DECL_P (t));
2406 if (DECL_NAME (t))
2407 name = IDENTIFIER_POINTER (DECL_NAME (t));
2408 else
2410 static char xname[8];
2411 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2412 name = xname;
2415 pp_c_identifier (pp, name);