PR c/64768
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob4ca6420c3ff9253628cad7b0db55bc54b4eeb560
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "stor-layout.h"
37 #include "attribs.h"
38 #include "intl.h"
39 #include "c-pretty-print.h"
40 #include "tree-pretty-print.h"
41 #include "tree-iterator.h"
42 #include "diagnostic.h"
43 #include "wide-int-print.h"
45 /* The pretty-printer code is primarily designed to closely follow
46 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
47 codes we used to have in the past. Following a structured
48 approach (preferably the official grammars) is believed to make it
49 much easier to add extensions and nifty pretty-printing effects that
50 takes expression or declaration contexts into account. */
53 #define pp_c_maybe_whitespace(PP) \
54 do { \
55 if ((PP)->padding == pp_before) \
56 pp_c_whitespace (PP); \
57 } while (0)
59 /* literal */
60 static void pp_c_char (c_pretty_printer *, int);
62 /* postfix-expression */
63 static void pp_c_initializer_list (c_pretty_printer *, tree);
64 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
66 static void pp_c_additive_expression (c_pretty_printer *, tree);
67 static void pp_c_shift_expression (c_pretty_printer *, tree);
68 static void pp_c_relational_expression (c_pretty_printer *, tree);
69 static void pp_c_equality_expression (c_pretty_printer *, tree);
70 static void pp_c_and_expression (c_pretty_printer *, tree);
71 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
72 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
73 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
75 /* declarations. */
78 /* Helper functions. */
80 void
81 pp_c_whitespace (c_pretty_printer *pp)
83 pp_space (pp);
84 pp->padding = pp_none;
87 void
88 pp_c_left_paren (c_pretty_printer *pp)
90 pp_left_paren (pp);
91 pp->padding = pp_none;
94 void
95 pp_c_right_paren (c_pretty_printer *pp)
97 pp_right_paren (pp);
98 pp->padding = pp_none;
101 void
102 pp_c_left_brace (c_pretty_printer *pp)
104 pp_left_brace (pp);
105 pp->padding = pp_none;
108 void
109 pp_c_right_brace (c_pretty_printer *pp)
111 pp_right_brace (pp);
112 pp->padding = pp_none;
115 void
116 pp_c_left_bracket (c_pretty_printer *pp)
118 pp_left_bracket (pp);
119 pp->padding = pp_none;
122 void
123 pp_c_right_bracket (c_pretty_printer *pp)
125 pp_right_bracket (pp);
126 pp->padding = pp_none;
129 void
130 pp_c_dot (c_pretty_printer *pp)
132 pp_dot (pp);
133 pp->padding = pp_none;
136 void
137 pp_c_ampersand (c_pretty_printer *pp)
139 pp_ampersand (pp);
140 pp->padding = pp_none;
143 void
144 pp_c_star (c_pretty_printer *pp)
146 pp_star (pp);
147 pp->padding = pp_none;
150 void
151 pp_c_arrow (c_pretty_printer *pp)
153 pp_arrow (pp);
154 pp->padding = pp_none;
157 void
158 pp_c_semicolon (c_pretty_printer *pp)
160 pp_semicolon (pp);
161 pp->padding = pp_none;
164 void
165 pp_c_complement (c_pretty_printer *pp)
167 pp_complement (pp);
168 pp->padding = pp_none;
171 void
172 pp_c_exclamation (c_pretty_printer *pp)
174 pp_exclamation (pp);
175 pp->padding = pp_none;
178 /* Print out the external representation of QUALIFIERS. */
180 void
181 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
183 const char *p = pp_last_position_in_text (pp);
184 bool previous = false;
186 if (!qualifiers)
187 return;
189 /* The C programming language does not have references, but it is much
190 simpler to handle those here rather than going through the same
191 logic in the C++ pretty-printer. */
192 if (p != NULL && (*p == '*' || *p == '&'))
193 pp_c_whitespace (pp);
195 if (qualifiers & TYPE_QUAL_ATOMIC)
197 pp_c_ws_string (pp, "_Atomic");
198 previous = true;
201 if (qualifiers & TYPE_QUAL_CONST)
203 if (previous)
204 pp_c_whitespace (pp);
205 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
206 previous = true;
209 if (qualifiers & TYPE_QUAL_VOLATILE)
211 if (previous)
212 pp_c_whitespace (pp);
213 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
214 previous = true;
217 if (qualifiers & TYPE_QUAL_RESTRICT)
219 if (previous)
220 pp_c_whitespace (pp);
221 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
222 ? "restrict" : "__restrict__"));
226 /* Pretty-print T using the type-cast notation '( type-name )'. */
228 static void
229 pp_c_type_cast (c_pretty_printer *pp, tree t)
231 pp_c_left_paren (pp);
232 pp->type_id (t);
233 pp_c_right_paren (pp);
236 /* We're about to pretty-print a pointer type as indicated by T.
237 Output a whitespace, if needed, preparing for subsequent output. */
239 void
240 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
242 if (POINTER_TYPE_P (t))
244 tree pointee = strip_pointer_operator (TREE_TYPE (t));
245 if (TREE_CODE (pointee) != ARRAY_TYPE
246 && TREE_CODE (pointee) != FUNCTION_TYPE)
247 pp_c_whitespace (pp);
252 /* Declarations. */
254 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
255 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
256 of its type. Take care of possible extensions.
258 type-qualifier-list:
259 type-qualifier
260 type-qualifier-list type-qualifier
262 type-qualifier:
263 const
264 restrict -- C99
265 __restrict__ -- GNU C
266 address-space-qualifier -- GNU C
267 volatile
268 _Atomic -- C11
270 address-space-qualifier:
271 identifier -- GNU C */
273 void
274 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
276 int qualifiers;
278 if (!t || t == error_mark_node)
279 return;
281 if (!TYPE_P (t))
282 t = TREE_TYPE (t);
284 qualifiers = TYPE_QUALS (t);
285 pp_c_cv_qualifiers (pp, qualifiers,
286 TREE_CODE (t) == FUNCTION_TYPE);
288 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
290 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
291 pp_c_identifier (pp, as);
295 /* pointer:
296 * type-qualifier-list(opt)
297 * type-qualifier-list(opt) pointer */
299 static void
300 pp_c_pointer (c_pretty_printer *pp, tree t)
302 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
303 t = TREE_TYPE (t);
304 switch (TREE_CODE (t))
306 case POINTER_TYPE:
307 /* It is easier to handle C++ reference types here. */
308 case REFERENCE_TYPE:
309 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
310 pp_c_pointer (pp, TREE_TYPE (t));
311 if (TREE_CODE (t) == POINTER_TYPE)
312 pp_c_star (pp);
313 else
314 pp_c_ampersand (pp);
315 pp_c_type_qualifier_list (pp, t);
316 break;
318 /* ??? This node is now in GENERIC and so shouldn't be here. But
319 we'll fix that later. */
320 case DECL_EXPR:
321 pp->declaration (DECL_EXPR_DECL (t));
322 pp_needs_newline (pp) = true;
323 break;
325 default:
326 pp_unsupported_tree (pp, t);
330 /* simple-type-specifier:
331 type-specifier
333 type-specifier:
334 void
335 char
336 short
338 long
339 float
340 double
341 signed
342 unsigned
343 _Bool -- C99
344 _Complex -- C99
345 _Imaginary -- C99
346 struct-or-union-specifier
347 enum-specifier
348 typedef-name.
350 GNU extensions.
351 simple-type-specifier:
352 __complex__
353 __vector__ */
355 void
356 c_pretty_printer::simple_type_specifier (tree t)
358 const enum tree_code code = TREE_CODE (t);
359 switch (code)
361 case ERROR_MARK:
362 translate_string ("<type-error>");
363 break;
365 case IDENTIFIER_NODE:
366 pp_c_identifier (this, IDENTIFIER_POINTER (t));
367 break;
369 case VOID_TYPE:
370 case BOOLEAN_TYPE:
371 case INTEGER_TYPE:
372 case REAL_TYPE:
373 case FIXED_POINT_TYPE:
374 if (TYPE_NAME (t))
376 t = TYPE_NAME (t);
377 simple_type_specifier (t);
379 else
381 int prec = TYPE_PRECISION (t);
382 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
383 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
384 else
385 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
386 if (TYPE_NAME (t))
388 simple_type_specifier (t);
389 if (TYPE_PRECISION (t) != prec)
391 pp_colon (this);
392 pp_decimal_int (this, prec);
395 else
397 switch (code)
399 case INTEGER_TYPE:
400 translate_string (TYPE_UNSIGNED (t)
401 ? "<unnamed-unsigned:"
402 : "<unnamed-signed:");
403 break;
404 case REAL_TYPE:
405 translate_string ("<unnamed-float:");
406 break;
407 case FIXED_POINT_TYPE:
408 translate_string ("<unnamed-fixed:");
409 break;
410 default:
411 gcc_unreachable ();
413 pp_decimal_int (this, prec);
414 pp_greater (this);
417 break;
419 case TYPE_DECL:
420 if (DECL_NAME (t))
421 id_expression (t);
422 else
423 translate_string ("<typedef-error>");
424 break;
426 case UNION_TYPE:
427 case RECORD_TYPE:
428 case ENUMERAL_TYPE:
429 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
430 /* Don't decorate the type if this is a typedef name. */;
431 else if (code == UNION_TYPE)
432 pp_c_ws_string (this, "union");
433 else if (code == RECORD_TYPE)
434 pp_c_ws_string (this, "struct");
435 else if (code == ENUMERAL_TYPE)
436 pp_c_ws_string (this, "enum");
437 else
438 translate_string ("<tag-error>");
440 if (TYPE_NAME (t))
441 id_expression (TYPE_NAME (t));
442 else
443 translate_string ("<anonymous>");
444 break;
446 default:
447 pp_unsupported_tree (this, t);
448 break;
452 /* specifier-qualifier-list:
453 type-specifier specifier-qualifier-list-opt
454 type-qualifier specifier-qualifier-list-opt
457 Implementation note: Because of the non-linearities in array or
458 function declarations, this routine prints not just the
459 specifier-qualifier-list of such entities or types of such entities,
460 but also the 'pointer' production part of their declarators. The
461 remaining part is done by declarator() or abstract_declarator(). */
463 void
464 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
466 const enum tree_code code = TREE_CODE (t);
468 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
469 pp_c_type_qualifier_list (pp, t);
470 switch (code)
472 case REFERENCE_TYPE:
473 case POINTER_TYPE:
475 /* Get the types-specifier of this type. */
476 tree pointee = strip_pointer_operator (TREE_TYPE (t));
477 pp_c_specifier_qualifier_list (pp, pointee);
478 if (TREE_CODE (pointee) == ARRAY_TYPE
479 || TREE_CODE (pointee) == FUNCTION_TYPE)
481 pp_c_whitespace (pp);
482 pp_c_left_paren (pp);
483 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
485 else if (!c_dialect_cxx ())
486 pp_c_whitespace (pp);
487 pp_ptr_operator (pp, t);
489 break;
491 case FUNCTION_TYPE:
492 case ARRAY_TYPE:
493 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
494 break;
496 case VECTOR_TYPE:
497 case COMPLEX_TYPE:
498 if (code == COMPLEX_TYPE)
499 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
500 ? "_Complex" : "__complex__"));
501 else if (code == VECTOR_TYPE)
503 pp_c_ws_string (pp, "__vector");
504 pp_c_left_paren (pp);
505 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
506 pp_c_right_paren (pp);
507 pp_c_whitespace (pp);
509 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
510 break;
512 default:
513 pp->simple_type_specifier (t);
514 break;
516 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
517 pp_c_type_qualifier_list (pp, t);
520 /* parameter-type-list:
521 parameter-list
522 parameter-list , ...
524 parameter-list:
525 parameter-declaration
526 parameter-list , parameter-declaration
528 parameter-declaration:
529 declaration-specifiers declarator
530 declaration-specifiers abstract-declarator(opt) */
532 void
533 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
535 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
536 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
537 pp_c_left_paren (pp);
538 if (parms == void_list_node)
539 pp_c_ws_string (pp, "void");
540 else
542 bool first = true;
543 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
545 if (!first)
546 pp_separate_with (pp, ',');
547 first = false;
548 pp->declaration_specifiers
549 (want_parm_decl ? parms : TREE_VALUE (parms));
550 if (want_parm_decl)
551 pp->declarator (parms);
552 else
553 pp->abstract_declarator (TREE_VALUE (parms));
556 pp_c_right_paren (pp);
559 /* abstract-declarator:
560 pointer
561 pointer(opt) direct-abstract-declarator */
563 void
564 c_pretty_printer::abstract_declarator (tree t)
566 if (TREE_CODE (t) == POINTER_TYPE)
568 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
569 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
570 pp_c_right_paren (this);
571 t = TREE_TYPE (t);
574 direct_abstract_declarator (t);
577 /* direct-abstract-declarator:
578 ( abstract-declarator )
579 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
580 direct-abstract-declarator(opt) [ * ]
581 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
583 void
584 c_pretty_printer::direct_abstract_declarator (tree t)
586 switch (TREE_CODE (t))
588 case POINTER_TYPE:
589 abstract_declarator (t);
590 break;
592 case FUNCTION_TYPE:
593 pp_c_parameter_type_list (this, t);
594 direct_abstract_declarator (TREE_TYPE (t));
595 break;
597 case ARRAY_TYPE:
598 pp_c_left_bracket (this);
599 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
601 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
602 tree type = TREE_TYPE (maxval);
604 if (tree_fits_shwi_p (maxval))
605 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
606 else
607 expression (fold_build2 (PLUS_EXPR, type, maxval,
608 build_int_cst (type, 1)));
610 pp_c_right_bracket (this);
611 direct_abstract_declarator (TREE_TYPE (t));
612 break;
614 case IDENTIFIER_NODE:
615 case VOID_TYPE:
616 case BOOLEAN_TYPE:
617 case INTEGER_TYPE:
618 case REAL_TYPE:
619 case FIXED_POINT_TYPE:
620 case ENUMERAL_TYPE:
621 case RECORD_TYPE:
622 case UNION_TYPE:
623 case VECTOR_TYPE:
624 case COMPLEX_TYPE:
625 case TYPE_DECL:
626 break;
628 default:
629 pp_unsupported_tree (this, t);
630 break;
634 /* type-name:
635 specifier-qualifier-list abstract-declarator(opt) */
637 void
638 c_pretty_printer::type_id (tree t)
640 pp_c_specifier_qualifier_list (this, t);
641 abstract_declarator (t);
644 /* storage-class-specifier:
645 typedef
646 extern
647 static
648 auto
649 register */
651 void
652 c_pretty_printer::storage_class_specifier (tree t)
654 if (TREE_CODE (t) == TYPE_DECL)
655 pp_c_ws_string (this, "typedef");
656 else if (DECL_P (t))
658 if (DECL_REGISTER (t))
659 pp_c_ws_string (this, "register");
660 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
661 pp_c_ws_string (this, "static");
665 /* function-specifier:
666 inline */
668 void
669 c_pretty_printer::function_specifier (tree t)
671 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
672 pp_c_ws_string (this, "inline");
675 /* declaration-specifiers:
676 storage-class-specifier declaration-specifiers(opt)
677 type-specifier declaration-specifiers(opt)
678 type-qualifier declaration-specifiers(opt)
679 function-specifier declaration-specifiers(opt) */
681 void
682 c_pretty_printer::declaration_specifiers (tree t)
684 storage_class_specifier (t);
685 function_specifier (t);
686 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
689 /* direct-declarator
690 identifier
691 ( declarator )
692 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
693 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
694 direct-declarator [ type-qualifier-list static assignment-expression ]
695 direct-declarator [ type-qualifier-list * ]
696 direct-declarator ( parameter-type-list )
697 direct-declarator ( identifier-list(opt) ) */
699 void
700 c_pretty_printer::direct_declarator (tree t)
702 switch (TREE_CODE (t))
704 case VAR_DECL:
705 case PARM_DECL:
706 case TYPE_DECL:
707 case FIELD_DECL:
708 case LABEL_DECL:
709 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
710 pp_c_tree_decl_identifier (this, t);
711 break;
713 case ARRAY_TYPE:
714 case POINTER_TYPE:
715 abstract_declarator (TREE_TYPE (t));
716 break;
718 case FUNCTION_TYPE:
719 pp_parameter_list (this, t);
720 abstract_declarator (TREE_TYPE (t));
721 break;
723 case FUNCTION_DECL:
724 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
725 pp_c_tree_decl_identifier (this, t);
726 if (flags & pp_c_flag_abstract)
727 abstract_declarator (TREE_TYPE (t));
728 else
730 pp_parameter_list (this, t);
731 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
733 break;
735 case INTEGER_TYPE:
736 case REAL_TYPE:
737 case FIXED_POINT_TYPE:
738 case ENUMERAL_TYPE:
739 case UNION_TYPE:
740 case RECORD_TYPE:
741 break;
743 default:
744 pp_unsupported_tree (this, t);
745 break;
750 /* declarator:
751 pointer(opt) direct-declarator */
753 void
754 c_pretty_printer::declarator (tree t)
756 switch (TREE_CODE (t))
758 case INTEGER_TYPE:
759 case REAL_TYPE:
760 case FIXED_POINT_TYPE:
761 case ENUMERAL_TYPE:
762 case UNION_TYPE:
763 case RECORD_TYPE:
764 break;
766 case VAR_DECL:
767 case PARM_DECL:
768 case FIELD_DECL:
769 case ARRAY_TYPE:
770 case FUNCTION_TYPE:
771 case FUNCTION_DECL:
772 case TYPE_DECL:
773 direct_declarator (t);
774 break;
777 default:
778 pp_unsupported_tree (this, t);
779 break;
783 /* declaration:
784 declaration-specifiers init-declarator-list(opt) ; */
786 void
787 c_pretty_printer::declaration (tree t)
789 declaration_specifiers (t);
790 pp_c_init_declarator (this, t);
793 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
795 void
796 pp_c_attributes (c_pretty_printer *pp, tree attributes)
798 if (attributes == NULL_TREE)
799 return;
801 pp_c_ws_string (pp, "__attribute__");
802 pp_c_left_paren (pp);
803 pp_c_left_paren (pp);
804 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
806 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
807 if (TREE_VALUE (attributes))
808 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
810 if (TREE_CHAIN (attributes))
811 pp_separate_with (pp, ',');
813 pp_c_right_paren (pp);
814 pp_c_right_paren (pp);
817 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
818 marked to be displayed on disgnostic. */
820 void
821 pp_c_attributes_display (c_pretty_printer *pp, tree a)
823 bool is_first = true;
825 if (a == NULL_TREE)
826 return;
828 for (; a != NULL_TREE; a = TREE_CHAIN (a))
830 const struct attribute_spec *as;
831 as = lookup_attribute_spec (TREE_PURPOSE (a));
832 if (!as || as->affects_type_identity == false)
833 continue;
834 if (is_first)
836 pp_c_ws_string (pp, "__attribute__");
837 pp_c_left_paren (pp);
838 pp_c_left_paren (pp);
839 is_first = false;
841 else
843 pp_separate_with (pp, ',');
845 pp_tree_identifier (pp, TREE_PURPOSE (a));
846 if (TREE_VALUE (a))
847 pp_c_call_argument_list (pp, TREE_VALUE (a));
850 if (!is_first)
852 pp_c_right_paren (pp);
853 pp_c_right_paren (pp);
854 pp_c_whitespace (pp);
858 /* function-definition:
859 declaration-specifiers declarator compound-statement */
861 void
862 pp_c_function_definition (c_pretty_printer *pp, tree t)
864 pp->declaration_specifiers (t);
865 pp->declarator (t);
866 pp_needs_newline (pp) = true;
867 pp->statement (DECL_SAVED_TREE (t));
868 pp_newline_and_flush (pp);
872 /* Expressions. */
874 /* Print out a c-char. This is called solely for characters which are
875 in the *target* execution character set. We ought to convert them
876 back to the *host* execution character set before printing, but we
877 have no way to do this at present. A decent compromise is to print
878 all characters as if they were in the host execution character set,
879 and not attempt to recover any named escape characters, but render
880 all unprintables as octal escapes. If the host and target character
881 sets are the same, this produces relatively readable output. If they
882 are not the same, strings may appear as gibberish, but that's okay
883 (in fact, it may well be what the reader wants, e.g. if they are looking
884 to see if conversion to the target character set happened correctly).
886 A special case: we need to prefix \, ", and ' with backslashes. It is
887 correct to do so for the *host*'s \, ", and ', because the rest of the
888 file appears in the host character set. */
890 static void
891 pp_c_char (c_pretty_printer *pp, int c)
893 if (ISPRINT (c))
895 switch (c)
897 case '\\': pp_string (pp, "\\\\"); break;
898 case '\'': pp_string (pp, "\\\'"); break;
899 case '\"': pp_string (pp, "\\\""); break;
900 default: pp_character (pp, c);
903 else
904 pp_scalar (pp, "\\%03o", (unsigned) c);
907 /* Print out a STRING literal. */
909 void
910 pp_c_string_literal (c_pretty_printer *pp, tree s)
912 const char *p = TREE_STRING_POINTER (s);
913 int n = TREE_STRING_LENGTH (s) - 1;
914 int i;
915 pp_doublequote (pp);
916 for (i = 0; i < n; ++i)
917 pp_c_char (pp, p[i]);
918 pp_doublequote (pp);
921 /* Pretty-print a VOID_CST (void_node). */
923 static void
924 pp_c_void_constant (c_pretty_printer *pp)
926 pp_c_type_cast (pp, void_type_node);
927 pp_string (pp, "0");
930 /* Pretty-print an INTEGER literal. */
932 static void
933 pp_c_integer_constant (c_pretty_printer *pp, tree i)
935 int idx;
937 /* We are going to compare the type of I to other types using
938 pointer comparison so we need to use its canonical type. */
939 tree type =
940 TYPE_CANONICAL (TREE_TYPE (i))
941 ? TYPE_CANONICAL (TREE_TYPE (i))
942 : TREE_TYPE (i);
944 if (tree_fits_shwi_p (i))
945 pp_wide_integer (pp, tree_to_shwi (i));
946 else if (tree_fits_uhwi_p (i))
947 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
948 else
950 wide_int wi = i;
952 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
954 pp_minus (pp);
955 wi = -wi;
957 print_hex (wi, pp_buffer (pp)->digit_buffer);
958 pp_string (pp, pp_buffer (pp)->digit_buffer);
960 if (TYPE_UNSIGNED (type))
961 pp_character (pp, 'u');
962 if (type == long_integer_type_node || type == long_unsigned_type_node)
963 pp_character (pp, 'l');
964 else if (type == long_long_integer_type_node
965 || type == long_long_unsigned_type_node)
966 pp_string (pp, "ll");
967 else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++)
968 if (int_n_enabled_p[idx])
970 char buf[2+20];
971 if (type == int_n_trees[idx].signed_type
972 || type == int_n_trees[idx].unsigned_type)
974 sprintf (buf, "I%d", int_n_data[idx].bitsize);
975 pp_string (pp, buf);
980 /* Print out a CHARACTER literal. */
982 static void
983 pp_c_character_constant (c_pretty_printer *pp, tree c)
985 pp_quote (pp);
986 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
987 pp_quote (pp);
990 /* Print out a BOOLEAN literal. */
992 static void
993 pp_c_bool_constant (c_pretty_printer *pp, tree b)
995 if (b == boolean_false_node)
997 if (c_dialect_cxx ())
998 pp_c_ws_string (pp, "false");
999 else if (flag_isoc99)
1000 pp_c_ws_string (pp, "_False");
1001 else
1002 pp_unsupported_tree (pp, b);
1004 else if (b == boolean_true_node)
1006 if (c_dialect_cxx ())
1007 pp_c_ws_string (pp, "true");
1008 else if (flag_isoc99)
1009 pp_c_ws_string (pp, "_True");
1010 else
1011 pp_unsupported_tree (pp, b);
1013 else if (TREE_CODE (b) == INTEGER_CST)
1014 pp_c_integer_constant (pp, b);
1015 else
1016 pp_unsupported_tree (pp, b);
1019 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
1020 false; that means the value was obtained by a cast, in which case
1021 print out the type-id part of the cast-expression -- the casted value
1022 is then printed by pp_c_integer_literal. */
1024 static bool
1025 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1027 bool value_is_named = true;
1028 tree type = TREE_TYPE (e);
1029 tree value;
1031 /* Find the name of this constant. */
1032 for (value = TYPE_VALUES (type);
1033 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1034 value = TREE_CHAIN (value))
1037 if (value != NULL_TREE)
1038 pp->id_expression (TREE_PURPOSE (value));
1039 else
1041 /* Value must have been cast. */
1042 pp_c_type_cast (pp, type);
1043 value_is_named = false;
1046 return value_is_named;
1049 /* Print out a REAL value as a decimal-floating-constant. */
1051 static void
1052 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1054 const struct real_format *fmt
1055 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1057 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1058 bool is_decimal = floating_cst.decimal;
1060 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1061 log10(2) to 7 significant digits. */
1062 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1064 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1065 sizeof (pp_buffer (pp)->digit_buffer),
1066 max_digits10, 1);
1068 pp_string (pp, pp_buffer(pp)->digit_buffer);
1069 if (TREE_TYPE (r) == float_type_node)
1070 pp_character (pp, 'f');
1071 else if (TREE_TYPE (r) == long_double_type_node)
1072 pp_character (pp, 'l');
1073 else if (TREE_TYPE (r) == dfloat128_type_node)
1074 pp_string (pp, "dl");
1075 else if (TREE_TYPE (r) == dfloat64_type_node)
1076 pp_string (pp, "dd");
1077 else if (TREE_TYPE (r) == dfloat32_type_node)
1078 pp_string (pp, "df");
1081 /* Print out a FIXED value as a decimal-floating-constant. */
1083 static void
1084 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1086 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1087 sizeof (pp_buffer (pp)->digit_buffer));
1088 pp_string (pp, pp_buffer(pp)->digit_buffer);
1091 /* Pretty-print a compound literal expression. GNU extensions include
1092 vector constants. */
1094 static void
1095 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1097 tree type = TREE_TYPE (e);
1098 pp_c_type_cast (pp, type);
1100 switch (TREE_CODE (type))
1102 case RECORD_TYPE:
1103 case UNION_TYPE:
1104 case ARRAY_TYPE:
1105 case VECTOR_TYPE:
1106 case COMPLEX_TYPE:
1107 pp_c_brace_enclosed_initializer_list (pp, e);
1108 break;
1110 default:
1111 pp_unsupported_tree (pp, e);
1112 break;
1116 /* Pretty-print a COMPLEX_EXPR expression. */
1118 static void
1119 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1121 /* Handle a few common special cases, otherwise fallback
1122 to printing it as compound literal. */
1123 tree type = TREE_TYPE (e);
1124 tree realexpr = TREE_OPERAND (e, 0);
1125 tree imagexpr = TREE_OPERAND (e, 1);
1127 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1128 if (TREE_CODE (realexpr) == NOP_EXPR
1129 && TREE_CODE (imagexpr) == NOP_EXPR
1130 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1131 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1132 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1133 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1134 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1135 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1137 pp_c_type_cast (pp, type);
1138 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1139 return;
1142 /* Cast of an scalar expression to COMPLEX_TYPE. */
1143 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1144 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1146 pp_c_type_cast (pp, type);
1147 if (TREE_CODE (realexpr) == NOP_EXPR)
1148 realexpr = TREE_OPERAND (realexpr, 0);
1149 pp->expression (realexpr);
1150 return;
1153 pp_c_compound_literal (pp, e);
1156 /* constant:
1157 integer-constant
1158 floating-constant
1159 fixed-point-constant
1160 enumeration-constant
1161 character-constant */
1163 void
1164 c_pretty_printer::constant (tree e)
1166 const enum tree_code code = TREE_CODE (e);
1168 switch (code)
1170 case VOID_CST:
1171 pp_c_void_constant (this);
1172 break;
1174 case INTEGER_CST:
1176 tree type = TREE_TYPE (e);
1177 if (type == boolean_type_node)
1178 pp_c_bool_constant (this, e);
1179 else if (type == char_type_node)
1180 pp_c_character_constant (this, e);
1181 else if (TREE_CODE (type) == ENUMERAL_TYPE
1182 && pp_c_enumeration_constant (this, e))
1184 else
1185 pp_c_integer_constant (this, e);
1187 break;
1189 case REAL_CST:
1190 pp_c_floating_constant (this, e);
1191 break;
1193 case FIXED_CST:
1194 pp_c_fixed_constant (this, e);
1195 break;
1197 case STRING_CST:
1198 pp_c_string_literal (this, e);
1199 break;
1201 case COMPLEX_CST:
1202 /* Sometimes, we are confused and we think a complex literal
1203 is a constant. Such thing is a compound literal which
1204 grammatically belongs to postfix-expr production. */
1205 pp_c_compound_literal (this, e);
1206 break;
1208 default:
1209 pp_unsupported_tree (this, e);
1210 break;
1214 /* Pretty-print a string such as an identifier, without changing its
1215 encoding, preceded by whitespace is necessary. */
1217 void
1218 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1220 pp_c_maybe_whitespace (pp);
1221 pp_string (pp, str);
1222 pp->padding = pp_before;
1225 void
1226 c_pretty_printer::translate_string (const char *gmsgid)
1228 if (pp_translate_identifiers (this))
1229 pp_c_ws_string (this, _(gmsgid));
1230 else
1231 pp_c_ws_string (this, gmsgid);
1234 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1235 that need converting to the locale encoding, preceded by whitespace
1236 is necessary. */
1238 void
1239 pp_c_identifier (c_pretty_printer *pp, const char *id)
1241 pp_c_maybe_whitespace (pp);
1242 pp_identifier (pp, id);
1243 pp->padding = pp_before;
1246 /* Pretty-print a C primary-expression.
1247 primary-expression:
1248 identifier
1249 constant
1250 string-literal
1251 ( expression ) */
1253 void
1254 c_pretty_printer::primary_expression (tree e)
1256 switch (TREE_CODE (e))
1258 case VAR_DECL:
1259 case PARM_DECL:
1260 case FIELD_DECL:
1261 case CONST_DECL:
1262 case FUNCTION_DECL:
1263 case LABEL_DECL:
1264 pp_c_tree_decl_identifier (this, e);
1265 break;
1267 case IDENTIFIER_NODE:
1268 pp_c_tree_identifier (this, e);
1269 break;
1271 case ERROR_MARK:
1272 translate_string ("<erroneous-expression>");
1273 break;
1275 case RESULT_DECL:
1276 translate_string ("<return-value>");
1277 break;
1279 case VOID_CST:
1280 case INTEGER_CST:
1281 case REAL_CST:
1282 case FIXED_CST:
1283 case STRING_CST:
1284 constant (e);
1285 break;
1287 case TARGET_EXPR:
1288 pp_c_ws_string (this, "__builtin_memcpy");
1289 pp_c_left_paren (this);
1290 pp_ampersand (this);
1291 primary_expression (TREE_OPERAND (e, 0));
1292 pp_separate_with (this, ',');
1293 pp_ampersand (this);
1294 initializer (TREE_OPERAND (e, 1));
1295 if (TREE_OPERAND (e, 2))
1297 pp_separate_with (this, ',');
1298 expression (TREE_OPERAND (e, 2));
1300 pp_c_right_paren (this);
1301 break;
1303 default:
1304 /* FIXME: Make sure we won't get into an infinite loop. */
1305 pp_c_left_paren (this);
1306 expression (e);
1307 pp_c_right_paren (this);
1308 break;
1312 /* Print out a C initializer -- also support C compound-literals.
1313 initializer:
1314 assignment-expression:
1315 { initializer-list }
1316 { initializer-list , } */
1318 void
1319 c_pretty_printer::initializer (tree e)
1321 if (TREE_CODE (e) == CONSTRUCTOR)
1322 pp_c_brace_enclosed_initializer_list (this, e);
1323 else
1324 expression (e);
1327 /* init-declarator:
1328 declarator:
1329 declarator = initializer */
1331 void
1332 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1334 pp->declarator (t);
1335 /* We don't want to output function definitions here. There are handled
1336 elsewhere (and the syntactic form is bogus anyway). */
1337 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1339 tree init = DECL_INITIAL (t);
1340 /* This C++ bit is handled here because it is easier to do so.
1341 In templates, the C++ parser builds a TREE_LIST for a
1342 direct-initialization; the TREE_PURPOSE is the variable to
1343 initialize and the TREE_VALUE is the initializer. */
1344 if (TREE_CODE (init) == TREE_LIST)
1346 pp_c_left_paren (pp);
1347 pp->expression (TREE_VALUE (init));
1348 pp_right_paren (pp);
1350 else
1352 pp_space (pp);
1353 pp_equal (pp);
1354 pp_space (pp);
1355 pp->initializer (init);
1360 /* initializer-list:
1361 designation(opt) initializer
1362 initializer-list , designation(opt) initializer
1364 designation:
1365 designator-list =
1367 designator-list:
1368 designator
1369 designator-list designator
1371 designator:
1372 [ constant-expression ]
1373 identifier */
1375 static void
1376 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1378 tree type = TREE_TYPE (e);
1379 const enum tree_code code = TREE_CODE (type);
1381 if (TREE_CODE (e) == CONSTRUCTOR)
1383 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1384 return;
1387 switch (code)
1389 case RECORD_TYPE:
1390 case UNION_TYPE:
1391 case ARRAY_TYPE:
1393 tree init = TREE_OPERAND (e, 0);
1394 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1396 if (code == RECORD_TYPE || code == UNION_TYPE)
1398 pp_c_dot (pp);
1399 pp->primary_expression (TREE_PURPOSE (init));
1401 else
1403 pp_c_left_bracket (pp);
1404 if (TREE_PURPOSE (init))
1405 pp->constant (TREE_PURPOSE (init));
1406 pp_c_right_bracket (pp);
1408 pp_c_whitespace (pp);
1409 pp_equal (pp);
1410 pp_c_whitespace (pp);
1411 pp->initializer (TREE_VALUE (init));
1412 if (TREE_CHAIN (init))
1413 pp_separate_with (pp, ',');
1416 return;
1418 case VECTOR_TYPE:
1419 if (TREE_CODE (e) == VECTOR_CST)
1421 unsigned i;
1422 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1424 if (i > 0)
1425 pp_separate_with (pp, ',');
1426 pp->expression (VECTOR_CST_ELT (e, i));
1429 else
1430 break;
1431 return;
1433 case COMPLEX_TYPE:
1434 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1436 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1437 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1438 pp_separate_with (pp, ',');
1439 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1441 else
1442 break;
1443 return;
1445 default:
1446 break;
1449 pp_unsupported_tree (pp, type);
1452 /* Pretty-print a brace-enclosed initializer-list. */
1454 static void
1455 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1457 pp_c_left_brace (pp);
1458 pp_c_initializer_list (pp, l);
1459 pp_c_right_brace (pp);
1463 /* This is a convenient function, used to bridge gap between C and C++
1464 grammars.
1466 id-expression:
1467 identifier */
1469 void
1470 c_pretty_printer::id_expression (tree t)
1472 switch (TREE_CODE (t))
1474 case VAR_DECL:
1475 case PARM_DECL:
1476 case CONST_DECL:
1477 case TYPE_DECL:
1478 case FUNCTION_DECL:
1479 case FIELD_DECL:
1480 case LABEL_DECL:
1481 pp_c_tree_decl_identifier (this, t);
1482 break;
1484 case IDENTIFIER_NODE:
1485 pp_c_tree_identifier (this, t);
1486 break;
1488 default:
1489 pp_unsupported_tree (this, t);
1490 break;
1494 /* postfix-expression:
1495 primary-expression
1496 postfix-expression [ expression ]
1497 postfix-expression ( argument-expression-list(opt) )
1498 postfix-expression . identifier
1499 postfix-expression -> identifier
1500 postfix-expression ++
1501 postfix-expression --
1502 ( type-name ) { initializer-list }
1503 ( type-name ) { initializer-list , } */
1505 void
1506 c_pretty_printer::postfix_expression (tree e)
1508 enum tree_code code = TREE_CODE (e);
1509 switch (code)
1511 case POSTINCREMENT_EXPR:
1512 case POSTDECREMENT_EXPR:
1513 postfix_expression (TREE_OPERAND (e, 0));
1514 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1515 break;
1517 case ARRAY_REF:
1518 postfix_expression (TREE_OPERAND (e, 0));
1519 pp_c_left_bracket (this);
1520 expression (TREE_OPERAND (e, 1));
1521 pp_c_right_bracket (this);
1522 break;
1524 case ARRAY_NOTATION_REF:
1525 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1526 pp_c_left_bracket (this);
1527 expression (ARRAY_NOTATION_START (e));
1528 pp_colon (this);
1529 expression (ARRAY_NOTATION_LENGTH (e));
1530 pp_colon (this);
1531 expression (ARRAY_NOTATION_STRIDE (e));
1532 pp_c_right_bracket (this);
1533 break;
1535 case CALL_EXPR:
1537 call_expr_arg_iterator iter;
1538 tree arg;
1539 postfix_expression (CALL_EXPR_FN (e));
1540 pp_c_left_paren (this);
1541 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1543 expression (arg);
1544 if (more_call_expr_args_p (&iter))
1545 pp_separate_with (this, ',');
1547 pp_c_right_paren (this);
1548 break;
1551 case UNORDERED_EXPR:
1552 pp_c_ws_string (this, flag_isoc99
1553 ? "isunordered"
1554 : "__builtin_isunordered");
1555 goto two_args_fun;
1557 case ORDERED_EXPR:
1558 pp_c_ws_string (this, flag_isoc99
1559 ? "!isunordered"
1560 : "!__builtin_isunordered");
1561 goto two_args_fun;
1563 case UNLT_EXPR:
1564 pp_c_ws_string (this, flag_isoc99
1565 ? "!isgreaterequal"
1566 : "!__builtin_isgreaterequal");
1567 goto two_args_fun;
1569 case UNLE_EXPR:
1570 pp_c_ws_string (this, flag_isoc99
1571 ? "!isgreater"
1572 : "!__builtin_isgreater");
1573 goto two_args_fun;
1575 case UNGT_EXPR:
1576 pp_c_ws_string (this, flag_isoc99
1577 ? "!islessequal"
1578 : "!__builtin_islessequal");
1579 goto two_args_fun;
1581 case UNGE_EXPR:
1582 pp_c_ws_string (this, flag_isoc99
1583 ? "!isless"
1584 : "!__builtin_isless");
1585 goto two_args_fun;
1587 case UNEQ_EXPR:
1588 pp_c_ws_string (this, flag_isoc99
1589 ? "!islessgreater"
1590 : "!__builtin_islessgreater");
1591 goto two_args_fun;
1593 case LTGT_EXPR:
1594 pp_c_ws_string (this, flag_isoc99
1595 ? "islessgreater"
1596 : "__builtin_islessgreater");
1597 goto two_args_fun;
1599 two_args_fun:
1600 pp_c_left_paren (this);
1601 expression (TREE_OPERAND (e, 0));
1602 pp_separate_with (this, ',');
1603 expression (TREE_OPERAND (e, 1));
1604 pp_c_right_paren (this);
1605 break;
1607 case ABS_EXPR:
1608 pp_c_ws_string (this, "__builtin_abs");
1609 pp_c_left_paren (this);
1610 expression (TREE_OPERAND (e, 0));
1611 pp_c_right_paren (this);
1612 break;
1614 case COMPONENT_REF:
1616 tree object = TREE_OPERAND (e, 0);
1617 if (TREE_CODE (object) == INDIRECT_REF)
1619 postfix_expression (TREE_OPERAND (object, 0));
1620 pp_c_arrow (this);
1622 else
1624 postfix_expression (object);
1625 pp_c_dot (this);
1627 expression (TREE_OPERAND (e, 1));
1629 break;
1631 case BIT_FIELD_REF:
1633 tree type = TREE_TYPE (e);
1635 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1636 if (type
1637 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1639 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1640 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1641 if ((bitpos % size) == 0)
1643 pp_c_left_paren (this);
1644 pp_c_left_paren (this);
1645 type_id (type);
1646 pp_c_star (this);
1647 pp_c_right_paren (this);
1648 pp_c_ampersand (this);
1649 expression (TREE_OPERAND (e, 0));
1650 pp_c_right_paren (this);
1651 pp_c_left_bracket (this);
1652 pp_wide_integer (this, bitpos / size);
1653 pp_c_right_bracket (this);
1654 break;
1657 pp_unsupported_tree (this, e);
1659 break;
1661 case MEM_REF:
1662 expression (e);
1663 break;
1665 case COMPLEX_CST:
1666 case VECTOR_CST:
1667 pp_c_compound_literal (this, e);
1668 break;
1670 case COMPLEX_EXPR:
1671 pp_c_complex_expr (this, e);
1672 break;
1674 case COMPOUND_LITERAL_EXPR:
1675 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1676 /* Fall through. */
1677 case CONSTRUCTOR:
1678 initializer (e);
1679 break;
1681 case VA_ARG_EXPR:
1682 pp_c_ws_string (this, "__builtin_va_arg");
1683 pp_c_left_paren (this);
1684 assignment_expression (TREE_OPERAND (e, 0));
1685 pp_separate_with (this, ',');
1686 type_id (TREE_TYPE (e));
1687 pp_c_right_paren (this);
1688 break;
1690 case ADDR_EXPR:
1691 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1693 id_expression (TREE_OPERAND (e, 0));
1694 break;
1696 /* else fall through. */
1698 default:
1699 primary_expression (e);
1700 break;
1704 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1706 void
1707 pp_c_expression_list (c_pretty_printer *pp, tree e)
1709 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1711 pp->expression (TREE_VALUE (e));
1712 if (TREE_CHAIN (e))
1713 pp_separate_with (pp, ',');
1717 /* Print out V, which contains the elements of a constructor. */
1719 void
1720 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1722 unsigned HOST_WIDE_INT ix;
1723 tree value;
1725 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1727 pp->expression (value);
1728 if (ix != vec_safe_length (v) - 1)
1729 pp_separate_with (pp, ',');
1733 /* Print out an expression-list in parens, as if it were the argument
1734 list to a function. */
1736 void
1737 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1739 pp_c_left_paren (pp);
1740 if (t && TREE_CODE (t) == TREE_LIST)
1741 pp_c_expression_list (pp, t);
1742 pp_c_right_paren (pp);
1745 /* unary-expression:
1746 postfix-expression
1747 ++ cast-expression
1748 -- cast-expression
1749 unary-operator cast-expression
1750 sizeof unary-expression
1751 sizeof ( type-id )
1753 unary-operator: one of
1754 * & + - ! ~
1756 GNU extensions.
1757 unary-expression:
1758 __alignof__ unary-expression
1759 __alignof__ ( type-id )
1760 __real__ unary-expression
1761 __imag__ unary-expression */
1763 void
1764 c_pretty_printer::unary_expression (tree e)
1766 enum tree_code code = TREE_CODE (e);
1767 switch (code)
1769 case PREINCREMENT_EXPR:
1770 case PREDECREMENT_EXPR:
1771 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1772 unary_expression (TREE_OPERAND (e, 0));
1773 break;
1775 case ADDR_EXPR:
1776 case INDIRECT_REF:
1777 case NEGATE_EXPR:
1778 case BIT_NOT_EXPR:
1779 case TRUTH_NOT_EXPR:
1780 case CONJ_EXPR:
1781 /* String literal are used by address. */
1782 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1783 pp_ampersand (this);
1784 else if (code == INDIRECT_REF)
1785 pp_c_star (this);
1786 else if (code == NEGATE_EXPR)
1787 pp_minus (this);
1788 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1789 pp_complement (this);
1790 else if (code == TRUTH_NOT_EXPR)
1791 pp_exclamation (this);
1792 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1793 break;
1795 case MEM_REF:
1796 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1797 && integer_zerop (TREE_OPERAND (e, 1)))
1798 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1799 else
1801 pp_c_star (this);
1802 if (!integer_zerop (TREE_OPERAND (e, 1)))
1804 pp_c_left_paren (this);
1805 if (!integer_onep (TYPE_SIZE_UNIT
1806 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1807 pp_c_type_cast (this, ptr_type_node);
1809 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1810 if (!integer_zerop (TREE_OPERAND (e, 1)))
1812 pp_plus (this);
1813 pp_c_integer_constant (this,
1814 fold_convert (ssizetype,
1815 TREE_OPERAND (e, 1)));
1816 pp_c_right_paren (this);
1819 break;
1821 case REALPART_EXPR:
1822 case IMAGPART_EXPR:
1823 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1824 pp_c_whitespace (this);
1825 unary_expression (TREE_OPERAND (e, 0));
1826 break;
1828 default:
1829 postfix_expression (e);
1830 break;
1834 /* cast-expression:
1835 unary-expression
1836 ( type-name ) cast-expression */
1838 void
1839 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1841 switch (TREE_CODE (e))
1843 case FLOAT_EXPR:
1844 case FIX_TRUNC_EXPR:
1845 CASE_CONVERT:
1846 case VIEW_CONVERT_EXPR:
1847 pp_c_type_cast (pp, TREE_TYPE (e));
1848 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1849 break;
1851 default:
1852 pp->unary_expression (e);
1856 /* multiplicative-expression:
1857 cast-expression
1858 multiplicative-expression * cast-expression
1859 multiplicative-expression / cast-expression
1860 multiplicative-expression % cast-expression */
1862 void
1863 c_pretty_printer::multiplicative_expression (tree e)
1865 enum tree_code code = TREE_CODE (e);
1866 switch (code)
1868 case MULT_EXPR:
1869 case TRUNC_DIV_EXPR:
1870 case TRUNC_MOD_EXPR:
1871 multiplicative_expression (TREE_OPERAND (e, 0));
1872 pp_c_whitespace (this);
1873 if (code == MULT_EXPR)
1874 pp_c_star (this);
1875 else if (code == TRUNC_DIV_EXPR)
1876 pp_slash (this);
1877 else
1878 pp_modulo (this);
1879 pp_c_whitespace (this);
1880 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1881 break;
1883 default:
1884 pp_c_cast_expression (this, e);
1885 break;
1889 /* additive-expression:
1890 multiplicative-expression
1891 additive-expression + multiplicative-expression
1892 additive-expression - multiplicative-expression */
1894 static void
1895 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1897 enum tree_code code = TREE_CODE (e);
1898 switch (code)
1900 case POINTER_PLUS_EXPR:
1901 case PLUS_EXPR:
1902 case MINUS_EXPR:
1903 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1904 pp_c_whitespace (pp);
1905 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1906 pp_plus (pp);
1907 else
1908 pp_minus (pp);
1909 pp_c_whitespace (pp);
1910 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1911 break;
1913 default:
1914 pp->multiplicative_expression (e);
1915 break;
1919 /* additive-expression:
1920 additive-expression
1921 shift-expression << additive-expression
1922 shift-expression >> additive-expression */
1924 static void
1925 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1927 enum tree_code code = TREE_CODE (e);
1928 switch (code)
1930 case LSHIFT_EXPR:
1931 case RSHIFT_EXPR:
1932 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1933 pp_c_whitespace (pp);
1934 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1935 pp_c_whitespace (pp);
1936 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1937 break;
1939 default:
1940 pp_c_additive_expression (pp, e);
1944 /* relational-expression:
1945 shift-expression
1946 relational-expression < shift-expression
1947 relational-expression > shift-expression
1948 relational-expression <= shift-expression
1949 relational-expression >= shift-expression */
1951 static void
1952 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1954 enum tree_code code = TREE_CODE (e);
1955 switch (code)
1957 case LT_EXPR:
1958 case GT_EXPR:
1959 case LE_EXPR:
1960 case GE_EXPR:
1961 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1962 pp_c_whitespace (pp);
1963 if (code == LT_EXPR)
1964 pp_less (pp);
1965 else if (code == GT_EXPR)
1966 pp_greater (pp);
1967 else if (code == LE_EXPR)
1968 pp_less_equal (pp);
1969 else if (code == GE_EXPR)
1970 pp_greater_equal (pp);
1971 pp_c_whitespace (pp);
1972 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1973 break;
1975 default:
1976 pp_c_shift_expression (pp, e);
1977 break;
1981 /* equality-expression:
1982 relational-expression
1983 equality-expression == relational-expression
1984 equality-equality != relational-expression */
1986 static void
1987 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1989 enum tree_code code = TREE_CODE (e);
1990 switch (code)
1992 case EQ_EXPR:
1993 case NE_EXPR:
1994 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1995 pp_c_whitespace (pp);
1996 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1997 pp_c_whitespace (pp);
1998 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1999 break;
2001 default:
2002 pp_c_relational_expression (pp, e);
2003 break;
2007 /* AND-expression:
2008 equality-expression
2009 AND-expression & equality-equality */
2011 static void
2012 pp_c_and_expression (c_pretty_printer *pp, tree e)
2014 if (TREE_CODE (e) == BIT_AND_EXPR)
2016 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2017 pp_c_whitespace (pp);
2018 pp_ampersand (pp);
2019 pp_c_whitespace (pp);
2020 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2022 else
2023 pp_c_equality_expression (pp, e);
2026 /* exclusive-OR-expression:
2027 AND-expression
2028 exclusive-OR-expression ^ AND-expression */
2030 static void
2031 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2033 if (TREE_CODE (e) == BIT_XOR_EXPR
2034 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2036 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2037 if (TREE_CODE (e) == BIT_XOR_EXPR)
2038 pp_c_maybe_whitespace (pp);
2039 else
2040 pp_c_whitespace (pp);
2041 pp_carret (pp);
2042 pp_c_whitespace (pp);
2043 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2045 else
2046 pp_c_and_expression (pp, e);
2049 /* inclusive-OR-expression:
2050 exclusive-OR-expression
2051 inclusive-OR-expression | exclusive-OR-expression */
2053 static void
2054 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2056 if (TREE_CODE (e) == BIT_IOR_EXPR)
2058 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2059 pp_c_whitespace (pp);
2060 pp_bar (pp);
2061 pp_c_whitespace (pp);
2062 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2064 else
2065 pp_c_exclusive_or_expression (pp, e);
2068 /* logical-AND-expression:
2069 inclusive-OR-expression
2070 logical-AND-expression && inclusive-OR-expression */
2072 static void
2073 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2075 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2076 || TREE_CODE (e) == TRUTH_AND_EXPR)
2078 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2079 pp_c_whitespace (pp);
2080 pp_ampersand_ampersand (pp);
2081 pp_c_whitespace (pp);
2082 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2084 else
2085 pp_c_inclusive_or_expression (pp, e);
2088 /* logical-OR-expression:
2089 logical-AND-expression
2090 logical-OR-expression || logical-AND-expression */
2092 void
2093 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2095 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2096 || TREE_CODE (e) == TRUTH_OR_EXPR)
2098 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2099 pp_c_whitespace (pp);
2100 pp_bar_bar (pp);
2101 pp_c_whitespace (pp);
2102 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2104 else
2105 pp_c_logical_and_expression (pp, e);
2108 /* conditional-expression:
2109 logical-OR-expression
2110 logical-OR-expression ? expression : conditional-expression */
2112 void
2113 c_pretty_printer::conditional_expression (tree e)
2115 if (TREE_CODE (e) == COND_EXPR)
2117 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2118 pp_c_whitespace (this);
2119 pp_question (this);
2120 pp_c_whitespace (this);
2121 expression (TREE_OPERAND (e, 1));
2122 pp_c_whitespace (this);
2123 pp_colon (this);
2124 pp_c_whitespace (this);
2125 conditional_expression (TREE_OPERAND (e, 2));
2127 else
2128 pp_c_logical_or_expression (this, e);
2132 /* assignment-expression:
2133 conditional-expression
2134 unary-expression assignment-operator assignment-expression
2136 assignment-expression: one of
2137 = *= /= %= += -= >>= <<= &= ^= |= */
2139 void
2140 c_pretty_printer::assignment_expression (tree e)
2142 if (TREE_CODE (e) == MODIFY_EXPR
2143 || TREE_CODE (e) == INIT_EXPR)
2145 unary_expression (TREE_OPERAND (e, 0));
2146 pp_c_whitespace (this);
2147 pp_equal (this);
2148 pp_space (this);
2149 expression (TREE_OPERAND (e, 1));
2151 else
2152 conditional_expression (e);
2155 /* expression:
2156 assignment-expression
2157 expression , assignment-expression
2159 Implementation note: instead of going through the usual recursion
2160 chain, I take the liberty of dispatching nodes to the appropriate
2161 functions. This makes some redundancy, but it worths it. That also
2162 prevents a possible infinite recursion between primary_expression ()
2163 and expression (). */
2165 void
2166 c_pretty_printer::expression (tree e)
2168 switch (TREE_CODE (e))
2170 case VOID_CST:
2171 pp_c_void_constant (this);
2172 break;
2174 case INTEGER_CST:
2175 pp_c_integer_constant (this, e);
2176 break;
2178 case REAL_CST:
2179 pp_c_floating_constant (this, e);
2180 break;
2182 case FIXED_CST:
2183 pp_c_fixed_constant (this, e);
2184 break;
2186 case STRING_CST:
2187 pp_c_string_literal (this, e);
2188 break;
2190 case IDENTIFIER_NODE:
2191 case FUNCTION_DECL:
2192 case VAR_DECL:
2193 case CONST_DECL:
2194 case PARM_DECL:
2195 case RESULT_DECL:
2196 case FIELD_DECL:
2197 case LABEL_DECL:
2198 case ERROR_MARK:
2199 primary_expression (e);
2200 break;
2202 case SSA_NAME:
2203 if (SSA_NAME_VAR (e)
2204 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2205 expression (SSA_NAME_VAR (e));
2206 else
2207 translate_string ("<unknown>");
2208 break;
2210 case POSTINCREMENT_EXPR:
2211 case POSTDECREMENT_EXPR:
2212 case ARRAY_REF:
2213 case ARRAY_NOTATION_REF:
2214 case CALL_EXPR:
2215 case COMPONENT_REF:
2216 case BIT_FIELD_REF:
2217 case COMPLEX_CST:
2218 case COMPLEX_EXPR:
2219 case VECTOR_CST:
2220 case ORDERED_EXPR:
2221 case UNORDERED_EXPR:
2222 case LTGT_EXPR:
2223 case UNEQ_EXPR:
2224 case UNLE_EXPR:
2225 case UNLT_EXPR:
2226 case UNGE_EXPR:
2227 case UNGT_EXPR:
2228 case ABS_EXPR:
2229 case CONSTRUCTOR:
2230 case COMPOUND_LITERAL_EXPR:
2231 case VA_ARG_EXPR:
2232 postfix_expression (e);
2233 break;
2235 case CONJ_EXPR:
2236 case ADDR_EXPR:
2237 case INDIRECT_REF:
2238 case MEM_REF:
2239 case NEGATE_EXPR:
2240 case BIT_NOT_EXPR:
2241 case TRUTH_NOT_EXPR:
2242 case PREINCREMENT_EXPR:
2243 case PREDECREMENT_EXPR:
2244 case REALPART_EXPR:
2245 case IMAGPART_EXPR:
2246 unary_expression (e);
2247 break;
2249 case FLOAT_EXPR:
2250 case FIX_TRUNC_EXPR:
2251 CASE_CONVERT:
2252 case VIEW_CONVERT_EXPR:
2253 pp_c_cast_expression (this, e);
2254 break;
2256 case MULT_EXPR:
2257 case TRUNC_MOD_EXPR:
2258 case TRUNC_DIV_EXPR:
2259 multiplicative_expression (e);
2260 break;
2262 case LSHIFT_EXPR:
2263 case RSHIFT_EXPR:
2264 pp_c_shift_expression (this, e);
2265 break;
2267 case LT_EXPR:
2268 case GT_EXPR:
2269 case LE_EXPR:
2270 case GE_EXPR:
2271 pp_c_relational_expression (this, e);
2272 break;
2274 case BIT_AND_EXPR:
2275 pp_c_and_expression (this, e);
2276 break;
2278 case BIT_XOR_EXPR:
2279 case TRUTH_XOR_EXPR:
2280 pp_c_exclusive_or_expression (this, e);
2281 break;
2283 case BIT_IOR_EXPR:
2284 pp_c_inclusive_or_expression (this, e);
2285 break;
2287 case TRUTH_ANDIF_EXPR:
2288 case TRUTH_AND_EXPR:
2289 pp_c_logical_and_expression (this, e);
2290 break;
2292 case TRUTH_ORIF_EXPR:
2293 case TRUTH_OR_EXPR:
2294 pp_c_logical_or_expression (this, e);
2295 break;
2297 case EQ_EXPR:
2298 case NE_EXPR:
2299 pp_c_equality_expression (this, e);
2300 break;
2302 case COND_EXPR:
2303 conditional_expression (e);
2304 break;
2306 case POINTER_PLUS_EXPR:
2307 case PLUS_EXPR:
2308 case MINUS_EXPR:
2309 pp_c_additive_expression (this, e);
2310 break;
2312 case MODIFY_EXPR:
2313 case INIT_EXPR:
2314 assignment_expression (e);
2315 break;
2317 case COMPOUND_EXPR:
2318 pp_c_left_paren (this);
2319 expression (TREE_OPERAND (e, 0));
2320 pp_separate_with (this, ',');
2321 assignment_expression (TREE_OPERAND (e, 1));
2322 pp_c_right_paren (this);
2323 break;
2325 case NON_LVALUE_EXPR:
2326 case SAVE_EXPR:
2327 expression (TREE_OPERAND (e, 0));
2328 break;
2330 case TARGET_EXPR:
2331 postfix_expression (TREE_OPERAND (e, 1));
2332 break;
2334 case BIND_EXPR:
2335 case GOTO_EXPR:
2336 /* We don't yet have a way of dumping statements in a
2337 human-readable format. */
2338 pp_string (this, "({...})");
2339 break;
2341 case C_MAYBE_CONST_EXPR:
2342 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2343 break;
2345 default:
2346 pp_unsupported_tree (this, e);
2347 break;
2353 /* Statements. */
2355 void
2356 c_pretty_printer::statement (tree stmt)
2358 if (stmt == NULL)
2359 return;
2361 if (pp_needs_newline (this))
2362 pp_newline_and_indent (this, 0);
2364 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2368 /* Initialize the PRETTY-PRINTER for handling C codes. */
2370 c_pretty_printer::c_pretty_printer ()
2371 : pretty_printer (),
2372 offset_list (),
2373 flags ()
2375 type_specifier_seq = pp_c_specifier_qualifier_list;
2376 ptr_operator = pp_c_pointer;
2377 parameter_list = pp_c_parameter_type_list;
2381 /* Print the tree T in full, on file FILE. */
2383 void
2384 print_c_tree (FILE *file, tree t)
2386 c_pretty_printer pp;
2388 pp_needs_newline (&pp) = true;
2389 pp.buffer->stream = file;
2390 pp.statement (t);
2391 pp_newline_and_flush (&pp);
2394 /* Print the tree T in full, on stderr. */
2396 DEBUG_FUNCTION void
2397 debug_c_tree (tree t)
2399 print_c_tree (stderr, t);
2400 fputc ('\n', stderr);
2403 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2404 up of T's memory address. */
2406 void
2407 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2409 const char *name;
2411 gcc_assert (DECL_P (t));
2413 if (DECL_NAME (t))
2414 name = IDENTIFIER_POINTER (DECL_NAME (t));
2415 else
2417 static char xname[8];
2418 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2419 name = xname;
2422 pp_c_identifier (pp, name);