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
));
525 pp_c_right_paren (pp
);
528 /* abstract-declarator:
530 pointer(opt) direct-abstract-declarator */
533 c_pretty_printer::abstract_declarator (tree t
)
535 if (TREE_CODE (t
) == POINTER_TYPE
)
537 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
538 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
539 pp_c_right_paren (this);
543 direct_abstract_declarator (t
);
546 /* direct-abstract-declarator:
547 ( abstract-declarator )
548 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
549 direct-abstract-declarator(opt) [ * ]
550 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
553 c_pretty_printer::direct_abstract_declarator (tree t
)
555 switch (TREE_CODE (t
))
558 abstract_declarator (t
);
562 pp_c_parameter_type_list (this, t
);
563 direct_abstract_declarator (TREE_TYPE (t
));
567 pp_c_left_bracket (this);
568 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
570 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
571 tree type
= TREE_TYPE (maxval
);
573 if (tree_fits_shwi_p (maxval
))
574 pp_wide_integer (this, tree_to_shwi (maxval
) + 1);
576 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
577 build_int_cst (type
, 1)));
579 pp_c_right_bracket (this);
580 direct_abstract_declarator (TREE_TYPE (t
));
583 case IDENTIFIER_NODE
:
588 case FIXED_POINT_TYPE
:
598 pp_unsupported_tree (this, t
);
604 specifier-qualifier-list abstract-declarator(opt) */
607 c_pretty_printer::type_id (tree t
)
609 pp_c_specifier_qualifier_list (this, t
);
610 abstract_declarator (t
);
613 /* storage-class-specifier:
621 c_pretty_printer::storage_class_specifier (tree t
)
623 if (TREE_CODE (t
) == TYPE_DECL
)
624 pp_c_ws_string (this, "typedef");
627 if (DECL_REGISTER (t
))
628 pp_c_ws_string (this, "register");
629 else if (TREE_STATIC (t
) && VAR_P (t
))
630 pp_c_ws_string (this, "static");
634 /* function-specifier:
638 c_pretty_printer::function_specifier (tree t
)
640 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
641 pp_c_ws_string (this, "inline");
644 /* declaration-specifiers:
645 storage-class-specifier declaration-specifiers(opt)
646 type-specifier declaration-specifiers(opt)
647 type-qualifier declaration-specifiers(opt)
648 function-specifier declaration-specifiers(opt) */
651 c_pretty_printer::declaration_specifiers (tree t
)
653 storage_class_specifier (t
);
654 function_specifier (t
);
655 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
661 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
662 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
663 direct-declarator [ type-qualifier-list static assignment-expression ]
664 direct-declarator [ type-qualifier-list * ]
665 direct-declarator ( parameter-type-list )
666 direct-declarator ( identifier-list(opt) ) */
669 c_pretty_printer::direct_declarator (tree t
)
671 switch (TREE_CODE (t
))
678 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
679 pp_c_tree_decl_identifier (this, t
);
684 abstract_declarator (TREE_TYPE (t
));
688 pp_parameter_list (this, t
);
689 abstract_declarator (TREE_TYPE (t
));
693 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
694 pp_c_tree_decl_identifier (this, t
);
695 if (flags
& pp_c_flag_abstract
)
696 abstract_declarator (TREE_TYPE (t
));
699 pp_parameter_list (this, t
);
700 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
706 case FIXED_POINT_TYPE
:
713 pp_unsupported_tree (this, t
);
720 pointer(opt) direct-declarator */
723 c_pretty_printer::declarator (tree t
)
725 switch (TREE_CODE (t
))
729 case FIXED_POINT_TYPE
:
742 direct_declarator (t
);
747 pp_unsupported_tree (this, t
);
753 declaration-specifiers init-declarator-list(opt) ; */
756 c_pretty_printer::declaration (tree t
)
758 declaration_specifiers (t
);
759 pp_c_init_declarator (this, t
);
762 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
765 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
767 if (attributes
== NULL_TREE
)
770 pp_c_ws_string (pp
, "__attribute__");
771 pp_c_left_paren (pp
);
772 pp_c_left_paren (pp
);
773 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
775 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
776 if (TREE_VALUE (attributes
))
777 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
779 if (TREE_CHAIN (attributes
))
780 pp_separate_with (pp
, ',');
782 pp_c_right_paren (pp
);
783 pp_c_right_paren (pp
);
786 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
787 marked to be displayed on disgnostic. */
790 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
792 bool is_first
= true;
797 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
799 const struct attribute_spec
*as
;
800 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
801 if (!as
|| as
->affects_type_identity
== false)
804 && !strcmp ("transaction_safe", as
->name
))
805 /* In C++ transaction_safe is printed at the end of the declarator. */
809 pp_c_ws_string (pp
, "__attribute__");
810 pp_c_left_paren (pp
);
811 pp_c_left_paren (pp
);
816 pp_separate_with (pp
, ',');
818 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
820 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
825 pp_c_right_paren (pp
);
826 pp_c_right_paren (pp
);
827 pp_c_whitespace (pp
);
831 /* function-definition:
832 declaration-specifiers declarator compound-statement */
835 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
837 pp
->declaration_specifiers (t
);
839 pp_needs_newline (pp
) = true;
840 pp
->statement (DECL_SAVED_TREE (t
));
841 pp_newline_and_flush (pp
);
847 /* Print out a c-char. This is called solely for characters which are
848 in the *target* execution character set. We ought to convert them
849 back to the *host* execution character set before printing, but we
850 have no way to do this at present. A decent compromise is to print
851 all characters as if they were in the host execution character set,
852 and not attempt to recover any named escape characters, but render
853 all unprintables as octal escapes. If the host and target character
854 sets are the same, this produces relatively readable output. If they
855 are not the same, strings may appear as gibberish, but that's okay
856 (in fact, it may well be what the reader wants, e.g. if they are looking
857 to see if conversion to the target character set happened correctly).
859 A special case: we need to prefix \, ", and ' with backslashes. It is
860 correct to do so for the *host*'s \, ", and ', because the rest of the
861 file appears in the host character set. */
864 pp_c_char (c_pretty_printer
*pp
, int c
)
870 case '\\': pp_string (pp
, "\\\\"); break;
871 case '\'': pp_string (pp
, "\\\'"); break;
872 case '\"': pp_string (pp
, "\\\""); break;
873 default: pp_character (pp
, c
);
877 pp_scalar (pp
, "\\%03o", (unsigned) c
);
880 /* Print out a STRING literal. */
883 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
885 const char *p
= TREE_STRING_POINTER (s
);
886 int n
= TREE_STRING_LENGTH (s
) - 1;
889 for (i
= 0; i
< n
; ++i
)
890 pp_c_char (pp
, p
[i
]);
894 /* Pretty-print a VOID_CST (void_node). */
897 pp_c_void_constant (c_pretty_printer
*pp
)
899 pp_c_type_cast (pp
, void_type_node
);
903 /* Pretty-print an INTEGER literal. */
906 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
908 if (tree_fits_shwi_p (i
))
909 pp_wide_integer (pp
, tree_to_shwi (i
));
910 else if (tree_fits_uhwi_p (i
))
911 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
916 if (wi::lt_p (i
, 0, TYPE_SIGN (TREE_TYPE (i
))))
921 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
922 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
926 /* Print out a CHARACTER literal. */
929 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
932 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
936 /* Print out a BOOLEAN literal. */
939 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
941 if (b
== boolean_false_node
)
943 if (c_dialect_cxx ())
944 pp_c_ws_string (pp
, "false");
945 else if (flag_isoc99
)
946 pp_c_ws_string (pp
, "_False");
948 pp_unsupported_tree (pp
, b
);
950 else if (b
== boolean_true_node
)
952 if (c_dialect_cxx ())
953 pp_c_ws_string (pp
, "true");
954 else if (flag_isoc99
)
955 pp_c_ws_string (pp
, "_True");
957 pp_unsupported_tree (pp
, b
);
959 else if (TREE_CODE (b
) == INTEGER_CST
)
960 pp_c_integer_constant (pp
, b
);
962 pp_unsupported_tree (pp
, b
);
965 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
966 false; that means the value was obtained by a cast, in which case
967 print out the type-id part of the cast-expression -- the casted value
968 is then printed by pp_c_integer_literal. */
971 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
973 bool value_is_named
= true;
974 tree type
= TREE_TYPE (e
);
977 /* Find the name of this constant. */
978 for (value
= TYPE_VALUES (type
);
979 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
980 value
= TREE_CHAIN (value
))
983 if (value
!= NULL_TREE
)
984 pp
->id_expression (TREE_PURPOSE (value
));
987 /* Value must have been cast. */
988 pp_c_type_cast (pp
, type
);
989 value_is_named
= false;
992 return value_is_named
;
995 /* Print out a REAL value as a decimal-floating-constant. */
998 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1000 const struct real_format
*fmt
1001 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1003 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1004 bool is_decimal
= floating_cst
.decimal
;
1006 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1007 log10(2) to 7 significant digits. */
1008 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1010 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1011 sizeof (pp_buffer (pp
)->digit_buffer
),
1014 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1015 if (TREE_TYPE (r
) == float_type_node
)
1016 pp_character (pp
, 'f');
1017 else if (TREE_TYPE (r
) == long_double_type_node
)
1018 pp_character (pp
, 'l');
1019 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1020 pp_string (pp
, "dl");
1021 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1022 pp_string (pp
, "dd");
1023 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1024 pp_string (pp
, "df");
1025 else if (TREE_TYPE (r
) != double_type_node
)
1026 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1027 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1029 pp_character (pp
, 'f');
1030 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1031 if (floatn_nx_types
[i
].extended
)
1032 pp_character (pp
, 'x');
1037 /* Print out a FIXED value as a decimal-floating-constant. */
1040 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1042 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1043 sizeof (pp_buffer (pp
)->digit_buffer
));
1044 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1047 /* Pretty-print a compound literal expression. GNU extensions include
1048 vector constants. */
1051 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1053 tree type
= TREE_TYPE (e
);
1054 pp_c_type_cast (pp
, type
);
1056 switch (TREE_CODE (type
))
1063 pp_c_brace_enclosed_initializer_list (pp
, e
);
1067 pp_unsupported_tree (pp
, e
);
1072 /* Pretty-print a COMPLEX_EXPR expression. */
1075 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1077 /* Handle a few common special cases, otherwise fallback
1078 to printing it as compound literal. */
1079 tree type
= TREE_TYPE (e
);
1080 tree realexpr
= TREE_OPERAND (e
, 0);
1081 tree imagexpr
= TREE_OPERAND (e
, 1);
1083 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1084 if (TREE_CODE (realexpr
) == NOP_EXPR
1085 && TREE_CODE (imagexpr
) == NOP_EXPR
1086 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1087 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1088 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1089 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1090 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1091 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1093 pp_c_type_cast (pp
, type
);
1094 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1098 /* Cast of an scalar expression to COMPLEX_TYPE. */
1099 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1100 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1102 pp_c_type_cast (pp
, type
);
1103 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1104 realexpr
= TREE_OPERAND (realexpr
, 0);
1105 pp
->expression (realexpr
);
1109 pp_c_compound_literal (pp
, e
);
1115 fixed-point-constant
1116 enumeration-constant
1117 character-constant */
1120 c_pretty_printer::constant (tree e
)
1122 const enum tree_code code
= TREE_CODE (e
);
1127 pp_c_void_constant (this);
1132 tree type
= TREE_TYPE (e
);
1133 if (type
== boolean_type_node
)
1134 pp_c_bool_constant (this, e
);
1135 else if (type
== char_type_node
)
1136 pp_c_character_constant (this, e
);
1137 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1138 && pp_c_enumeration_constant (this, e
))
1141 pp_c_integer_constant (this, e
);
1146 pp_c_floating_constant (this, e
);
1150 pp_c_fixed_constant (this, e
);
1154 pp_c_string_literal (this, e
);
1158 /* Sometimes, we are confused and we think a complex literal
1159 is a constant. Such thing is a compound literal which
1160 grammatically belongs to postfix-expr production. */
1161 pp_c_compound_literal (this, e
);
1165 pp_unsupported_tree (this, e
);
1170 /* Pretty-print a string such as an identifier, without changing its
1171 encoding, preceded by whitespace is necessary. */
1174 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1176 pp_c_maybe_whitespace (pp
);
1177 pp_string (pp
, str
);
1178 pp
->padding
= pp_before
;
1182 c_pretty_printer::translate_string (const char *gmsgid
)
1184 if (pp_translate_identifiers (this))
1185 pp_c_ws_string (this, _(gmsgid
));
1187 pp_c_ws_string (this, gmsgid
);
1190 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1191 that need converting to the locale encoding, preceded by whitespace
1195 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1197 pp_c_maybe_whitespace (pp
);
1198 pp_identifier (pp
, id
);
1199 pp
->padding
= pp_before
;
1202 /* Pretty-print a C primary-expression.
1210 c_pretty_printer::primary_expression (tree e
)
1212 switch (TREE_CODE (e
))
1220 pp_c_tree_decl_identifier (this, e
);
1223 case IDENTIFIER_NODE
:
1224 pp_c_tree_identifier (this, e
);
1228 translate_string ("<erroneous-expression>");
1232 translate_string ("<return-value>");
1244 pp_c_ws_string (this, "__builtin_memcpy");
1245 pp_c_left_paren (this);
1246 pp_ampersand (this);
1247 primary_expression (TREE_OPERAND (e
, 0));
1248 pp_separate_with (this, ',');
1249 pp_ampersand (this);
1250 initializer (TREE_OPERAND (e
, 1));
1251 if (TREE_OPERAND (e
, 2))
1253 pp_separate_with (this, ',');
1254 expression (TREE_OPERAND (e
, 2));
1256 pp_c_right_paren (this);
1260 /* FIXME: Make sure we won't get into an infinite loop. */
1261 pp_c_left_paren (this);
1263 pp_c_right_paren (this);
1268 /* Print out a C initializer -- also support C compound-literals.
1270 assignment-expression:
1271 { initializer-list }
1272 { initializer-list , } */
1275 c_pretty_printer::initializer (tree e
)
1277 if (TREE_CODE (e
) == CONSTRUCTOR
)
1278 pp_c_brace_enclosed_initializer_list (this, e
);
1285 declarator = initializer */
1288 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1291 /* We don't want to output function definitions here. There are handled
1292 elsewhere (and the syntactic form is bogus anyway). */
1293 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1295 tree init
= DECL_INITIAL (t
);
1296 /* This C++ bit is handled here because it is easier to do so.
1297 In templates, the C++ parser builds a TREE_LIST for a
1298 direct-initialization; the TREE_PURPOSE is the variable to
1299 initialize and the TREE_VALUE is the initializer. */
1300 if (TREE_CODE (init
) == TREE_LIST
)
1302 pp_c_left_paren (pp
);
1303 pp
->expression (TREE_VALUE (init
));
1304 pp_right_paren (pp
);
1311 pp
->initializer (init
);
1316 /* initializer-list:
1317 designation(opt) initializer
1318 initializer-list , designation(opt) initializer
1325 designator-list designator
1328 [ constant-expression ]
1332 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1334 tree type
= TREE_TYPE (e
);
1335 const enum tree_code code
= TREE_CODE (type
);
1337 if (TREE_CODE (e
) == CONSTRUCTOR
)
1339 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1349 tree init
= TREE_OPERAND (e
, 0);
1350 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1352 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1355 pp
->primary_expression (TREE_PURPOSE (init
));
1359 pp_c_left_bracket (pp
);
1360 if (TREE_PURPOSE (init
))
1361 pp
->constant (TREE_PURPOSE (init
));
1362 pp_c_right_bracket (pp
);
1364 pp_c_whitespace (pp
);
1366 pp_c_whitespace (pp
);
1367 pp
->initializer (TREE_VALUE (init
));
1368 if (TREE_CHAIN (init
))
1369 pp_separate_with (pp
, ',');
1375 if (TREE_CODE (e
) == VECTOR_CST
)
1378 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1381 pp_separate_with (pp
, ',');
1382 pp
->expression (VECTOR_CST_ELT (e
, i
));
1390 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1392 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1393 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1394 pp_separate_with (pp
, ',');
1395 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1405 pp_unsupported_tree (pp
, type
);
1408 /* Pretty-print a brace-enclosed initializer-list. */
1411 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1413 pp_c_left_brace (pp
);
1414 pp_c_initializer_list (pp
, l
);
1415 pp_c_right_brace (pp
);
1419 /* This is a convenient function, used to bridge gap between C and C++
1426 c_pretty_printer::id_expression (tree t
)
1428 switch (TREE_CODE (t
))
1437 pp_c_tree_decl_identifier (this, t
);
1440 case IDENTIFIER_NODE
:
1441 pp_c_tree_identifier (this, t
);
1445 pp_unsupported_tree (this, t
);
1450 /* postfix-expression:
1452 postfix-expression [ expression ]
1453 postfix-expression ( argument-expression-list(opt) )
1454 postfix-expression . identifier
1455 postfix-expression -> identifier
1456 postfix-expression ++
1457 postfix-expression --
1458 ( type-name ) { initializer-list }
1459 ( type-name ) { initializer-list , } */
1462 c_pretty_printer::postfix_expression (tree e
)
1464 enum tree_code code
= TREE_CODE (e
);
1467 case POSTINCREMENT_EXPR
:
1468 case POSTDECREMENT_EXPR
:
1469 postfix_expression (TREE_OPERAND (e
, 0));
1470 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1474 postfix_expression (TREE_OPERAND (e
, 0));
1475 pp_c_left_bracket (this);
1476 expression (TREE_OPERAND (e
, 1));
1477 pp_c_right_bracket (this);
1480 case ARRAY_NOTATION_REF
:
1481 postfix_expression (ARRAY_NOTATION_ARRAY (e
));
1482 pp_c_left_bracket (this);
1483 expression (ARRAY_NOTATION_START (e
));
1485 expression (ARRAY_NOTATION_LENGTH (e
));
1487 expression (ARRAY_NOTATION_STRIDE (e
));
1488 pp_c_right_bracket (this);
1493 call_expr_arg_iterator iter
;
1495 postfix_expression (CALL_EXPR_FN (e
));
1496 pp_c_left_paren (this);
1497 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1500 if (more_call_expr_args_p (&iter
))
1501 pp_separate_with (this, ',');
1503 pp_c_right_paren (this);
1507 case UNORDERED_EXPR
:
1508 pp_c_ws_string (this, flag_isoc99
1510 : "__builtin_isunordered");
1514 pp_c_ws_string (this, flag_isoc99
1516 : "!__builtin_isunordered");
1520 pp_c_ws_string (this, flag_isoc99
1522 : "!__builtin_isgreaterequal");
1526 pp_c_ws_string (this, flag_isoc99
1528 : "!__builtin_isgreater");
1532 pp_c_ws_string (this, flag_isoc99
1534 : "!__builtin_islessequal");
1538 pp_c_ws_string (this, flag_isoc99
1540 : "!__builtin_isless");
1544 pp_c_ws_string (this, flag_isoc99
1546 : "!__builtin_islessgreater");
1550 pp_c_ws_string (this, flag_isoc99
1552 : "__builtin_islessgreater");
1556 pp_c_ws_string (this, "max");
1560 pp_c_ws_string (this, "min");
1564 pp_c_left_paren (this);
1565 expression (TREE_OPERAND (e
, 0));
1566 pp_separate_with (this, ',');
1567 expression (TREE_OPERAND (e
, 1));
1568 pp_c_right_paren (this);
1572 pp_c_ws_string (this, "__builtin_abs");
1573 pp_c_left_paren (this);
1574 expression (TREE_OPERAND (e
, 0));
1575 pp_c_right_paren (this);
1580 tree object
= TREE_OPERAND (e
, 0);
1581 if (INDIRECT_REF_P (object
))
1583 postfix_expression (TREE_OPERAND (object
, 0));
1588 postfix_expression (object
);
1591 expression (TREE_OPERAND (e
, 1));
1597 tree type
= TREE_TYPE (e
);
1599 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1601 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1603 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1604 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1605 if ((bitpos
% size
) == 0)
1607 pp_c_left_paren (this);
1608 pp_c_left_paren (this);
1611 pp_c_right_paren (this);
1612 pp_c_ampersand (this);
1613 expression (TREE_OPERAND (e
, 0));
1614 pp_c_right_paren (this);
1615 pp_c_left_bracket (this);
1616 pp_wide_integer (this, bitpos
/ size
);
1617 pp_c_right_bracket (this);
1621 pp_unsupported_tree (this, e
);
1631 pp_c_compound_literal (this, e
);
1635 pp_c_complex_expr (this, e
);
1638 case COMPOUND_LITERAL_EXPR
:
1639 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1646 pp_c_ws_string (this, "__builtin_va_arg");
1647 pp_c_left_paren (this);
1648 assignment_expression (TREE_OPERAND (e
, 0));
1649 pp_separate_with (this, ',');
1650 type_id (TREE_TYPE (e
));
1651 pp_c_right_paren (this);
1655 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1657 id_expression (TREE_OPERAND (e
, 0));
1663 primary_expression (e
);
1668 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1671 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1673 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1675 pp
->expression (TREE_VALUE (e
));
1677 pp_separate_with (pp
, ',');
1681 /* Print out V, which contains the elements of a constructor. */
1684 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1686 unsigned HOST_WIDE_INT ix
;
1689 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1691 pp
->expression (value
);
1692 if (ix
!= vec_safe_length (v
) - 1)
1693 pp_separate_with (pp
, ',');
1697 /* Print out an expression-list in parens, as if it were the argument
1698 list to a function. */
1701 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1703 pp_c_left_paren (pp
);
1704 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1705 pp_c_expression_list (pp
, t
);
1706 pp_c_right_paren (pp
);
1709 /* unary-expression:
1713 unary-operator cast-expression
1714 sizeof unary-expression
1717 unary-operator: one of
1722 __alignof__ unary-expression
1723 __alignof__ ( type-id )
1724 __real__ unary-expression
1725 __imag__ unary-expression */
1728 c_pretty_printer::unary_expression (tree e
)
1730 enum tree_code code
= TREE_CODE (e
);
1733 case PREINCREMENT_EXPR
:
1734 case PREDECREMENT_EXPR
:
1735 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1736 unary_expression (TREE_OPERAND (e
, 0));
1743 case TRUTH_NOT_EXPR
:
1745 /* String literal are used by address. */
1746 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1747 pp_ampersand (this);
1748 else if (code
== INDIRECT_REF
)
1750 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
1751 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
1752 /* Reference decay is implicit, don't print anything. */;
1756 else if (code
== NEGATE_EXPR
)
1758 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1759 pp_complement (this);
1760 else if (code
== TRUTH_NOT_EXPR
)
1761 pp_exclamation (this);
1762 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1766 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1767 && integer_zerop (TREE_OPERAND (e
, 1)))
1768 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1772 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1774 pp_c_left_paren (this);
1775 if (!integer_onep (TYPE_SIZE_UNIT
1776 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1777 pp_c_type_cast (this, ptr_type_node
);
1779 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1780 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1783 pp_c_integer_constant (this,
1784 fold_convert (ssizetype
,
1785 TREE_OPERAND (e
, 1)));
1786 pp_c_right_paren (this);
1793 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1794 pp_c_whitespace (this);
1795 unary_expression (TREE_OPERAND (e
, 0));
1799 postfix_expression (e
);
1806 ( type-name ) cast-expression */
1809 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1811 switch (TREE_CODE (e
))
1814 case FIX_TRUNC_EXPR
:
1816 case VIEW_CONVERT_EXPR
:
1817 pp_c_type_cast (pp
, TREE_TYPE (e
));
1818 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1822 pp
->unary_expression (e
);
1826 /* multiplicative-expression:
1828 multiplicative-expression * cast-expression
1829 multiplicative-expression / cast-expression
1830 multiplicative-expression % cast-expression */
1833 c_pretty_printer::multiplicative_expression (tree e
)
1835 enum tree_code code
= TREE_CODE (e
);
1839 case TRUNC_DIV_EXPR
:
1840 case TRUNC_MOD_EXPR
:
1841 case EXACT_DIV_EXPR
:
1843 multiplicative_expression (TREE_OPERAND (e
, 0));
1844 pp_c_whitespace (this);
1845 if (code
== MULT_EXPR
)
1847 else if (code
== TRUNC_DIV_EXPR
)
1851 pp_c_whitespace (this);
1852 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1856 pp_c_cast_expression (this, e
);
1861 /* additive-expression:
1862 multiplicative-expression
1863 additive-expression + multiplicative-expression
1864 additive-expression - multiplicative-expression */
1867 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1869 enum tree_code code
= TREE_CODE (e
);
1872 case POINTER_PLUS_EXPR
:
1875 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1876 pp_c_whitespace (pp
);
1877 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1881 pp_c_whitespace (pp
);
1882 pp
->multiplicative_expression (TREE_OPERAND (e
, 1));
1886 pp
->multiplicative_expression (e
);
1891 /* additive-expression:
1893 shift-expression << additive-expression
1894 shift-expression >> additive-expression */
1897 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1899 enum tree_code code
= TREE_CODE (e
);
1906 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1907 pp_c_whitespace (pp
);
1908 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
1909 code
== RSHIFT_EXPR
? ">>" :
1910 code
== LROTATE_EXPR
? "<<<" : ">>>");
1911 pp_c_whitespace (pp
);
1912 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1916 pp_c_additive_expression (pp
, e
);
1920 /* relational-expression:
1922 relational-expression < shift-expression
1923 relational-expression > shift-expression
1924 relational-expression <= shift-expression
1925 relational-expression >= shift-expression */
1928 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1930 enum tree_code code
= TREE_CODE (e
);
1937 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1938 pp_c_whitespace (pp
);
1939 if (code
== LT_EXPR
)
1941 else if (code
== GT_EXPR
)
1943 else if (code
== LE_EXPR
)
1945 else if (code
== GE_EXPR
)
1946 pp_greater_equal (pp
);
1947 pp_c_whitespace (pp
);
1948 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1952 pp_c_shift_expression (pp
, e
);
1957 /* equality-expression:
1958 relational-expression
1959 equality-expression == relational-expression
1960 equality-equality != relational-expression */
1963 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1965 enum tree_code code
= TREE_CODE (e
);
1970 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1971 pp_c_whitespace (pp
);
1972 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1973 pp_c_whitespace (pp
);
1974 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1978 pp_c_relational_expression (pp
, e
);
1985 AND-expression & equality-equality */
1988 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1990 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1992 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1993 pp_c_whitespace (pp
);
1995 pp_c_whitespace (pp
);
1996 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1999 pp_c_equality_expression (pp
, e
);
2002 /* exclusive-OR-expression:
2004 exclusive-OR-expression ^ AND-expression */
2007 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2009 if (TREE_CODE (e
) == BIT_XOR_EXPR
2010 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2012 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2013 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2014 pp_c_maybe_whitespace (pp
);
2016 pp_c_whitespace (pp
);
2018 pp_c_whitespace (pp
);
2019 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2022 pp_c_and_expression (pp
, e
);
2025 /* inclusive-OR-expression:
2026 exclusive-OR-expression
2027 inclusive-OR-expression | exclusive-OR-expression */
2030 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2032 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2034 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2035 pp_c_whitespace (pp
);
2037 pp_c_whitespace (pp
);
2038 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2041 pp_c_exclusive_or_expression (pp
, e
);
2044 /* logical-AND-expression:
2045 inclusive-OR-expression
2046 logical-AND-expression && inclusive-OR-expression */
2049 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2051 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2052 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2054 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2055 pp_c_whitespace (pp
);
2056 pp_ampersand_ampersand (pp
);
2057 pp_c_whitespace (pp
);
2058 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2061 pp_c_inclusive_or_expression (pp
, e
);
2064 /* logical-OR-expression:
2065 logical-AND-expression
2066 logical-OR-expression || logical-AND-expression */
2069 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2071 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2072 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2074 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2075 pp_c_whitespace (pp
);
2077 pp_c_whitespace (pp
);
2078 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2081 pp_c_logical_and_expression (pp
, e
);
2084 /* conditional-expression:
2085 logical-OR-expression
2086 logical-OR-expression ? expression : conditional-expression */
2089 c_pretty_printer::conditional_expression (tree e
)
2091 if (TREE_CODE (e
) == COND_EXPR
)
2093 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2094 pp_c_whitespace (this);
2096 pp_c_whitespace (this);
2097 expression (TREE_OPERAND (e
, 1));
2098 pp_c_whitespace (this);
2100 pp_c_whitespace (this);
2101 conditional_expression (TREE_OPERAND (e
, 2));
2104 pp_c_logical_or_expression (this, e
);
2108 /* assignment-expression:
2109 conditional-expression
2110 unary-expression assignment-operator assignment-expression
2112 assignment-expression: one of
2113 = *= /= %= += -= >>= <<= &= ^= |= */
2116 c_pretty_printer::assignment_expression (tree e
)
2118 if (TREE_CODE (e
) == MODIFY_EXPR
2119 || TREE_CODE (e
) == INIT_EXPR
)
2121 unary_expression (TREE_OPERAND (e
, 0));
2122 pp_c_whitespace (this);
2125 expression (TREE_OPERAND (e
, 1));
2128 conditional_expression (e
);
2132 assignment-expression
2133 expression , assignment-expression
2135 Implementation note: instead of going through the usual recursion
2136 chain, I take the liberty of dispatching nodes to the appropriate
2137 functions. This makes some redundancy, but it worths it. That also
2138 prevents a possible infinite recursion between primary_expression ()
2139 and expression (). */
2142 c_pretty_printer::expression (tree e
)
2144 switch (TREE_CODE (e
))
2147 pp_c_void_constant (this);
2151 pp_c_integer_constant (this, e
);
2155 pp_c_floating_constant (this, e
);
2159 pp_c_fixed_constant (this, e
);
2163 pp_c_string_literal (this, e
);
2166 case IDENTIFIER_NODE
:
2175 primary_expression (e
);
2179 if (SSA_NAME_VAR (e
)
2180 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2181 expression (SSA_NAME_VAR (e
));
2183 translate_string ("<unknown>");
2186 case POSTINCREMENT_EXPR
:
2187 case POSTDECREMENT_EXPR
:
2189 case ARRAY_NOTATION_REF
:
2197 case UNORDERED_EXPR
:
2208 case COMPOUND_LITERAL_EXPR
:
2210 postfix_expression (e
);
2219 case TRUTH_NOT_EXPR
:
2220 case PREINCREMENT_EXPR
:
2221 case PREDECREMENT_EXPR
:
2224 unary_expression (e
);
2228 case FIX_TRUNC_EXPR
:
2230 case VIEW_CONVERT_EXPR
:
2231 pp_c_cast_expression (this, e
);
2235 case TRUNC_MOD_EXPR
:
2236 case TRUNC_DIV_EXPR
:
2237 case EXACT_DIV_EXPR
:
2239 multiplicative_expression (e
);
2246 pp_c_shift_expression (this, e
);
2253 pp_c_relational_expression (this, e
);
2257 pp_c_and_expression (this, e
);
2261 case TRUTH_XOR_EXPR
:
2262 pp_c_exclusive_or_expression (this, e
);
2266 pp_c_inclusive_or_expression (this, e
);
2269 case TRUTH_ANDIF_EXPR
:
2270 case TRUTH_AND_EXPR
:
2271 pp_c_logical_and_expression (this, e
);
2274 case TRUTH_ORIF_EXPR
:
2276 pp_c_logical_or_expression (this, e
);
2281 pp_c_equality_expression (this, e
);
2285 conditional_expression (e
);
2288 case POINTER_PLUS_EXPR
:
2291 pp_c_additive_expression (this, e
);
2296 assignment_expression (e
);
2300 pp_c_left_paren (this);
2301 expression (TREE_OPERAND (e
, 0));
2302 pp_separate_with (this, ',');
2303 assignment_expression (TREE_OPERAND (e
, 1));
2304 pp_c_right_paren (this);
2307 case NON_LVALUE_EXPR
:
2309 expression (TREE_OPERAND (e
, 0));
2313 postfix_expression (TREE_OPERAND (e
, 1));
2318 /* We don't yet have a way of dumping statements in a
2319 human-readable format. */
2320 pp_string (this, "({...})");
2323 case C_MAYBE_CONST_EXPR
:
2324 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2328 pp_unsupported_tree (this, e
);
2338 c_pretty_printer::statement (tree stmt
)
2343 if (pp_needs_newline (this))
2344 pp_newline_and_indent (this, 0);
2346 dump_generic_node (this, stmt
, pp_indentation (this), 0, true);
2350 /* Initialize the PRETTY-PRINTER for handling C codes. */
2352 c_pretty_printer::c_pretty_printer ()
2353 : pretty_printer (),
2357 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2358 ptr_operator
= pp_c_pointer
;
2359 parameter_list
= pp_c_parameter_type_list
;
2363 /* Print the tree T in full, on file FILE. */
2366 print_c_tree (FILE *file
, tree t
)
2368 c_pretty_printer pp
;
2370 pp_needs_newline (&pp
) = true;
2371 pp
.buffer
->stream
= file
;
2373 pp_newline_and_flush (&pp
);
2376 /* Print the tree T in full, on stderr. */
2379 debug_c_tree (tree t
)
2381 print_c_tree (stderr
, t
);
2382 fputc ('\n', stderr
);
2385 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2386 up of T's memory address. */
2389 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2393 gcc_assert (DECL_P (t
));
2396 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2399 static char xname
[8];
2400 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2405 pp_c_identifier (pp
, name
);