1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2017 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
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
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/>. */
23 #include "coretypes.h"
24 #include "c-pretty-print.h"
25 #include "diagnostic.h"
26 #include "stor-layout.h"
27 #include "stringpool.h"
30 #include "tree-pretty-print.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) \
42 if ((PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
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
);
65 /* Helper functions. */
68 pp_c_whitespace (c_pretty_printer
*pp
)
71 pp
->padding
= pp_none
;
75 pp_c_left_paren (c_pretty_printer
*pp
)
78 pp
->padding
= pp_none
;
82 pp_c_right_paren (c_pretty_printer
*pp
)
85 pp
->padding
= pp_none
;
89 pp_c_left_brace (c_pretty_printer
*pp
)
92 pp
->padding
= pp_none
;
96 pp_c_right_brace (c_pretty_printer
*pp
)
99 pp
->padding
= pp_none
;
103 pp_c_left_bracket (c_pretty_printer
*pp
)
105 pp_left_bracket (pp
);
106 pp
->padding
= pp_none
;
110 pp_c_right_bracket (c_pretty_printer
*pp
)
112 pp_right_bracket (pp
);
113 pp
->padding
= pp_none
;
117 pp_c_dot (c_pretty_printer
*pp
)
120 pp
->padding
= pp_none
;
124 pp_c_ampersand (c_pretty_printer
*pp
)
127 pp
->padding
= pp_none
;
131 pp_c_star (c_pretty_printer
*pp
)
134 pp
->padding
= pp_none
;
138 pp_c_arrow (c_pretty_printer
*pp
)
141 pp
->padding
= pp_none
;
145 pp_c_semicolon (c_pretty_printer
*pp
)
148 pp
->padding
= pp_none
;
152 pp_c_complement (c_pretty_printer
*pp
)
155 pp
->padding
= pp_none
;
159 pp_c_exclamation (c_pretty_printer
*pp
)
162 pp
->padding
= pp_none
;
165 /* Print out the external representation of QUALIFIERS. */
168 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
170 const char *p
= pp_last_position_in_text (pp
);
175 /* The C programming language does not have references, but it is much
176 simpler to handle those here rather than going through the same
177 logic in the C++ pretty-printer. */
178 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
179 pp_c_whitespace (pp
);
181 if (qualifiers
& TYPE_QUAL_ATOMIC
)
182 pp_c_ws_string (pp
, "_Atomic");
183 if (qualifiers
& TYPE_QUAL_CONST
)
184 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
185 if (qualifiers
& TYPE_QUAL_VOLATILE
)
186 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
187 if (qualifiers
& TYPE_QUAL_RESTRICT
)
188 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
189 ? "restrict" : "__restrict__"));
192 /* Pretty-print T using the type-cast notation '( type-name )'. */
195 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
197 pp_c_left_paren (pp
);
199 pp_c_right_paren (pp
);
202 /* We're about to pretty-print a pointer type as indicated by T.
203 Output a whitespace, if needed, preparing for subsequent output. */
206 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
208 if (POINTER_TYPE_P (t
))
210 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
211 if (TREE_CODE (pointee
) != ARRAY_TYPE
212 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
213 pp_c_whitespace (pp
);
220 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
221 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
222 of its type. Take care of possible extensions.
226 type-qualifier-list type-qualifier
231 __restrict__ -- GNU C
232 address-space-qualifier -- GNU C
236 address-space-qualifier:
237 identifier -- GNU C */
240 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
244 if (!t
|| t
== error_mark_node
)
250 qualifiers
= TYPE_QUALS (t
);
251 pp_c_cv_qualifiers (pp
, qualifiers
,
252 TREE_CODE (t
) == FUNCTION_TYPE
);
254 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
256 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
257 pp_c_identifier (pp
, as
);
262 * type-qualifier-list(opt)
263 * type-qualifier-list(opt) pointer */
266 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
268 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
270 switch (TREE_CODE (t
))
273 /* It is easier to handle C++ reference types here. */
275 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
276 pp_c_pointer (pp
, TREE_TYPE (t
));
277 if (TREE_CODE (t
) == POINTER_TYPE
)
281 pp_c_type_qualifier_list (pp
, t
);
284 /* ??? This node is now in GENERIC and so shouldn't be here. But
285 we'll fix that later. */
287 pp
->declaration (DECL_EXPR_DECL (t
));
288 pp_needs_newline (pp
) = true;
292 pp_unsupported_tree (pp
, t
);
296 /* simple-type-specifier:
312 struct-or-union-specifier
317 simple-type-specifier:
322 c_pretty_printer::simple_type_specifier (tree t
)
324 const enum tree_code code
= TREE_CODE (t
);
328 translate_string ("<type-error>");
331 case IDENTIFIER_NODE
:
332 pp_c_identifier (this, IDENTIFIER_POINTER (t
));
339 case FIXED_POINT_TYPE
:
343 simple_type_specifier (t
);
347 int prec
= TYPE_PRECISION (t
);
349 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
350 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
351 TYPE_SATURATING (t
));
353 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
355 if (common_t
&& TYPE_NAME (common_t
))
357 simple_type_specifier (common_t
);
358 if (TYPE_PRECISION (common_t
) != prec
)
361 pp_decimal_int (this, prec
);
369 translate_string (TYPE_UNSIGNED (t
)
370 ? "<unnamed-unsigned:"
371 : "<unnamed-signed:");
374 translate_string ("<unnamed-float:");
376 case FIXED_POINT_TYPE
:
377 translate_string ("<unnamed-fixed:");
382 pp_decimal_int (this, prec
);
392 translate_string ("<typedef-error>");
398 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
399 /* Don't decorate the type if this is a typedef name. */;
400 else if (code
== UNION_TYPE
)
401 pp_c_ws_string (this, "union");
402 else if (code
== RECORD_TYPE
)
403 pp_c_ws_string (this, "struct");
404 else if (code
== ENUMERAL_TYPE
)
405 pp_c_ws_string (this, "enum");
407 translate_string ("<tag-error>");
410 id_expression (TYPE_NAME (t
));
412 translate_string ("<anonymous>");
416 pp_unsupported_tree (this, t
);
421 /* specifier-qualifier-list:
422 type-specifier specifier-qualifier-list-opt
423 type-qualifier specifier-qualifier-list-opt
426 Implementation note: Because of the non-linearities in array or
427 function declarations, this routine prints not just the
428 specifier-qualifier-list of such entities or types of such entities,
429 but also the 'pointer' production part of their declarators. The
430 remaining part is done by declarator() or abstract_declarator(). */
433 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
435 const enum tree_code code
= TREE_CODE (t
);
437 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
438 pp_c_type_qualifier_list (pp
, t
);
444 /* Get the types-specifier of this type. */
445 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
446 pp_c_specifier_qualifier_list (pp
, pointee
);
447 if (TREE_CODE (pointee
) == ARRAY_TYPE
448 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
450 pp_c_whitespace (pp
);
451 pp_c_left_paren (pp
);
452 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
454 else if (!c_dialect_cxx ())
455 pp_c_whitespace (pp
);
456 pp_ptr_operator (pp
, t
);
462 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
467 if (code
== COMPLEX_TYPE
)
468 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
469 ? "_Complex" : "__complex__"));
470 else if (code
== VECTOR_TYPE
)
472 pp_c_ws_string (pp
, "__vector");
473 pp_c_left_paren (pp
);
474 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
475 pp_c_right_paren (pp
);
476 pp_c_whitespace (pp
);
478 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
482 pp
->simple_type_specifier (t
);
485 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
486 pp_c_type_qualifier_list (pp
, t
);
489 /* parameter-type-list:
494 parameter-declaration
495 parameter-list , parameter-declaration
497 parameter-declaration:
498 declaration-specifiers declarator
499 declaration-specifiers abstract-declarator(opt) */
502 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
504 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
505 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
506 pp_c_left_paren (pp
);
507 if (parms
== void_list_node
)
508 pp_c_ws_string (pp
, "void");
512 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
515 pp_separate_with (pp
, ',');
517 pp
->declaration_specifiers
518 (want_parm_decl
? parms
: TREE_VALUE (parms
));
520 pp
->declarator (parms
);
522 pp
->abstract_declarator (TREE_VALUE (parms
));
524 if (!first
&& !parms
)
526 pp_separate_with (pp
, ',');
527 pp_c_ws_string (pp
, "...");
530 pp_c_right_paren (pp
);
533 /* abstract-declarator:
535 pointer(opt) direct-abstract-declarator */
538 c_pretty_printer::abstract_declarator (tree t
)
540 if (TREE_CODE (t
) == POINTER_TYPE
)
542 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
543 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
544 pp_c_right_paren (this);
548 direct_abstract_declarator (t
);
551 /* direct-abstract-declarator:
552 ( abstract-declarator )
553 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
554 direct-abstract-declarator(opt) [ * ]
555 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
558 c_pretty_printer::direct_abstract_declarator (tree t
)
560 switch (TREE_CODE (t
))
563 abstract_declarator (t
);
567 pp_c_parameter_type_list (this, t
);
568 direct_abstract_declarator (TREE_TYPE (t
));
572 pp_c_left_bracket (this);
573 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
575 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
576 tree type
= TREE_TYPE (maxval
);
578 if (tree_fits_shwi_p (maxval
))
579 pp_wide_integer (this, tree_to_shwi (maxval
) + 1);
581 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
582 build_int_cst (type
, 1)));
584 pp_c_right_bracket (this);
585 direct_abstract_declarator (TREE_TYPE (t
));
588 case IDENTIFIER_NODE
:
593 case FIXED_POINT_TYPE
:
603 pp_unsupported_tree (this, t
);
609 specifier-qualifier-list abstract-declarator(opt) */
612 c_pretty_printer::type_id (tree t
)
614 pp_c_specifier_qualifier_list (this, t
);
615 abstract_declarator (t
);
618 /* storage-class-specifier:
626 c_pretty_printer::storage_class_specifier (tree t
)
628 if (TREE_CODE (t
) == TYPE_DECL
)
629 pp_c_ws_string (this, "typedef");
632 if (DECL_REGISTER (t
))
633 pp_c_ws_string (this, "register");
634 else if (TREE_STATIC (t
) && VAR_P (t
))
635 pp_c_ws_string (this, "static");
639 /* function-specifier:
643 c_pretty_printer::function_specifier (tree t
)
645 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
646 pp_c_ws_string (this, "inline");
649 /* declaration-specifiers:
650 storage-class-specifier declaration-specifiers(opt)
651 type-specifier declaration-specifiers(opt)
652 type-qualifier declaration-specifiers(opt)
653 function-specifier declaration-specifiers(opt) */
656 c_pretty_printer::declaration_specifiers (tree t
)
658 storage_class_specifier (t
);
659 function_specifier (t
);
660 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
666 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
667 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
668 direct-declarator [ type-qualifier-list static assignment-expression ]
669 direct-declarator [ type-qualifier-list * ]
670 direct-declarator ( parameter-type-list )
671 direct-declarator ( identifier-list(opt) ) */
674 c_pretty_printer::direct_declarator (tree t
)
676 switch (TREE_CODE (t
))
683 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
684 pp_c_tree_decl_identifier (this, t
);
689 abstract_declarator (TREE_TYPE (t
));
693 pp_parameter_list (this, t
);
694 abstract_declarator (TREE_TYPE (t
));
698 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
699 pp_c_tree_decl_identifier (this, t
);
700 if (flags
& pp_c_flag_abstract
)
701 abstract_declarator (TREE_TYPE (t
));
704 pp_parameter_list (this, t
);
705 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
711 case FIXED_POINT_TYPE
:
718 pp_unsupported_tree (this, t
);
725 pointer(opt) direct-declarator */
728 c_pretty_printer::declarator (tree t
)
730 switch (TREE_CODE (t
))
734 case FIXED_POINT_TYPE
:
747 direct_declarator (t
);
752 pp_unsupported_tree (this, t
);
758 declaration-specifiers init-declarator-list(opt) ; */
761 c_pretty_printer::declaration (tree t
)
763 declaration_specifiers (t
);
764 pp_c_init_declarator (this, t
);
767 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
770 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
772 if (attributes
== NULL_TREE
)
775 pp_c_ws_string (pp
, "__attribute__");
776 pp_c_left_paren (pp
);
777 pp_c_left_paren (pp
);
778 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
780 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
781 if (TREE_VALUE (attributes
))
782 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
784 if (TREE_CHAIN (attributes
))
785 pp_separate_with (pp
, ',');
787 pp_c_right_paren (pp
);
788 pp_c_right_paren (pp
);
791 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
792 marked to be displayed on disgnostic. */
795 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
797 bool is_first
= true;
802 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
804 const struct attribute_spec
*as
;
805 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
806 if (!as
|| as
->affects_type_identity
== false)
809 && !strcmp ("transaction_safe", as
->name
))
810 /* In C++ transaction_safe is printed at the end of the declarator. */
814 pp_c_ws_string (pp
, "__attribute__");
815 pp_c_left_paren (pp
);
816 pp_c_left_paren (pp
);
821 pp_separate_with (pp
, ',');
823 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
825 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
830 pp_c_right_paren (pp
);
831 pp_c_right_paren (pp
);
832 pp_c_whitespace (pp
);
836 /* function-definition:
837 declaration-specifiers declarator compound-statement */
840 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
842 pp
->declaration_specifiers (t
);
844 pp_needs_newline (pp
) = true;
845 pp
->statement (DECL_SAVED_TREE (t
));
846 pp_newline_and_flush (pp
);
852 /* Print out a c-char. This is called solely for characters which are
853 in the *target* execution character set. We ought to convert them
854 back to the *host* execution character set before printing, but we
855 have no way to do this at present. A decent compromise is to print
856 all characters as if they were in the host execution character set,
857 and not attempt to recover any named escape characters, but render
858 all unprintables as octal escapes. If the host and target character
859 sets are the same, this produces relatively readable output. If they
860 are not the same, strings may appear as gibberish, but that's okay
861 (in fact, it may well be what the reader wants, e.g. if they are looking
862 to see if conversion to the target character set happened correctly).
864 A special case: we need to prefix \, ", and ' with backslashes. It is
865 correct to do so for the *host*'s \, ", and ', because the rest of the
866 file appears in the host character set. */
869 pp_c_char (c_pretty_printer
*pp
, int c
)
875 case '\\': pp_string (pp
, "\\\\"); break;
876 case '\'': pp_string (pp
, "\\\'"); break;
877 case '\"': pp_string (pp
, "\\\""); break;
878 default: pp_character (pp
, c
);
882 pp_scalar (pp
, "\\%03o", (unsigned) c
);
885 /* Print out a STRING literal. */
888 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
890 const char *p
= TREE_STRING_POINTER (s
);
891 int n
= TREE_STRING_LENGTH (s
) - 1;
894 for (i
= 0; i
< n
; ++i
)
895 pp_c_char (pp
, p
[i
]);
899 /* Pretty-print a VOID_CST (void_node). */
902 pp_c_void_constant (c_pretty_printer
*pp
)
904 pp_c_type_cast (pp
, void_type_node
);
908 /* Pretty-print an INTEGER literal. */
911 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
913 if (tree_fits_shwi_p (i
))
914 pp_wide_integer (pp
, tree_to_shwi (i
));
915 else if (tree_fits_uhwi_p (i
))
916 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
919 wide_int wi
= wi::to_wide (i
);
921 if (wi::lt_p (wi::to_wide (i
), 0, TYPE_SIGN (TREE_TYPE (i
))))
926 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
927 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
931 /* Print out a CHARACTER literal. */
934 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
937 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
941 /* Print out a BOOLEAN literal. */
944 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
946 if (b
== boolean_false_node
)
948 if (c_dialect_cxx ())
949 pp_c_ws_string (pp
, "false");
950 else if (flag_isoc99
)
951 pp_c_ws_string (pp
, "_False");
953 pp_unsupported_tree (pp
, b
);
955 else if (b
== boolean_true_node
)
957 if (c_dialect_cxx ())
958 pp_c_ws_string (pp
, "true");
959 else if (flag_isoc99
)
960 pp_c_ws_string (pp
, "_True");
962 pp_unsupported_tree (pp
, b
);
964 else if (TREE_CODE (b
) == INTEGER_CST
)
965 pp_c_integer_constant (pp
, b
);
967 pp_unsupported_tree (pp
, b
);
970 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
971 false; that means the value was obtained by a cast, in which case
972 print out the type-id part of the cast-expression -- the casted value
973 is then printed by pp_c_integer_literal. */
976 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
978 bool value_is_named
= true;
979 tree type
= TREE_TYPE (e
);
982 /* Find the name of this constant. */
983 for (value
= TYPE_VALUES (type
);
984 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
985 value
= TREE_CHAIN (value
))
988 if (value
!= NULL_TREE
)
989 pp
->id_expression (TREE_PURPOSE (value
));
992 /* Value must have been cast. */
993 pp_c_type_cast (pp
, type
);
994 value_is_named
= false;
997 return value_is_named
;
1000 /* Print out a REAL value as a decimal-floating-constant. */
1003 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1005 const struct real_format
*fmt
1006 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1008 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1009 bool is_decimal
= floating_cst
.decimal
;
1011 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1012 log10(2) to 7 significant digits. */
1013 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1015 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1016 sizeof (pp_buffer (pp
)->digit_buffer
),
1019 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1020 if (TREE_TYPE (r
) == float_type_node
)
1021 pp_character (pp
, 'f');
1022 else if (TREE_TYPE (r
) == long_double_type_node
)
1023 pp_character (pp
, 'l');
1024 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1025 pp_string (pp
, "dl");
1026 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1027 pp_string (pp
, "dd");
1028 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1029 pp_string (pp
, "df");
1030 else if (TREE_TYPE (r
) != double_type_node
)
1031 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1032 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1034 pp_character (pp
, 'f');
1035 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1036 if (floatn_nx_types
[i
].extended
)
1037 pp_character (pp
, 'x');
1042 /* Print out a FIXED value as a decimal-floating-constant. */
1045 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1047 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1048 sizeof (pp_buffer (pp
)->digit_buffer
));
1049 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1052 /* Pretty-print a compound literal expression. GNU extensions include
1053 vector constants. */
1056 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1058 tree type
= TREE_TYPE (e
);
1059 pp_c_type_cast (pp
, type
);
1061 switch (TREE_CODE (type
))
1068 pp_c_brace_enclosed_initializer_list (pp
, e
);
1072 pp_unsupported_tree (pp
, e
);
1077 /* Pretty-print a COMPLEX_EXPR expression. */
1080 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1082 /* Handle a few common special cases, otherwise fallback
1083 to printing it as compound literal. */
1084 tree type
= TREE_TYPE (e
);
1085 tree realexpr
= TREE_OPERAND (e
, 0);
1086 tree imagexpr
= TREE_OPERAND (e
, 1);
1088 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1089 if (TREE_CODE (realexpr
) == NOP_EXPR
1090 && TREE_CODE (imagexpr
) == NOP_EXPR
1091 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1092 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1093 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1094 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1095 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1096 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1098 pp_c_type_cast (pp
, type
);
1099 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1103 /* Cast of an scalar expression to COMPLEX_TYPE. */
1104 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1105 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1107 pp_c_type_cast (pp
, type
);
1108 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1109 realexpr
= TREE_OPERAND (realexpr
, 0);
1110 pp
->expression (realexpr
);
1114 pp_c_compound_literal (pp
, e
);
1120 fixed-point-constant
1121 enumeration-constant
1122 character-constant */
1125 c_pretty_printer::constant (tree e
)
1127 const enum tree_code code
= TREE_CODE (e
);
1132 pp_c_void_constant (this);
1137 tree type
= TREE_TYPE (e
);
1138 if (type
== boolean_type_node
)
1139 pp_c_bool_constant (this, e
);
1140 else if (type
== char_type_node
)
1141 pp_c_character_constant (this, e
);
1142 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1143 && pp_c_enumeration_constant (this, e
))
1146 pp_c_integer_constant (this, e
);
1151 pp_c_floating_constant (this, e
);
1155 pp_c_fixed_constant (this, e
);
1159 pp_c_string_literal (this, e
);
1163 /* Sometimes, we are confused and we think a complex literal
1164 is a constant. Such thing is a compound literal which
1165 grammatically belongs to postfix-expr production. */
1166 pp_c_compound_literal (this, e
);
1170 pp_unsupported_tree (this, e
);
1175 /* Pretty-print a string such as an identifier, without changing its
1176 encoding, preceded by whitespace is necessary. */
1179 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1181 pp_c_maybe_whitespace (pp
);
1182 pp_string (pp
, str
);
1183 pp
->padding
= pp_before
;
1187 c_pretty_printer::translate_string (const char *gmsgid
)
1189 if (pp_translate_identifiers (this))
1190 pp_c_ws_string (this, _(gmsgid
));
1192 pp_c_ws_string (this, gmsgid
);
1195 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1196 that need converting to the locale encoding, preceded by whitespace
1200 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1202 pp_c_maybe_whitespace (pp
);
1203 pp_identifier (pp
, id
);
1204 pp
->padding
= pp_before
;
1207 /* Pretty-print a C primary-expression.
1215 c_pretty_printer::primary_expression (tree e
)
1217 switch (TREE_CODE (e
))
1225 pp_c_tree_decl_identifier (this, e
);
1228 case IDENTIFIER_NODE
:
1229 pp_c_tree_identifier (this, e
);
1233 translate_string ("<erroneous-expression>");
1237 translate_string ("<return-value>");
1249 pp_c_ws_string (this, "__builtin_memcpy");
1250 pp_c_left_paren (this);
1251 pp_ampersand (this);
1252 primary_expression (TREE_OPERAND (e
, 0));
1253 pp_separate_with (this, ',');
1254 pp_ampersand (this);
1255 initializer (TREE_OPERAND (e
, 1));
1256 if (TREE_OPERAND (e
, 2))
1258 pp_separate_with (this, ',');
1259 expression (TREE_OPERAND (e
, 2));
1261 pp_c_right_paren (this);
1265 /* FIXME: Make sure we won't get into an infinite loop. */
1266 pp_c_left_paren (this);
1268 pp_c_right_paren (this);
1273 /* Print out a C initializer -- also support C compound-literals.
1275 assignment-expression:
1276 { initializer-list }
1277 { initializer-list , } */
1280 c_pretty_printer::initializer (tree e
)
1282 if (TREE_CODE (e
) == CONSTRUCTOR
)
1283 pp_c_brace_enclosed_initializer_list (this, e
);
1290 declarator = initializer */
1293 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1296 /* We don't want to output function definitions here. There are handled
1297 elsewhere (and the syntactic form is bogus anyway). */
1298 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1300 tree init
= DECL_INITIAL (t
);
1301 /* This C++ bit is handled here because it is easier to do so.
1302 In templates, the C++ parser builds a TREE_LIST for a
1303 direct-initialization; the TREE_PURPOSE is the variable to
1304 initialize and the TREE_VALUE is the initializer. */
1305 if (TREE_CODE (init
) == TREE_LIST
)
1307 pp_c_left_paren (pp
);
1308 pp
->expression (TREE_VALUE (init
));
1309 pp_right_paren (pp
);
1316 pp
->initializer (init
);
1321 /* initializer-list:
1322 designation(opt) initializer
1323 initializer-list , designation(opt) initializer
1330 designator-list designator
1333 [ constant-expression ]
1337 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1339 tree type
= TREE_TYPE (e
);
1340 const enum tree_code code
= TREE_CODE (type
);
1342 if (TREE_CODE (e
) == CONSTRUCTOR
)
1344 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1354 tree init
= TREE_OPERAND (e
, 0);
1355 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1357 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1360 pp
->primary_expression (TREE_PURPOSE (init
));
1364 pp_c_left_bracket (pp
);
1365 if (TREE_PURPOSE (init
))
1366 pp
->constant (TREE_PURPOSE (init
));
1367 pp_c_right_bracket (pp
);
1369 pp_c_whitespace (pp
);
1371 pp_c_whitespace (pp
);
1372 pp
->initializer (TREE_VALUE (init
));
1373 if (TREE_CHAIN (init
))
1374 pp_separate_with (pp
, ',');
1380 if (TREE_CODE (e
) == VECTOR_CST
)
1383 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1386 pp_separate_with (pp
, ',');
1387 pp
->expression (VECTOR_CST_ELT (e
, i
));
1395 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1397 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1398 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1399 pp_separate_with (pp
, ',');
1400 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1410 pp_unsupported_tree (pp
, type
);
1413 /* Pretty-print a brace-enclosed initializer-list. */
1416 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1418 pp_c_left_brace (pp
);
1419 pp_c_initializer_list (pp
, l
);
1420 pp_c_right_brace (pp
);
1424 /* This is a convenient function, used to bridge gap between C and C++
1431 c_pretty_printer::id_expression (tree t
)
1433 switch (TREE_CODE (t
))
1442 pp_c_tree_decl_identifier (this, t
);
1445 case IDENTIFIER_NODE
:
1446 pp_c_tree_identifier (this, t
);
1450 pp_unsupported_tree (this, t
);
1455 /* postfix-expression:
1457 postfix-expression [ expression ]
1458 postfix-expression ( argument-expression-list(opt) )
1459 postfix-expression . identifier
1460 postfix-expression -> identifier
1461 postfix-expression ++
1462 postfix-expression --
1463 ( type-name ) { initializer-list }
1464 ( type-name ) { initializer-list , } */
1467 c_pretty_printer::postfix_expression (tree e
)
1469 enum tree_code code
= TREE_CODE (e
);
1472 case POSTINCREMENT_EXPR
:
1473 case POSTDECREMENT_EXPR
:
1474 postfix_expression (TREE_OPERAND (e
, 0));
1475 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1479 postfix_expression (TREE_OPERAND (e
, 0));
1480 pp_c_left_bracket (this);
1481 expression (TREE_OPERAND (e
, 1));
1482 pp_c_right_bracket (this);
1485 case ARRAY_NOTATION_REF
:
1486 postfix_expression (ARRAY_NOTATION_ARRAY (e
));
1487 pp_c_left_bracket (this);
1488 expression (ARRAY_NOTATION_START (e
));
1490 expression (ARRAY_NOTATION_LENGTH (e
));
1492 expression (ARRAY_NOTATION_STRIDE (e
));
1493 pp_c_right_bracket (this);
1498 call_expr_arg_iterator iter
;
1500 postfix_expression (CALL_EXPR_FN (e
));
1501 pp_c_left_paren (this);
1502 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1505 if (more_call_expr_args_p (&iter
))
1506 pp_separate_with (this, ',');
1508 pp_c_right_paren (this);
1512 case UNORDERED_EXPR
:
1513 pp_c_ws_string (this, flag_isoc99
1515 : "__builtin_isunordered");
1519 pp_c_ws_string (this, flag_isoc99
1521 : "!__builtin_isunordered");
1525 pp_c_ws_string (this, flag_isoc99
1527 : "!__builtin_isgreaterequal");
1531 pp_c_ws_string (this, flag_isoc99
1533 : "!__builtin_isgreater");
1537 pp_c_ws_string (this, flag_isoc99
1539 : "!__builtin_islessequal");
1543 pp_c_ws_string (this, flag_isoc99
1545 : "!__builtin_isless");
1549 pp_c_ws_string (this, flag_isoc99
1551 : "!__builtin_islessgreater");
1555 pp_c_ws_string (this, flag_isoc99
1557 : "__builtin_islessgreater");
1561 pp_c_ws_string (this, "max");
1565 pp_c_ws_string (this, "min");
1569 pp_c_left_paren (this);
1570 expression (TREE_OPERAND (e
, 0));
1571 pp_separate_with (this, ',');
1572 expression (TREE_OPERAND (e
, 1));
1573 pp_c_right_paren (this);
1577 pp_c_ws_string (this, "__builtin_abs");
1578 pp_c_left_paren (this);
1579 expression (TREE_OPERAND (e
, 0));
1580 pp_c_right_paren (this);
1585 tree object
= TREE_OPERAND (e
, 0);
1586 if (INDIRECT_REF_P (object
))
1588 postfix_expression (TREE_OPERAND (object
, 0));
1593 postfix_expression (object
);
1596 expression (TREE_OPERAND (e
, 1));
1602 tree type
= TREE_TYPE (e
);
1604 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1606 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1608 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1609 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1610 if ((bitpos
% size
) == 0)
1612 pp_c_left_paren (this);
1613 pp_c_left_paren (this);
1616 pp_c_right_paren (this);
1617 pp_c_ampersand (this);
1618 expression (TREE_OPERAND (e
, 0));
1619 pp_c_right_paren (this);
1620 pp_c_left_bracket (this);
1621 pp_wide_integer (this, bitpos
/ size
);
1622 pp_c_right_bracket (this);
1626 pp_unsupported_tree (this, e
);
1636 pp_c_compound_literal (this, e
);
1640 pp_c_complex_expr (this, e
);
1643 case COMPOUND_LITERAL_EXPR
:
1644 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1651 pp_c_ws_string (this, "__builtin_va_arg");
1652 pp_c_left_paren (this);
1653 assignment_expression (TREE_OPERAND (e
, 0));
1654 pp_separate_with (this, ',');
1655 type_id (TREE_TYPE (e
));
1656 pp_c_right_paren (this);
1660 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1662 id_expression (TREE_OPERAND (e
, 0));
1668 primary_expression (e
);
1673 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1676 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1678 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1680 pp
->expression (TREE_VALUE (e
));
1682 pp_separate_with (pp
, ',');
1686 /* Print out V, which contains the elements of a constructor. */
1689 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1691 unsigned HOST_WIDE_INT ix
;
1694 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1696 pp
->expression (value
);
1697 if (ix
!= vec_safe_length (v
) - 1)
1698 pp_separate_with (pp
, ',');
1702 /* Print out an expression-list in parens, as if it were the argument
1703 list to a function. */
1706 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1708 pp_c_left_paren (pp
);
1709 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1710 pp_c_expression_list (pp
, t
);
1711 pp_c_right_paren (pp
);
1714 /* unary-expression:
1718 unary-operator cast-expression
1719 sizeof unary-expression
1722 unary-operator: one of
1727 __alignof__ unary-expression
1728 __alignof__ ( type-id )
1729 __real__ unary-expression
1730 __imag__ unary-expression */
1733 c_pretty_printer::unary_expression (tree e
)
1735 enum tree_code code
= TREE_CODE (e
);
1738 case PREINCREMENT_EXPR
:
1739 case PREDECREMENT_EXPR
:
1740 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1741 unary_expression (TREE_OPERAND (e
, 0));
1748 case TRUTH_NOT_EXPR
:
1750 /* String literal are used by address. */
1751 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1752 pp_ampersand (this);
1753 else if (code
== INDIRECT_REF
)
1755 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
1756 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
1757 /* Reference decay is implicit, don't print anything. */;
1761 else if (code
== NEGATE_EXPR
)
1763 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1764 pp_complement (this);
1765 else if (code
== TRUTH_NOT_EXPR
)
1766 pp_exclamation (this);
1767 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1771 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1772 && integer_zerop (TREE_OPERAND (e
, 1)))
1773 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1777 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1779 pp_c_left_paren (this);
1780 if (!integer_onep (TYPE_SIZE_UNIT
1781 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1782 pp_c_type_cast (this, ptr_type_node
);
1784 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1785 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1788 pp_c_integer_constant (this,
1789 fold_convert (ssizetype
,
1790 TREE_OPERAND (e
, 1)));
1791 pp_c_right_paren (this);
1798 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1799 pp_c_whitespace (this);
1800 unary_expression (TREE_OPERAND (e
, 0));
1804 postfix_expression (e
);
1811 ( type-name ) cast-expression */
1814 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1816 switch (TREE_CODE (e
))
1819 case FIX_TRUNC_EXPR
:
1821 case VIEW_CONVERT_EXPR
:
1822 pp_c_type_cast (pp
, TREE_TYPE (e
));
1823 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1827 pp
->unary_expression (e
);
1831 /* multiplicative-expression:
1833 multiplicative-expression * cast-expression
1834 multiplicative-expression / cast-expression
1835 multiplicative-expression % cast-expression */
1838 c_pretty_printer::multiplicative_expression (tree e
)
1840 enum tree_code code
= TREE_CODE (e
);
1844 case TRUNC_DIV_EXPR
:
1845 case TRUNC_MOD_EXPR
:
1846 case EXACT_DIV_EXPR
:
1848 multiplicative_expression (TREE_OPERAND (e
, 0));
1849 pp_c_whitespace (this);
1850 if (code
== MULT_EXPR
)
1852 else if (code
== TRUNC_DIV_EXPR
)
1856 pp_c_whitespace (this);
1857 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1861 pp_c_cast_expression (this, e
);
1866 /* additive-expression:
1867 multiplicative-expression
1868 additive-expression + multiplicative-expression
1869 additive-expression - multiplicative-expression */
1872 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1874 enum tree_code code
= TREE_CODE (e
);
1877 case POINTER_PLUS_EXPR
:
1879 case POINTER_DIFF_EXPR
:
1881 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1882 pp_c_whitespace (pp
);
1883 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1887 pp_c_whitespace (pp
);
1888 pp
->multiplicative_expression (TREE_OPERAND (e
, 1));
1892 pp
->multiplicative_expression (e
);
1897 /* additive-expression:
1899 shift-expression << additive-expression
1900 shift-expression >> additive-expression */
1903 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1905 enum tree_code code
= TREE_CODE (e
);
1912 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1913 pp_c_whitespace (pp
);
1914 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
1915 code
== RSHIFT_EXPR
? ">>" :
1916 code
== LROTATE_EXPR
? "<<<" : ">>>");
1917 pp_c_whitespace (pp
);
1918 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1922 pp_c_additive_expression (pp
, e
);
1926 /* relational-expression:
1928 relational-expression < shift-expression
1929 relational-expression > shift-expression
1930 relational-expression <= shift-expression
1931 relational-expression >= shift-expression */
1934 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1936 enum tree_code code
= TREE_CODE (e
);
1943 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1944 pp_c_whitespace (pp
);
1945 if (code
== LT_EXPR
)
1947 else if (code
== GT_EXPR
)
1949 else if (code
== LE_EXPR
)
1951 else if (code
== GE_EXPR
)
1952 pp_greater_equal (pp
);
1953 pp_c_whitespace (pp
);
1954 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1958 pp_c_shift_expression (pp
, e
);
1963 /* equality-expression:
1964 relational-expression
1965 equality-expression == relational-expression
1966 equality-equality != relational-expression */
1969 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1971 enum tree_code code
= TREE_CODE (e
);
1976 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1977 pp_c_whitespace (pp
);
1978 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1979 pp_c_whitespace (pp
);
1980 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1984 pp_c_relational_expression (pp
, e
);
1991 AND-expression & equality-equality */
1994 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1996 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1998 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1999 pp_c_whitespace (pp
);
2001 pp_c_whitespace (pp
);
2002 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
2005 pp_c_equality_expression (pp
, e
);
2008 /* exclusive-OR-expression:
2010 exclusive-OR-expression ^ AND-expression */
2013 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2015 if (TREE_CODE (e
) == BIT_XOR_EXPR
2016 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2018 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2019 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2020 pp_c_maybe_whitespace (pp
);
2022 pp_c_whitespace (pp
);
2024 pp_c_whitespace (pp
);
2025 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2028 pp_c_and_expression (pp
, e
);
2031 /* inclusive-OR-expression:
2032 exclusive-OR-expression
2033 inclusive-OR-expression | exclusive-OR-expression */
2036 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2038 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2040 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2041 pp_c_whitespace (pp
);
2043 pp_c_whitespace (pp
);
2044 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2047 pp_c_exclusive_or_expression (pp
, e
);
2050 /* logical-AND-expression:
2051 inclusive-OR-expression
2052 logical-AND-expression && inclusive-OR-expression */
2055 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2057 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2058 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2060 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2061 pp_c_whitespace (pp
);
2062 pp_ampersand_ampersand (pp
);
2063 pp_c_whitespace (pp
);
2064 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2067 pp_c_inclusive_or_expression (pp
, e
);
2070 /* logical-OR-expression:
2071 logical-AND-expression
2072 logical-OR-expression || logical-AND-expression */
2075 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2077 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2078 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2080 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2081 pp_c_whitespace (pp
);
2083 pp_c_whitespace (pp
);
2084 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2087 pp_c_logical_and_expression (pp
, e
);
2090 /* conditional-expression:
2091 logical-OR-expression
2092 logical-OR-expression ? expression : conditional-expression */
2095 c_pretty_printer::conditional_expression (tree e
)
2097 if (TREE_CODE (e
) == COND_EXPR
)
2099 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2100 pp_c_whitespace (this);
2102 pp_c_whitespace (this);
2103 expression (TREE_OPERAND (e
, 1));
2104 pp_c_whitespace (this);
2106 pp_c_whitespace (this);
2107 conditional_expression (TREE_OPERAND (e
, 2));
2110 pp_c_logical_or_expression (this, e
);
2114 /* assignment-expression:
2115 conditional-expression
2116 unary-expression assignment-operator assignment-expression
2118 assignment-expression: one of
2119 = *= /= %= += -= >>= <<= &= ^= |= */
2122 c_pretty_printer::assignment_expression (tree e
)
2124 if (TREE_CODE (e
) == MODIFY_EXPR
2125 || TREE_CODE (e
) == INIT_EXPR
)
2127 unary_expression (TREE_OPERAND (e
, 0));
2128 pp_c_whitespace (this);
2131 expression (TREE_OPERAND (e
, 1));
2134 conditional_expression (e
);
2138 assignment-expression
2139 expression , assignment-expression
2141 Implementation note: instead of going through the usual recursion
2142 chain, I take the liberty of dispatching nodes to the appropriate
2143 functions. This makes some redundancy, but it worths it. That also
2144 prevents a possible infinite recursion between primary_expression ()
2145 and expression (). */
2148 c_pretty_printer::expression (tree e
)
2150 switch (TREE_CODE (e
))
2153 pp_c_void_constant (this);
2157 pp_c_integer_constant (this, e
);
2161 pp_c_floating_constant (this, e
);
2165 pp_c_fixed_constant (this, e
);
2169 pp_c_string_literal (this, e
);
2172 case IDENTIFIER_NODE
:
2181 primary_expression (e
);
2185 if (SSA_NAME_VAR (e
)
2186 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2187 expression (SSA_NAME_VAR (e
));
2189 translate_string ("<unknown>");
2192 case POSTINCREMENT_EXPR
:
2193 case POSTDECREMENT_EXPR
:
2195 case ARRAY_NOTATION_REF
:
2203 case UNORDERED_EXPR
:
2214 case COMPOUND_LITERAL_EXPR
:
2216 postfix_expression (e
);
2225 case TRUTH_NOT_EXPR
:
2226 case PREINCREMENT_EXPR
:
2227 case PREDECREMENT_EXPR
:
2230 unary_expression (e
);
2234 case FIX_TRUNC_EXPR
:
2236 case VIEW_CONVERT_EXPR
:
2237 pp_c_cast_expression (this, e
);
2241 case TRUNC_MOD_EXPR
:
2242 case TRUNC_DIV_EXPR
:
2243 case EXACT_DIV_EXPR
:
2245 multiplicative_expression (e
);
2252 pp_c_shift_expression (this, e
);
2259 pp_c_relational_expression (this, e
);
2263 pp_c_and_expression (this, e
);
2267 case TRUTH_XOR_EXPR
:
2268 pp_c_exclusive_or_expression (this, e
);
2272 pp_c_inclusive_or_expression (this, e
);
2275 case TRUTH_ANDIF_EXPR
:
2276 case TRUTH_AND_EXPR
:
2277 pp_c_logical_and_expression (this, e
);
2280 case TRUTH_ORIF_EXPR
:
2282 pp_c_logical_or_expression (this, e
);
2287 pp_c_equality_expression (this, e
);
2291 conditional_expression (e
);
2294 case POINTER_PLUS_EXPR
:
2296 case POINTER_DIFF_EXPR
:
2298 pp_c_additive_expression (this, e
);
2303 assignment_expression (e
);
2307 pp_c_left_paren (this);
2308 expression (TREE_OPERAND (e
, 0));
2309 pp_separate_with (this, ',');
2310 assignment_expression (TREE_OPERAND (e
, 1));
2311 pp_c_right_paren (this);
2314 case NON_LVALUE_EXPR
:
2316 expression (TREE_OPERAND (e
, 0));
2320 postfix_expression (TREE_OPERAND (e
, 1));
2325 /* We don't yet have a way of dumping statements in a
2326 human-readable format. */
2327 pp_string (this, "({...})");
2330 case C_MAYBE_CONST_EXPR
:
2331 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2335 pp_unsupported_tree (this, e
);
2345 c_pretty_printer::statement (tree stmt
)
2350 if (pp_needs_newline (this))
2351 pp_newline_and_indent (this, 0);
2353 dump_generic_node (this, stmt
, pp_indentation (this), 0, true);
2357 /* Initialize the PRETTY-PRINTER for handling C codes. */
2359 c_pretty_printer::c_pretty_printer ()
2360 : pretty_printer (),
2364 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2365 ptr_operator
= pp_c_pointer
;
2366 parameter_list
= pp_c_parameter_type_list
;
2370 /* Print the tree T in full, on file FILE. */
2373 print_c_tree (FILE *file
, tree t
)
2375 c_pretty_printer pp
;
2377 pp_needs_newline (&pp
) = true;
2378 pp
.buffer
->stream
= file
;
2380 pp_newline_and_flush (&pp
);
2383 /* Print the tree T in full, on stderr. */
2386 debug_c_tree (tree t
)
2388 print_c_tree (stderr
, t
);
2389 fputc ('\n', stderr
);
2392 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2393 up of T's memory address. */
2396 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2400 gcc_assert (DECL_P (t
));
2403 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2406 static char xname
[8];
2407 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2412 pp_c_identifier (pp
, name
);