Merge trunk version 204659 into gupc branch.
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob1f95caeb8b23fc1fcefdba3588022f982d2f18d2
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "intl.h"
27 #include "c-pretty-print.h"
28 #include "tree-pretty-print.h"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
41 do { \
42 if ((PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
44 } while (0)
46 /* literal */
47 static void pp_c_char (c_pretty_printer *, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer *, tree);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
53 static void pp_c_additive_expression (c_pretty_printer *, tree);
54 static void pp_c_shift_expression (c_pretty_printer *, tree);
55 static void pp_c_relational_expression (c_pretty_printer *, tree);
56 static void pp_c_equality_expression (c_pretty_printer *, tree);
57 static void pp_c_and_expression (c_pretty_printer *, tree);
58 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
59 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62 /* declarations. */
65 /* Helper functions. */
67 void
68 pp_c_whitespace (c_pretty_printer *pp)
70 pp_space (pp);
71 pp->padding = pp_none;
74 void
75 pp_c_left_paren (c_pretty_printer *pp)
77 pp_left_paren (pp);
78 pp->padding = pp_none;
81 void
82 pp_c_right_paren (c_pretty_printer *pp)
84 pp_right_paren (pp);
85 pp->padding = pp_none;
88 void
89 pp_c_left_brace (c_pretty_printer *pp)
91 pp_left_brace (pp);
92 pp->padding = pp_none;
95 void
96 pp_c_right_brace (c_pretty_printer *pp)
98 pp_right_brace (pp);
99 pp->padding = pp_none;
102 void
103 pp_c_left_bracket (c_pretty_printer *pp)
105 pp_left_bracket (pp);
106 pp->padding = pp_none;
109 void
110 pp_c_right_bracket (c_pretty_printer *pp)
112 pp_right_bracket (pp);
113 pp->padding = pp_none;
116 void
117 pp_c_dot (c_pretty_printer *pp)
119 pp_dot (pp);
120 pp->padding = pp_none;
123 void
124 pp_c_ampersand (c_pretty_printer *pp)
126 pp_ampersand (pp);
127 pp->padding = pp_none;
130 void
131 pp_c_star (c_pretty_printer *pp)
133 pp_star (pp);
134 pp->padding = pp_none;
137 void
138 pp_c_arrow (c_pretty_printer *pp)
140 pp_arrow (pp);
141 pp->padding = pp_none;
144 void
145 pp_c_semicolon (c_pretty_printer *pp)
147 pp_semicolon (pp);
148 pp->padding = pp_none;
151 void
152 pp_c_complement (c_pretty_printer *pp)
154 pp_complement (pp);
155 pp->padding = pp_none;
158 void
159 pp_c_exclamation (c_pretty_printer *pp)
161 pp_exclamation (pp);
162 pp->padding = pp_none;
165 /* Print out the external representation of QUALIFIERS. */
167 void
168 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
170 const char *p = pp_last_position_in_text (pp);
171 bool previous = false;
173 if (!qualifiers)
174 return;
176 /* The C programming language does not have references, but it is much
177 simpler to handle those here rather than going through the same
178 logic in the C++ pretty-printer. */
179 if (p != NULL && (*p == '*' || *p == '&'))
180 pp_c_whitespace (pp);
182 if (qualifiers & TYPE_QUAL_ATOMIC)
184 pp_c_ws_string (pp, "_Atomic");
185 previous = true;
188 if (qualifiers & TYPE_QUAL_CONST)
190 if (previous)
191 pp_c_whitespace (pp);
192 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
193 previous = true;
196 if (qualifiers & TYPE_QUAL_VOLATILE)
198 if (previous)
199 pp_c_whitespace (pp);
200 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
201 previous = true;
204 if (qualifiers & TYPE_QUAL_RESTRICT)
206 if (previous)
207 pp_c_whitespace (pp);
208 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
209 ? "restrict" : "__restrict__"));
210 previous = true;
213 if (qualifiers & TYPE_QUAL_RELAXED)
215 if (previous)
216 pp_c_whitespace (pp);
217 pp_c_ws_string (pp, "relaxed");
218 previous = true;
221 if (qualifiers & TYPE_QUAL_STRICT)
223 if (previous)
224 pp_c_whitespace (pp);
225 pp_c_ws_string (pp, "strict");
226 previous = true;
229 if (qualifiers & TYPE_QUAL_SHARED)
231 if (previous)
232 pp_c_whitespace (pp);
233 pp_c_ws_string (pp, "shared");
234 previous = true;
239 /* Pretty-print T using the type-cast notation '( type-name )'. */
241 static void
242 pp_c_type_cast (c_pretty_printer *pp, tree t)
244 pp_c_left_paren (pp);
245 pp->type_id (t);
246 pp_c_right_paren (pp);
249 /* We're about to pretty-print a pointer type as indicated by T.
250 Output a whitespace, if needed, preparing for subsequent output. */
252 void
253 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
255 if (POINTER_TYPE_P (t))
257 tree pointee = strip_pointer_operator (TREE_TYPE (t));
258 if (TREE_CODE (pointee) != ARRAY_TYPE
259 && TREE_CODE (pointee) != FUNCTION_TYPE)
260 pp_c_whitespace (pp);
265 /* Declarations. */
267 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
268 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
269 of its type. Take care of possible extensions.
271 type-qualifier-list:
272 type-qualifier
273 type-qualifier-list type-qualifier
275 type-qualifier:
276 const
277 restrict -- C99
278 __restrict__ -- GNU C
279 address-space-qualifier -- GNU C
280 volatile
281 _Atomic -- C11
283 address-space-qualifier:
284 identifier -- GNU C */
286 void
287 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
289 int qualifiers;
291 if (!t || t == error_mark_node)
292 return;
294 if (!TYPE_P (t))
295 t = TREE_TYPE (t);
297 qualifiers = TYPE_QUALS (t);
298 pp_c_cv_qualifiers (pp, qualifiers,
299 TREE_CODE (t) == FUNCTION_TYPE);
301 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
303 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
304 pp_c_identifier (pp, as);
308 /* pointer:
309 * type-qualifier-list(opt)
310 * type-qualifier-list(opt) pointer */
312 static void
313 pp_c_pointer (c_pretty_printer *pp, tree t)
315 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
316 t = TREE_TYPE (t);
317 switch (TREE_CODE (t))
319 case POINTER_TYPE:
320 /* It is easier to handle C++ reference types here. */
321 case REFERENCE_TYPE:
322 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
323 pp_c_pointer (pp, TREE_TYPE (t));
324 if (TREE_CODE (t) == POINTER_TYPE)
325 pp_c_star (pp);
326 else
327 pp_c_ampersand (pp);
328 pp_c_type_qualifier_list (pp, t);
329 break;
331 /* ??? This node is now in GENERIC and so shouldn't be here. But
332 we'll fix that later. */
333 case DECL_EXPR:
334 pp->declaration (DECL_EXPR_DECL (t));
335 pp_needs_newline (pp) = true;
336 break;
338 default:
339 pp_unsupported_tree (pp, t);
343 /* simple-type-specifier:
344 type-specifier
346 type-specifier:
347 void
348 char
349 short
351 long
352 float
353 double
354 signed
355 unsigned
356 _Bool -- C99
357 _Complex -- C99
358 _Imaginary -- C99
359 struct-or-union-specifier
360 enum-specifier
361 typedef-name.
363 GNU extensions.
364 simple-type-specifier:
365 __complex__
366 __vector__ */
368 void
369 c_pretty_printer::simple_type_specifier (tree t)
371 const enum tree_code code = TREE_CODE (t);
372 switch (code)
374 case ERROR_MARK:
375 translate_string ("<type-error>");
376 break;
378 case IDENTIFIER_NODE:
379 pp_c_identifier (this, IDENTIFIER_POINTER (t));
380 break;
382 case VOID_TYPE:
383 case BOOLEAN_TYPE:
384 case INTEGER_TYPE:
385 case REAL_TYPE:
386 case FIXED_POINT_TYPE:
387 if (TYPE_NAME (t))
389 t = TYPE_NAME (t);
390 simple_type_specifier (t);
392 else
394 int prec = TYPE_PRECISION (t);
395 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
396 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
397 else
398 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
399 if (TYPE_NAME (t))
401 simple_type_specifier (t);
402 if (TYPE_PRECISION (t) != prec)
404 pp_colon (this);
405 pp_decimal_int (this, prec);
408 else
410 switch (code)
412 case INTEGER_TYPE:
413 translate_string (TYPE_UNSIGNED (t)
414 ? "<unnamed-unsigned:"
415 : "<unnamed-signed:");
416 break;
417 case REAL_TYPE:
418 translate_string ("<unnamed-float:");
419 break;
420 case FIXED_POINT_TYPE:
421 translate_string ("<unnamed-fixed:");
422 break;
423 default:
424 gcc_unreachable ();
426 pp_decimal_int (this, prec);
427 pp_greater (this);
430 break;
432 case TYPE_DECL:
433 if (DECL_NAME (t))
434 id_expression (t);
435 else
436 translate_string ("<typedef-error>");
437 break;
439 case UNION_TYPE:
440 case RECORD_TYPE:
441 case ENUMERAL_TYPE:
442 if (code == UNION_TYPE)
443 pp_c_ws_string (this, "union");
444 else if (code == RECORD_TYPE)
445 pp_c_ws_string (this, "struct");
446 else if (code == ENUMERAL_TYPE)
447 pp_c_ws_string (this, "enum");
448 else
449 translate_string ("<tag-error>");
451 if (TYPE_NAME (t))
452 id_expression (TYPE_NAME (t));
453 else
454 translate_string ("<anonymous>");
455 break;
457 default:
458 pp_unsupported_tree (this, t);
459 break;
463 /* specifier-qualifier-list:
464 type-specifier specifier-qualifier-list-opt
465 type-qualifier specifier-qualifier-list-opt
468 Implementation note: Because of the non-linearities in array or
469 function declarations, this routine prints not just the
470 specifier-qualifier-list of such entities or types of such entities,
471 but also the 'pointer' production part of their declarators. The
472 remaining part is done by declarator() or abstract_declarator(). */
474 void
475 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
477 const enum tree_code code = TREE_CODE (t);
479 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
480 pp_c_type_qualifier_list (pp, t);
481 switch (code)
483 case REFERENCE_TYPE:
484 case POINTER_TYPE:
486 /* Get the types-specifier of this type. */
487 tree pointee = strip_pointer_operator (TREE_TYPE (t));
488 pp_c_specifier_qualifier_list (pp, pointee);
489 if (TREE_CODE (pointee) == ARRAY_TYPE
490 || TREE_CODE (pointee) == FUNCTION_TYPE)
492 pp_c_whitespace (pp);
493 pp_c_left_paren (pp);
494 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
496 else if (!c_dialect_cxx ())
497 pp_c_whitespace (pp);
498 pp_ptr_operator (pp, t);
500 break;
502 case FUNCTION_TYPE:
503 case ARRAY_TYPE:
504 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
505 break;
507 case VECTOR_TYPE:
508 case COMPLEX_TYPE:
509 if (code == COMPLEX_TYPE)
510 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
511 ? "_Complex" : "__complex__"));
512 else if (code == VECTOR_TYPE)
514 pp_c_ws_string (pp, "__vector");
515 pp_c_left_paren (pp);
516 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
517 pp_c_right_paren (pp);
518 pp_c_whitespace (pp);
520 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
521 break;
523 default:
524 pp->simple_type_specifier (t);
525 break;
527 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
528 pp_c_type_qualifier_list (pp, t);
531 /* parameter-type-list:
532 parameter-list
533 parameter-list , ...
535 parameter-list:
536 parameter-declaration
537 parameter-list , parameter-declaration
539 parameter-declaration:
540 declaration-specifiers declarator
541 declaration-specifiers abstract-declarator(opt) */
543 void
544 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
546 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
547 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
548 pp_c_left_paren (pp);
549 if (parms == void_list_node)
550 pp_c_ws_string (pp, "void");
551 else
553 bool first = true;
554 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
556 if (!first)
557 pp_separate_with (pp, ',');
558 first = false;
559 pp->declaration_specifiers
560 (want_parm_decl ? parms : TREE_VALUE (parms));
561 if (want_parm_decl)
562 pp->declarator (parms);
563 else
564 pp->abstract_declarator (TREE_VALUE (parms));
567 pp_c_right_paren (pp);
570 /* abstract-declarator:
571 pointer
572 pointer(opt) direct-abstract-declarator */
574 void
575 c_pretty_printer::abstract_declarator (tree t)
577 if (TREE_CODE (t) == POINTER_TYPE)
579 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
580 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
581 pp_c_right_paren (this);
582 t = TREE_TYPE (t);
585 direct_abstract_declarator (t);
588 /* direct-abstract-declarator:
589 ( abstract-declarator )
590 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
591 direct-abstract-declarator(opt) [ * ]
592 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
594 void
595 c_pretty_printer::direct_abstract_declarator (tree t)
597 switch (TREE_CODE (t))
599 case POINTER_TYPE:
600 abstract_declarator (t);
601 break;
603 case FUNCTION_TYPE:
604 pp_c_parameter_type_list (this, t);
605 direct_abstract_declarator (TREE_TYPE (t));
606 break;
608 case ARRAY_TYPE:
609 pp_c_left_bracket (this);
610 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
612 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
613 tree type = TREE_TYPE (maxval);
615 if (host_integerp (maxval, 0))
616 pp_wide_integer (this, tree_low_cst (maxval, 0) + 1);
617 else
618 expression (fold_build2 (PLUS_EXPR, type, maxval,
619 build_int_cst (type, 1)));
621 pp_c_right_bracket (this);
622 direct_abstract_declarator (TREE_TYPE (t));
623 break;
625 case IDENTIFIER_NODE:
626 case VOID_TYPE:
627 case BOOLEAN_TYPE:
628 case INTEGER_TYPE:
629 case REAL_TYPE:
630 case FIXED_POINT_TYPE:
631 case ENUMERAL_TYPE:
632 case RECORD_TYPE:
633 case UNION_TYPE:
634 case VECTOR_TYPE:
635 case COMPLEX_TYPE:
636 case TYPE_DECL:
637 break;
639 default:
640 pp_unsupported_tree (this, t);
641 break;
645 /* type-name:
646 specifier-qualifier-list abstract-declarator(opt) */
648 void
649 c_pretty_printer::type_id (tree t)
651 pp_c_specifier_qualifier_list (this, t);
652 abstract_declarator (t);
655 /* storage-class-specifier:
656 typedef
657 extern
658 static
659 auto
660 register */
662 void
663 c_pretty_printer::storage_class_specifier (tree t)
665 if (TREE_CODE (t) == TYPE_DECL)
666 pp_c_ws_string (this, "typedef");
667 else if (DECL_P (t))
669 if (DECL_REGISTER (t))
670 pp_c_ws_string (this, "register");
671 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
672 pp_c_ws_string (this, "static");
676 /* function-specifier:
677 inline */
679 void
680 c_pretty_printer::function_specifier (tree t)
682 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
683 pp_c_ws_string (this, "inline");
686 /* declaration-specifiers:
687 storage-class-specifier declaration-specifiers(opt)
688 type-specifier declaration-specifiers(opt)
689 type-qualifier declaration-specifiers(opt)
690 function-specifier declaration-specifiers(opt) */
692 void
693 c_pretty_printer::declaration_specifiers (tree t)
695 storage_class_specifier (t);
696 function_specifier (t);
697 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
700 /* direct-declarator
701 identifier
702 ( declarator )
703 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
704 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
705 direct-declarator [ type-qualifier-list static assignment-expression ]
706 direct-declarator [ type-qualifier-list * ]
707 direct-declarator ( parameter-type-list )
708 direct-declarator ( identifier-list(opt) ) */
710 void
711 c_pretty_printer::direct_declarator (tree t)
713 switch (TREE_CODE (t))
715 case VAR_DECL:
716 case PARM_DECL:
717 case TYPE_DECL:
718 case FIELD_DECL:
719 case LABEL_DECL:
720 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
721 pp_c_tree_decl_identifier (this, t);
722 break;
724 case ARRAY_TYPE:
725 case POINTER_TYPE:
726 abstract_declarator (TREE_TYPE (t));
727 break;
729 case FUNCTION_TYPE:
730 pp_parameter_list (this, t);
731 abstract_declarator (TREE_TYPE (t));
732 break;
734 case FUNCTION_DECL:
735 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
736 pp_c_tree_decl_identifier (this, t);
737 if (flags & pp_c_flag_abstract)
738 abstract_declarator (TREE_TYPE (t));
739 else
741 pp_parameter_list (this, t);
742 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
744 break;
746 case INTEGER_TYPE:
747 case REAL_TYPE:
748 case FIXED_POINT_TYPE:
749 case ENUMERAL_TYPE:
750 case UNION_TYPE:
751 case RECORD_TYPE:
752 break;
754 default:
755 pp_unsupported_tree (this, t);
756 break;
761 /* declarator:
762 pointer(opt) direct-declarator */
764 void
765 c_pretty_printer::declarator (tree t)
767 switch (TREE_CODE (t))
769 case INTEGER_TYPE:
770 case REAL_TYPE:
771 case FIXED_POINT_TYPE:
772 case ENUMERAL_TYPE:
773 case UNION_TYPE:
774 case RECORD_TYPE:
775 break;
777 case VAR_DECL:
778 case PARM_DECL:
779 case FIELD_DECL:
780 case ARRAY_TYPE:
781 case FUNCTION_TYPE:
782 case FUNCTION_DECL:
783 case TYPE_DECL:
784 direct_declarator (t);
785 break;
788 default:
789 pp_unsupported_tree (this, t);
790 break;
794 /* declaration:
795 declaration-specifiers init-declarator-list(opt) ; */
797 void
798 c_pretty_printer::declaration (tree t)
800 declaration_specifiers (t);
801 pp_c_init_declarator (this, t);
804 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
806 void
807 pp_c_attributes (c_pretty_printer *pp, tree attributes)
809 if (attributes == NULL_TREE)
810 return;
812 pp_c_ws_string (pp, "__attribute__");
813 pp_c_left_paren (pp);
814 pp_c_left_paren (pp);
815 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
817 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
818 if (TREE_VALUE (attributes))
819 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
821 if (TREE_CHAIN (attributes))
822 pp_separate_with (pp, ',');
824 pp_c_right_paren (pp);
825 pp_c_right_paren (pp);
828 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
829 marked to be displayed on disgnostic. */
831 void
832 pp_c_attributes_display (c_pretty_printer *pp, tree a)
834 bool is_first = true;
836 if (a == NULL_TREE)
837 return;
839 for (; a != NULL_TREE; a = TREE_CHAIN (a))
841 const struct attribute_spec *as;
842 as = lookup_attribute_spec (TREE_PURPOSE (a));
843 if (!as || as->affects_type_identity == false)
844 continue;
845 if (is_first)
847 pp_c_ws_string (pp, "__attribute__");
848 pp_c_left_paren (pp);
849 pp_c_left_paren (pp);
850 is_first = false;
852 else
854 pp_separate_with (pp, ',');
856 pp_tree_identifier (pp, TREE_PURPOSE (a));
857 if (TREE_VALUE (a))
858 pp_c_call_argument_list (pp, TREE_VALUE (a));
861 if (!is_first)
863 pp_c_right_paren (pp);
864 pp_c_right_paren (pp);
865 pp_c_whitespace (pp);
869 /* function-definition:
870 declaration-specifiers declarator compound-statement */
872 void
873 pp_c_function_definition (c_pretty_printer *pp, tree t)
875 pp->declaration_specifiers (t);
876 pp->declarator (t);
877 pp_needs_newline (pp) = true;
878 pp->statement (DECL_SAVED_TREE (t));
879 pp_newline_and_flush (pp);
883 /* Expressions. */
885 /* Print out a c-char. This is called solely for characters which are
886 in the *target* execution character set. We ought to convert them
887 back to the *host* execution character set before printing, but we
888 have no way to do this at present. A decent compromise is to print
889 all characters as if they were in the host execution character set,
890 and not attempt to recover any named escape characters, but render
891 all unprintables as octal escapes. If the host and target character
892 sets are the same, this produces relatively readable output. If they
893 are not the same, strings may appear as gibberish, but that's okay
894 (in fact, it may well be what the reader wants, e.g. if they are looking
895 to see if conversion to the target character set happened correctly).
897 A special case: we need to prefix \, ", and ' with backslashes. It is
898 correct to do so for the *host*'s \, ", and ', because the rest of the
899 file appears in the host character set. */
901 static void
902 pp_c_char (c_pretty_printer *pp, int c)
904 if (ISPRINT (c))
906 switch (c)
908 case '\\': pp_string (pp, "\\\\"); break;
909 case '\'': pp_string (pp, "\\\'"); break;
910 case '\"': pp_string (pp, "\\\""); break;
911 default: pp_character (pp, c);
914 else
915 pp_scalar (pp, "\\%03o", (unsigned) c);
918 /* Print out a STRING literal. */
920 void
921 pp_c_string_literal (c_pretty_printer *pp, tree s)
923 const char *p = TREE_STRING_POINTER (s);
924 int n = TREE_STRING_LENGTH (s) - 1;
925 int i;
926 pp_doublequote (pp);
927 for (i = 0; i < n; ++i)
928 pp_c_char (pp, p[i]);
929 pp_doublequote (pp);
932 /* Pretty-print an INTEGER literal. */
934 static void
935 pp_c_integer_constant (c_pretty_printer *pp, tree i)
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 (host_integerp (i, 0))
945 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
946 else if (host_integerp (i, 1))
947 pp_unsigned_wide_integer (pp, TREE_INT_CST_LOW (i));
948 else
950 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
951 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
952 if (tree_int_cst_sgn (i) < 0)
954 pp_minus (pp);
955 high = ~high + !low;
956 low = -low;
958 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
959 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
960 pp_string (pp, pp_buffer (pp)->digit_buffer);
962 if (TYPE_UNSIGNED (type))
963 pp_character (pp, 'u');
964 if (type == long_integer_type_node || type == long_unsigned_type_node)
965 pp_character (pp, 'l');
966 else if (type == long_long_integer_type_node
967 || type == long_long_unsigned_type_node)
968 pp_string (pp, "ll");
969 else if (type == int128_integer_type_node
970 || type == int128_unsigned_type_node)
971 pp_string (pp, "I128");
974 /* Print out a CHARACTER literal. */
976 static void
977 pp_c_character_constant (c_pretty_printer *pp, tree c)
979 tree type = TREE_TYPE (c);
980 if (type == wchar_type_node)
981 pp_character (pp, 'L');
982 pp_quote (pp);
983 if (host_integerp (c, TYPE_UNSIGNED (type)))
984 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
985 else
986 pp_scalar (pp, "\\x%x", (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 INTEGER_CST:
1172 tree type = TREE_TYPE (e);
1173 if (type == boolean_type_node)
1174 pp_c_bool_constant (this, e);
1175 else if (type == char_type_node)
1176 pp_c_character_constant (this, e);
1177 else if (TREE_CODE (type) == ENUMERAL_TYPE
1178 && pp_c_enumeration_constant (this, e))
1180 else
1181 pp_c_integer_constant (this, e);
1183 break;
1185 case REAL_CST:
1186 pp_c_floating_constant (this, e);
1187 break;
1189 case FIXED_CST:
1190 pp_c_fixed_constant (this, e);
1191 break;
1193 case STRING_CST:
1194 pp_c_string_literal (this, e);
1195 break;
1197 case COMPLEX_CST:
1198 /* Sometimes, we are confused and we think a complex literal
1199 is a constant. Such thing is a compound literal which
1200 grammatically belongs to postfix-expr production. */
1201 pp_c_compound_literal (this, e);
1202 break;
1204 default:
1205 pp_unsupported_tree (this, e);
1206 break;
1210 /* Pretty-print a string such as an identifier, without changing its
1211 encoding, preceded by whitespace is necessary. */
1213 void
1214 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1216 pp_c_maybe_whitespace (pp);
1217 pp_string (pp, str);
1218 pp->padding = pp_before;
1221 void
1222 c_pretty_printer::translate_string (const char *gmsgid)
1224 if (pp_translate_identifiers (this))
1225 pp_c_ws_string (this, _(gmsgid));
1226 else
1227 pp_c_ws_string (this, gmsgid);
1230 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1231 that need converting to the locale encoding, preceded by whitespace
1232 is necessary. */
1234 void
1235 pp_c_identifier (c_pretty_printer *pp, const char *id)
1237 pp_c_maybe_whitespace (pp);
1238 pp_identifier (pp, id);
1239 pp->padding = pp_before;
1242 /* Pretty-print a C primary-expression.
1243 primary-expression:
1244 identifier
1245 constant
1246 string-literal
1247 ( expression ) */
1249 void
1250 c_pretty_printer::primary_expression (tree e)
1252 switch (TREE_CODE (e))
1254 case VAR_DECL:
1255 case PARM_DECL:
1256 case FIELD_DECL:
1257 case CONST_DECL:
1258 case FUNCTION_DECL:
1259 case LABEL_DECL:
1260 pp_c_tree_decl_identifier (this, e);
1261 break;
1263 case IDENTIFIER_NODE:
1264 pp_c_tree_identifier (this, e);
1265 break;
1267 case ERROR_MARK:
1268 translate_string ("<erroneous-expression>");
1269 break;
1271 case RESULT_DECL:
1272 translate_string ("<return-value>");
1273 break;
1275 case INTEGER_CST:
1276 case REAL_CST:
1277 case FIXED_CST:
1278 case STRING_CST:
1279 constant (e);
1280 break;
1282 case TARGET_EXPR:
1283 pp_c_ws_string (this, "__builtin_memcpy");
1284 pp_c_left_paren (this);
1285 pp_ampersand (this);
1286 primary_expression (TREE_OPERAND (e, 0));
1287 pp_separate_with (this, ',');
1288 pp_ampersand (this);
1289 initializer (TREE_OPERAND (e, 1));
1290 if (TREE_OPERAND (e, 2))
1292 pp_separate_with (this, ',');
1293 expression (TREE_OPERAND (e, 2));
1295 pp_c_right_paren (this);
1296 break;
1298 default:
1299 /* FIXME: Make sure we won't get into an infinite loop. */
1300 pp_c_left_paren (this);
1301 expression (e);
1302 pp_c_right_paren (this);
1303 break;
1307 /* Print out a C initializer -- also support C compound-literals.
1308 initializer:
1309 assignment-expression:
1310 { initializer-list }
1311 { initializer-list , } */
1313 void
1314 c_pretty_printer::initializer (tree e)
1316 if (TREE_CODE (e) == CONSTRUCTOR)
1317 pp_c_brace_enclosed_initializer_list (this, e);
1318 else
1319 expression (e);
1322 /* init-declarator:
1323 declarator:
1324 declarator = initializer */
1326 void
1327 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1329 pp->declarator (t);
1330 /* We don't want to output function definitions here. There are handled
1331 elsewhere (and the syntactic form is bogus anyway). */
1332 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1334 tree init = DECL_INITIAL (t);
1335 /* This C++ bit is handled here because it is easier to do so.
1336 In templates, the C++ parser builds a TREE_LIST for a
1337 direct-initialization; the TREE_PURPOSE is the variable to
1338 initialize and the TREE_VALUE is the initializer. */
1339 if (TREE_CODE (init) == TREE_LIST)
1341 pp_c_left_paren (pp);
1342 pp->expression (TREE_VALUE (init));
1343 pp_right_paren (pp);
1345 else
1347 pp_space (pp);
1348 pp_equal (pp);
1349 pp_space (pp);
1350 pp->initializer (init);
1355 /* initializer-list:
1356 designation(opt) initializer
1357 initializer-list , designation(opt) initializer
1359 designation:
1360 designator-list =
1362 designator-list:
1363 designator
1364 designator-list designator
1366 designator:
1367 [ constant-expression ]
1368 identifier */
1370 static void
1371 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1373 tree type = TREE_TYPE (e);
1374 const enum tree_code code = TREE_CODE (type);
1376 if (TREE_CODE (e) == CONSTRUCTOR)
1378 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1379 return;
1382 switch (code)
1384 case RECORD_TYPE:
1385 case UNION_TYPE:
1386 case ARRAY_TYPE:
1388 tree init = TREE_OPERAND (e, 0);
1389 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1391 if (code == RECORD_TYPE || code == UNION_TYPE)
1393 pp_c_dot (pp);
1394 pp->primary_expression (TREE_PURPOSE (init));
1396 else
1398 pp_c_left_bracket (pp);
1399 if (TREE_PURPOSE (init))
1400 pp->constant (TREE_PURPOSE (init));
1401 pp_c_right_bracket (pp);
1403 pp_c_whitespace (pp);
1404 pp_equal (pp);
1405 pp_c_whitespace (pp);
1406 pp->initializer (TREE_VALUE (init));
1407 if (TREE_CHAIN (init))
1408 pp_separate_with (pp, ',');
1411 return;
1413 case VECTOR_TYPE:
1414 if (TREE_CODE (e) == VECTOR_CST)
1416 unsigned i;
1417 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1419 if (i > 0)
1420 pp_separate_with (pp, ',');
1421 pp->expression (VECTOR_CST_ELT (e, i));
1424 else
1425 break;
1426 return;
1428 case COMPLEX_TYPE:
1429 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1431 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1432 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1433 pp_separate_with (pp, ',');
1434 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1436 else
1437 break;
1438 return;
1440 default:
1441 break;
1444 pp_unsupported_tree (pp, type);
1447 /* Pretty-print a brace-enclosed initializer-list. */
1449 static void
1450 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1452 pp_c_left_brace (pp);
1453 pp_c_initializer_list (pp, l);
1454 pp_c_right_brace (pp);
1458 /* This is a convenient function, used to bridge gap between C and C++
1459 grammars.
1461 id-expression:
1462 identifier */
1464 void
1465 c_pretty_printer::id_expression (tree t)
1467 switch (TREE_CODE (t))
1469 case VAR_DECL:
1470 case PARM_DECL:
1471 case CONST_DECL:
1472 case TYPE_DECL:
1473 case FUNCTION_DECL:
1474 case FIELD_DECL:
1475 case LABEL_DECL:
1476 pp_c_tree_decl_identifier (this, t);
1477 break;
1479 case IDENTIFIER_NODE:
1480 pp_c_tree_identifier (this, t);
1481 break;
1483 default:
1484 pp_unsupported_tree (this, t);
1485 break;
1489 /* postfix-expression:
1490 primary-expression
1491 postfix-expression [ expression ]
1492 postfix-expression ( argument-expression-list(opt) )
1493 postfix-expression . identifier
1494 postfix-expression -> identifier
1495 postfix-expression ++
1496 postfix-expression --
1497 ( type-name ) { initializer-list }
1498 ( type-name ) { initializer-list , } */
1500 void
1501 c_pretty_printer::postfix_expression (tree e)
1503 enum tree_code code = TREE_CODE (e);
1504 switch (code)
1506 case POSTINCREMENT_EXPR:
1507 case POSTDECREMENT_EXPR:
1508 postfix_expression (TREE_OPERAND (e, 0));
1509 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1510 break;
1512 case ARRAY_REF:
1513 postfix_expression (TREE_OPERAND (e, 0));
1514 pp_c_left_bracket (this);
1515 expression (TREE_OPERAND (e, 1));
1516 pp_c_right_bracket (this);
1517 break;
1519 case ARRAY_NOTATION_REF:
1520 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1521 pp_c_left_bracket (this);
1522 expression (ARRAY_NOTATION_START (e));
1523 pp_colon (this);
1524 expression (ARRAY_NOTATION_LENGTH (e));
1525 pp_colon (this);
1526 expression (ARRAY_NOTATION_STRIDE (e));
1527 pp_c_right_bracket (this);
1528 break;
1530 case CALL_EXPR:
1532 call_expr_arg_iterator iter;
1533 tree arg;
1534 postfix_expression (CALL_EXPR_FN (e));
1535 pp_c_left_paren (this);
1536 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1538 expression (arg);
1539 if (more_call_expr_args_p (&iter))
1540 pp_separate_with (this, ',');
1542 pp_c_right_paren (this);
1543 break;
1546 case UNORDERED_EXPR:
1547 pp_c_ws_string (this, flag_isoc99
1548 ? "isunordered"
1549 : "__builtin_isunordered");
1550 goto two_args_fun;
1552 case ORDERED_EXPR:
1553 pp_c_ws_string (this, flag_isoc99
1554 ? "!isunordered"
1555 : "!__builtin_isunordered");
1556 goto two_args_fun;
1558 case UNLT_EXPR:
1559 pp_c_ws_string (this, flag_isoc99
1560 ? "!isgreaterequal"
1561 : "!__builtin_isgreaterequal");
1562 goto two_args_fun;
1564 case UNLE_EXPR:
1565 pp_c_ws_string (this, flag_isoc99
1566 ? "!isgreater"
1567 : "!__builtin_isgreater");
1568 goto two_args_fun;
1570 case UNGT_EXPR:
1571 pp_c_ws_string (this, flag_isoc99
1572 ? "!islessequal"
1573 : "!__builtin_islessequal");
1574 goto two_args_fun;
1576 case UNGE_EXPR:
1577 pp_c_ws_string (this, flag_isoc99
1578 ? "!isless"
1579 : "!__builtin_isless");
1580 goto two_args_fun;
1582 case UNEQ_EXPR:
1583 pp_c_ws_string (this, flag_isoc99
1584 ? "!islessgreater"
1585 : "!__builtin_islessgreater");
1586 goto two_args_fun;
1588 case LTGT_EXPR:
1589 pp_c_ws_string (this, flag_isoc99
1590 ? "islessgreater"
1591 : "__builtin_islessgreater");
1592 goto two_args_fun;
1594 two_args_fun:
1595 pp_c_left_paren (this);
1596 expression (TREE_OPERAND (e, 0));
1597 pp_separate_with (this, ',');
1598 expression (TREE_OPERAND (e, 1));
1599 pp_c_right_paren (this);
1600 break;
1602 case ABS_EXPR:
1603 pp_c_ws_string (this, "__builtin_abs");
1604 pp_c_left_paren (this);
1605 expression (TREE_OPERAND (e, 0));
1606 pp_c_right_paren (this);
1607 break;
1609 case COMPONENT_REF:
1611 tree object = TREE_OPERAND (e, 0);
1612 if (TREE_CODE (object) == INDIRECT_REF)
1614 postfix_expression (TREE_OPERAND (object, 0));
1615 pp_c_arrow (this);
1617 else
1619 postfix_expression (object);
1620 pp_c_dot (this);
1622 expression (TREE_OPERAND (e, 1));
1624 break;
1626 case BIT_FIELD_REF:
1628 tree type = TREE_TYPE (e);
1630 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1631 if (type
1632 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1634 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1635 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1636 if ((bitpos % size) == 0)
1638 pp_c_left_paren (this);
1639 pp_c_left_paren (this);
1640 type_id (type);
1641 pp_c_star (this);
1642 pp_c_right_paren (this);
1643 pp_c_ampersand (this);
1644 expression (TREE_OPERAND (e, 0));
1645 pp_c_right_paren (this);
1646 pp_c_left_bracket (this);
1647 pp_wide_integer (this, bitpos / size);
1648 pp_c_right_bracket (this);
1649 break;
1652 pp_unsupported_tree (this, e);
1654 break;
1656 case MEM_REF:
1657 expression (e);
1658 break;
1660 case COMPLEX_CST:
1661 case VECTOR_CST:
1662 pp_c_compound_literal (this, e);
1663 break;
1665 case COMPLEX_EXPR:
1666 pp_c_complex_expr (this, e);
1667 break;
1669 case COMPOUND_LITERAL_EXPR:
1670 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1671 /* Fall through. */
1672 case CONSTRUCTOR:
1673 initializer (e);
1674 break;
1676 case VA_ARG_EXPR:
1677 pp_c_ws_string (this, "__builtin_va_arg");
1678 pp_c_left_paren (this);
1679 assignment_expression (TREE_OPERAND (e, 0));
1680 pp_separate_with (this, ',');
1681 type_id (TREE_TYPE (e));
1682 pp_c_right_paren (this);
1683 break;
1685 case ADDR_EXPR:
1686 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1688 id_expression (TREE_OPERAND (e, 0));
1689 break;
1691 /* else fall through. */
1693 default:
1694 primary_expression (e);
1695 break;
1699 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1701 void
1702 pp_c_expression_list (c_pretty_printer *pp, tree e)
1704 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1706 pp->expression (TREE_VALUE (e));
1707 if (TREE_CHAIN (e))
1708 pp_separate_with (pp, ',');
1712 /* Print out V, which contains the elements of a constructor. */
1714 void
1715 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1717 unsigned HOST_WIDE_INT ix;
1718 tree value;
1720 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1722 pp->expression (value);
1723 if (ix != vec_safe_length (v) - 1)
1724 pp_separate_with (pp, ',');
1728 /* Print out an expression-list in parens, as if it were the argument
1729 list to a function. */
1731 void
1732 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1734 pp_c_left_paren (pp);
1735 if (t && TREE_CODE (t) == TREE_LIST)
1736 pp_c_expression_list (pp, t);
1737 pp_c_right_paren (pp);
1740 /* unary-expression:
1741 postfix-expression
1742 ++ cast-expression
1743 -- cast-expression
1744 unary-operator cast-expression
1745 sizeof unary-expression
1746 sizeof ( type-id )
1748 unary-operator: one of
1749 * & + - ! ~
1751 GNU extensions.
1752 unary-expression:
1753 __alignof__ unary-expression
1754 __alignof__ ( type-id )
1755 __real__ unary-expression
1756 __imag__ unary-expression */
1758 void
1759 c_pretty_printer::unary_expression (tree e)
1761 enum tree_code code = TREE_CODE (e);
1762 switch (code)
1764 case PREINCREMENT_EXPR:
1765 case PREDECREMENT_EXPR:
1766 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1767 unary_expression (TREE_OPERAND (e, 0));
1768 break;
1770 case ADDR_EXPR:
1771 case INDIRECT_REF:
1772 case NEGATE_EXPR:
1773 case BIT_NOT_EXPR:
1774 case TRUTH_NOT_EXPR:
1775 case CONJ_EXPR:
1776 /* String literal are used by address. */
1777 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1778 pp_ampersand (this);
1779 else if (code == INDIRECT_REF)
1780 pp_c_star (this);
1781 else if (code == NEGATE_EXPR)
1782 pp_minus (this);
1783 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1784 pp_complement (this);
1785 else if (code == TRUTH_NOT_EXPR)
1786 pp_exclamation (this);
1787 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1788 break;
1790 case MEM_REF:
1791 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1792 && integer_zerop (TREE_OPERAND (e, 1)))
1793 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1794 else
1796 pp_c_star (this);
1797 if (!integer_zerop (TREE_OPERAND (e, 1)))
1799 pp_c_left_paren (this);
1800 if (!integer_onep (TYPE_SIZE_UNIT
1801 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1802 pp_c_type_cast (this, ptr_type_node);
1804 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1805 if (!integer_zerop (TREE_OPERAND (e, 1)))
1807 pp_plus (this);
1808 pp_c_integer_constant (this,
1809 fold_convert (ssizetype,
1810 TREE_OPERAND (e, 1)));
1811 pp_c_right_paren (this);
1814 break;
1816 case REALPART_EXPR:
1817 case IMAGPART_EXPR:
1818 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1819 pp_c_whitespace (this);
1820 unary_expression (TREE_OPERAND (e, 0));
1821 break;
1823 default:
1824 postfix_expression (e);
1825 break;
1829 /* cast-expression:
1830 unary-expression
1831 ( type-name ) cast-expression */
1833 void
1834 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1836 switch (TREE_CODE (e))
1838 case FLOAT_EXPR:
1839 case FIX_TRUNC_EXPR:
1840 CASE_CONVERT:
1841 case VIEW_CONVERT_EXPR:
1842 pp_c_type_cast (pp, TREE_TYPE (e));
1843 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1844 break;
1846 default:
1847 pp->unary_expression (e);
1851 /* multiplicative-expression:
1852 cast-expression
1853 multiplicative-expression * cast-expression
1854 multiplicative-expression / cast-expression
1855 multiplicative-expression % cast-expression */
1857 void
1858 c_pretty_printer::multiplicative_expression (tree e)
1860 enum tree_code code = TREE_CODE (e);
1861 switch (code)
1863 case MULT_EXPR:
1864 case TRUNC_DIV_EXPR:
1865 case TRUNC_MOD_EXPR:
1866 multiplicative_expression (TREE_OPERAND (e, 0));
1867 pp_c_whitespace (this);
1868 if (code == MULT_EXPR)
1869 pp_c_star (this);
1870 else if (code == TRUNC_DIV_EXPR)
1871 pp_slash (this);
1872 else
1873 pp_modulo (this);
1874 pp_c_whitespace (this);
1875 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1876 break;
1878 default:
1879 pp_c_cast_expression (this, e);
1880 break;
1884 /* additive-expression:
1885 multiplicative-expression
1886 additive-expression + multiplicative-expression
1887 additive-expression - multiplicative-expression */
1889 static void
1890 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1892 enum tree_code code = TREE_CODE (e);
1893 switch (code)
1895 case POINTER_PLUS_EXPR:
1896 case PLUS_EXPR:
1897 case MINUS_EXPR:
1898 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1899 pp_c_whitespace (pp);
1900 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1901 pp_plus (pp);
1902 else
1903 pp_minus (pp);
1904 pp_c_whitespace (pp);
1905 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1906 break;
1908 default:
1909 pp->multiplicative_expression (e);
1910 break;
1914 /* additive-expression:
1915 additive-expression
1916 shift-expression << additive-expression
1917 shift-expression >> additive-expression */
1919 static void
1920 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1922 enum tree_code code = TREE_CODE (e);
1923 switch (code)
1925 case LSHIFT_EXPR:
1926 case RSHIFT_EXPR:
1927 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1928 pp_c_whitespace (pp);
1929 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1930 pp_c_whitespace (pp);
1931 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1932 break;
1934 default:
1935 pp_c_additive_expression (pp, e);
1939 /* relational-expression:
1940 shift-expression
1941 relational-expression < shift-expression
1942 relational-expression > shift-expression
1943 relational-expression <= shift-expression
1944 relational-expression >= shift-expression */
1946 static void
1947 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1949 enum tree_code code = TREE_CODE (e);
1950 switch (code)
1952 case LT_EXPR:
1953 case GT_EXPR:
1954 case LE_EXPR:
1955 case GE_EXPR:
1956 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1957 pp_c_whitespace (pp);
1958 if (code == LT_EXPR)
1959 pp_less (pp);
1960 else if (code == GT_EXPR)
1961 pp_greater (pp);
1962 else if (code == LE_EXPR)
1963 pp_less_equal (pp);
1964 else if (code == GE_EXPR)
1965 pp_greater_equal (pp);
1966 pp_c_whitespace (pp);
1967 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1968 break;
1970 default:
1971 pp_c_shift_expression (pp, e);
1972 break;
1976 /* equality-expression:
1977 relational-expression
1978 equality-expression == relational-expression
1979 equality-equality != relational-expression */
1981 static void
1982 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1984 enum tree_code code = TREE_CODE (e);
1985 switch (code)
1987 case EQ_EXPR:
1988 case NE_EXPR:
1989 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1990 pp_c_whitespace (pp);
1991 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1992 pp_c_whitespace (pp);
1993 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1994 break;
1996 default:
1997 pp_c_relational_expression (pp, e);
1998 break;
2002 /* AND-expression:
2003 equality-expression
2004 AND-expression & equality-equality */
2006 static void
2007 pp_c_and_expression (c_pretty_printer *pp, tree e)
2009 if (TREE_CODE (e) == BIT_AND_EXPR)
2011 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2012 pp_c_whitespace (pp);
2013 pp_ampersand (pp);
2014 pp_c_whitespace (pp);
2015 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2017 else
2018 pp_c_equality_expression (pp, e);
2021 /* exclusive-OR-expression:
2022 AND-expression
2023 exclusive-OR-expression ^ AND-expression */
2025 static void
2026 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2028 if (TREE_CODE (e) == BIT_XOR_EXPR
2029 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2031 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2032 if (TREE_CODE (e) == BIT_XOR_EXPR)
2033 pp_c_maybe_whitespace (pp);
2034 else
2035 pp_c_whitespace (pp);
2036 pp_carret (pp);
2037 pp_c_whitespace (pp);
2038 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2040 else
2041 pp_c_and_expression (pp, e);
2044 /* inclusive-OR-expression:
2045 exclusive-OR-expression
2046 inclusive-OR-expression | exclusive-OR-expression */
2048 static void
2049 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2051 if (TREE_CODE (e) == BIT_IOR_EXPR)
2053 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2054 pp_c_whitespace (pp);
2055 pp_bar (pp);
2056 pp_c_whitespace (pp);
2057 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2059 else
2060 pp_c_exclusive_or_expression (pp, e);
2063 /* logical-AND-expression:
2064 inclusive-OR-expression
2065 logical-AND-expression && inclusive-OR-expression */
2067 static void
2068 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2070 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2071 || TREE_CODE (e) == TRUTH_AND_EXPR)
2073 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2074 pp_c_whitespace (pp);
2075 pp_ampersand_ampersand (pp);
2076 pp_c_whitespace (pp);
2077 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2079 else
2080 pp_c_inclusive_or_expression (pp, e);
2083 /* logical-OR-expression:
2084 logical-AND-expression
2085 logical-OR-expression || logical-AND-expression */
2087 void
2088 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2090 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2091 || TREE_CODE (e) == TRUTH_OR_EXPR)
2093 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2094 pp_c_whitespace (pp);
2095 pp_bar_bar (pp);
2096 pp_c_whitespace (pp);
2097 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2099 else
2100 pp_c_logical_and_expression (pp, e);
2103 /* conditional-expression:
2104 logical-OR-expression
2105 logical-OR-expression ? expression : conditional-expression */
2107 void
2108 c_pretty_printer::conditional_expression (tree e)
2110 if (TREE_CODE (e) == COND_EXPR)
2112 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2113 pp_c_whitespace (this);
2114 pp_question (this);
2115 pp_c_whitespace (this);
2116 expression (TREE_OPERAND (e, 1));
2117 pp_c_whitespace (this);
2118 pp_colon (this);
2119 pp_c_whitespace (this);
2120 conditional_expression (TREE_OPERAND (e, 2));
2122 else
2123 pp_c_logical_or_expression (this, e);
2127 /* assignment-expression:
2128 conditional-expression
2129 unary-expression assignment-operator assignment-expression
2131 assignment-expression: one of
2132 = *= /= %= += -= >>= <<= &= ^= |= */
2134 void
2135 c_pretty_printer::assignment_expression (tree e)
2137 if (TREE_CODE (e) == MODIFY_EXPR
2138 || TREE_CODE (e) == INIT_EXPR)
2140 unary_expression (TREE_OPERAND (e, 0));
2141 pp_c_whitespace (this);
2142 pp_equal (this);
2143 pp_space (this);
2144 expression (TREE_OPERAND (e, 1));
2146 else
2147 conditional_expression (e);
2150 /* expression:
2151 assignment-expression
2152 expression , assignment-expression
2154 Implementation note: instead of going through the usual recursion
2155 chain, I take the liberty of dispatching nodes to the appropriate
2156 functions. This makes some redundancy, but it worths it. That also
2157 prevents a possible infinite recursion between primary_expression ()
2158 and expression (). */
2160 void
2161 c_pretty_printer::expression (tree e)
2163 switch (TREE_CODE (e))
2165 case INTEGER_CST:
2166 pp_c_integer_constant (this, e);
2167 break;
2169 case REAL_CST:
2170 pp_c_floating_constant (this, e);
2171 break;
2173 case FIXED_CST:
2174 pp_c_fixed_constant (this, e);
2175 break;
2177 case STRING_CST:
2178 pp_c_string_literal (this, e);
2179 break;
2181 case IDENTIFIER_NODE:
2182 case FUNCTION_DECL:
2183 case VAR_DECL:
2184 case CONST_DECL:
2185 case PARM_DECL:
2186 case RESULT_DECL:
2187 case FIELD_DECL:
2188 case LABEL_DECL:
2189 case ERROR_MARK:
2190 primary_expression (e);
2191 break;
2193 case SSA_NAME:
2194 if (SSA_NAME_VAR (e)
2195 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2196 expression (SSA_NAME_VAR (e));
2197 else
2198 translate_string ("<unknown>");
2199 break;
2201 case POSTINCREMENT_EXPR:
2202 case POSTDECREMENT_EXPR:
2203 case ARRAY_REF:
2204 case ARRAY_NOTATION_REF:
2205 case CALL_EXPR:
2206 case COMPONENT_REF:
2207 case BIT_FIELD_REF:
2208 case COMPLEX_CST:
2209 case COMPLEX_EXPR:
2210 case VECTOR_CST:
2211 case ORDERED_EXPR:
2212 case UNORDERED_EXPR:
2213 case LTGT_EXPR:
2214 case UNEQ_EXPR:
2215 case UNLE_EXPR:
2216 case UNLT_EXPR:
2217 case UNGE_EXPR:
2218 case UNGT_EXPR:
2219 case ABS_EXPR:
2220 case CONSTRUCTOR:
2221 case COMPOUND_LITERAL_EXPR:
2222 case VA_ARG_EXPR:
2223 postfix_expression (e);
2224 break;
2226 case CONJ_EXPR:
2227 case ADDR_EXPR:
2228 case INDIRECT_REF:
2229 case MEM_REF:
2230 case NEGATE_EXPR:
2231 case BIT_NOT_EXPR:
2232 case TRUTH_NOT_EXPR:
2233 case PREINCREMENT_EXPR:
2234 case PREDECREMENT_EXPR:
2235 case REALPART_EXPR:
2236 case IMAGPART_EXPR:
2237 unary_expression (e);
2238 break;
2240 case FLOAT_EXPR:
2241 case FIX_TRUNC_EXPR:
2242 CASE_CONVERT:
2243 case VIEW_CONVERT_EXPR:
2244 pp_c_cast_expression (this, e);
2245 break;
2247 case MULT_EXPR:
2248 case TRUNC_MOD_EXPR:
2249 case TRUNC_DIV_EXPR:
2250 multiplicative_expression (e);
2251 break;
2253 case LSHIFT_EXPR:
2254 case RSHIFT_EXPR:
2255 pp_c_shift_expression (this, e);
2256 break;
2258 case LT_EXPR:
2259 case GT_EXPR:
2260 case LE_EXPR:
2261 case GE_EXPR:
2262 pp_c_relational_expression (this, e);
2263 break;
2265 case BIT_AND_EXPR:
2266 pp_c_and_expression (this, e);
2267 break;
2269 case BIT_XOR_EXPR:
2270 case TRUTH_XOR_EXPR:
2271 pp_c_exclusive_or_expression (this, e);
2272 break;
2274 case BIT_IOR_EXPR:
2275 pp_c_inclusive_or_expression (this, e);
2276 break;
2278 case TRUTH_ANDIF_EXPR:
2279 case TRUTH_AND_EXPR:
2280 pp_c_logical_and_expression (this, e);
2281 break;
2283 case TRUTH_ORIF_EXPR:
2284 case TRUTH_OR_EXPR:
2285 pp_c_logical_or_expression (this, e);
2286 break;
2288 case EQ_EXPR:
2289 case NE_EXPR:
2290 pp_c_equality_expression (this, e);
2291 break;
2293 case COND_EXPR:
2294 conditional_expression (e);
2295 break;
2297 case POINTER_PLUS_EXPR:
2298 case PLUS_EXPR:
2299 case MINUS_EXPR:
2300 pp_c_additive_expression (this, e);
2301 break;
2303 case MODIFY_EXPR:
2304 case INIT_EXPR:
2305 assignment_expression (e);
2306 break;
2308 case COMPOUND_EXPR:
2309 pp_c_left_paren (this);
2310 expression (TREE_OPERAND (e, 0));
2311 pp_separate_with (this, ',');
2312 assignment_expression (TREE_OPERAND (e, 1));
2313 pp_c_right_paren (this);
2314 break;
2316 case NON_LVALUE_EXPR:
2317 case SAVE_EXPR:
2318 expression (TREE_OPERAND (e, 0));
2319 break;
2321 case TARGET_EXPR:
2322 postfix_expression (TREE_OPERAND (e, 1));
2323 break;
2325 case BIND_EXPR:
2326 case GOTO_EXPR:
2327 /* We don't yet have a way of dumping statements in a
2328 human-readable format. */
2329 pp_string (this, "({...})");
2330 break;
2332 case C_MAYBE_CONST_EXPR:
2333 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2334 break;
2336 default:
2337 pp_unsupported_tree (this, e);
2338 break;
2344 /* Statements. */
2346 void
2347 c_pretty_printer::statement (tree stmt)
2349 if (stmt == NULL)
2350 return;
2352 if (pp_needs_newline (this))
2353 pp_newline_and_indent (this, 0);
2355 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2359 /* Initialize the PRETTY-PRINTER for handling C codes. */
2361 c_pretty_printer::c_pretty_printer ()
2362 : pretty_printer (),
2363 offset_list (),
2364 flags ()
2366 type_specifier_seq = pp_c_specifier_qualifier_list;
2367 ptr_operator = pp_c_pointer;
2368 parameter_list = pp_c_parameter_type_list;
2372 /* Print the tree T in full, on file FILE. */
2374 void
2375 print_c_tree (FILE *file, tree t)
2377 c_pretty_printer pp;
2379 pp_needs_newline (&pp) = true;
2380 pp.buffer->stream = file;
2381 pp.statement (t);
2382 pp_newline_and_flush (&pp);
2385 /* Print the tree T in full, on stderr. */
2387 DEBUG_FUNCTION void
2388 debug_c_tree (tree t)
2390 print_c_tree (stderr, t);
2391 fputc ('\n', stderr);
2394 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2395 up of T's memory address. */
2397 void
2398 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2400 const char *name;
2402 gcc_assert (DECL_P (t));
2404 if (DECL_NAME (t))
2405 name = IDENTIFIER_POINTER (DECL_NAME (t));
2406 else
2408 static char xname[8];
2409 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2410 name = xname;
2413 pp_c_identifier (pp, name);