1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2014 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"
26 #include "stor-layout.h"
29 #include "c-pretty-print.h"
30 #include "tree-pretty-print.h"
31 #include "tree-iterator.h"
32 #include "diagnostic.h"
33 #include "wide-int-print.h"
35 /* The pretty-printer code is primarily designed to closely follow
36 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
37 codes we used to have in the past. Following a structured
38 approach (preferably the official grammars) is believed to make it
39 much easier to add extensions and nifty pretty-printing effects that
40 takes expression or declaration contexts into account. */
43 #define pp_c_maybe_whitespace(PP) \
45 if ((PP)->padding == pp_before) \
46 pp_c_whitespace (PP); \
50 static void pp_c_char (c_pretty_printer
*, int);
52 /* postfix-expression */
53 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
54 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
56 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
57 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
58 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
59 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
60 static void pp_c_and_expression (c_pretty_printer
*, tree
);
61 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
62 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
63 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
68 /* Helper functions. */
71 pp_c_whitespace (c_pretty_printer
*pp
)
74 pp
->padding
= pp_none
;
78 pp_c_left_paren (c_pretty_printer
*pp
)
81 pp
->padding
= pp_none
;
85 pp_c_right_paren (c_pretty_printer
*pp
)
88 pp
->padding
= pp_none
;
92 pp_c_left_brace (c_pretty_printer
*pp
)
95 pp
->padding
= pp_none
;
99 pp_c_right_brace (c_pretty_printer
*pp
)
102 pp
->padding
= pp_none
;
106 pp_c_left_bracket (c_pretty_printer
*pp
)
108 pp_left_bracket (pp
);
109 pp
->padding
= pp_none
;
113 pp_c_right_bracket (c_pretty_printer
*pp
)
115 pp_right_bracket (pp
);
116 pp
->padding
= pp_none
;
120 pp_c_dot (c_pretty_printer
*pp
)
123 pp
->padding
= pp_none
;
127 pp_c_ampersand (c_pretty_printer
*pp
)
130 pp
->padding
= pp_none
;
134 pp_c_star (c_pretty_printer
*pp
)
137 pp
->padding
= pp_none
;
141 pp_c_arrow (c_pretty_printer
*pp
)
144 pp
->padding
= pp_none
;
148 pp_c_semicolon (c_pretty_printer
*pp
)
151 pp
->padding
= pp_none
;
155 pp_c_complement (c_pretty_printer
*pp
)
158 pp
->padding
= pp_none
;
162 pp_c_exclamation (c_pretty_printer
*pp
)
165 pp
->padding
= pp_none
;
168 /* Print out the external representation of QUALIFIERS. */
171 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
173 const char *p
= pp_last_position_in_text (pp
);
174 bool previous
= false;
179 /* The C programming language does not have references, but it is much
180 simpler to handle those here rather than going through the same
181 logic in the C++ pretty-printer. */
182 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
183 pp_c_whitespace (pp
);
185 if (qualifiers
& TYPE_QUAL_ATOMIC
)
187 pp_c_ws_string (pp
, "_Atomic");
191 if (qualifiers
& TYPE_QUAL_CONST
)
194 pp_c_whitespace (pp
);
195 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
199 if (qualifiers
& TYPE_QUAL_VOLATILE
)
202 pp_c_whitespace (pp
);
203 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
207 if (qualifiers
& TYPE_QUAL_RESTRICT
)
210 pp_c_whitespace (pp
);
211 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
212 ? "restrict" : "__restrict__"));
216 /* Pretty-print T using the type-cast notation '( type-name )'. */
219 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
221 pp_c_left_paren (pp
);
223 pp_c_right_paren (pp
);
226 /* We're about to pretty-print a pointer type as indicated by T.
227 Output a whitespace, if needed, preparing for subsequent output. */
230 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
232 if (POINTER_TYPE_P (t
))
234 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
235 if (TREE_CODE (pointee
) != ARRAY_TYPE
236 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
237 pp_c_whitespace (pp
);
244 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
245 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
246 of its type. Take care of possible extensions.
250 type-qualifier-list type-qualifier
255 __restrict__ -- GNU C
256 address-space-qualifier -- GNU C
260 address-space-qualifier:
261 identifier -- GNU C */
264 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
268 if (!t
|| t
== error_mark_node
)
274 qualifiers
= TYPE_QUALS (t
);
275 pp_c_cv_qualifiers (pp
, qualifiers
,
276 TREE_CODE (t
) == FUNCTION_TYPE
);
278 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
280 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
281 pp_c_identifier (pp
, as
);
286 * type-qualifier-list(opt)
287 * type-qualifier-list(opt) pointer */
290 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
292 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
294 switch (TREE_CODE (t
))
297 /* It is easier to handle C++ reference types here. */
299 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
300 pp_c_pointer (pp
, TREE_TYPE (t
));
301 if (TREE_CODE (t
) == POINTER_TYPE
)
305 pp_c_type_qualifier_list (pp
, t
);
308 /* ??? This node is now in GENERIC and so shouldn't be here. But
309 we'll fix that later. */
311 pp
->declaration (DECL_EXPR_DECL (t
));
312 pp_needs_newline (pp
) = true;
316 pp_unsupported_tree (pp
, t
);
320 /* simple-type-specifier:
336 struct-or-union-specifier
341 simple-type-specifier:
346 c_pretty_printer::simple_type_specifier (tree t
)
348 const enum tree_code code
= TREE_CODE (t
);
352 translate_string ("<type-error>");
355 case IDENTIFIER_NODE
:
356 pp_c_identifier (this, IDENTIFIER_POINTER (t
));
363 case FIXED_POINT_TYPE
:
367 simple_type_specifier (t
);
371 int prec
= TYPE_PRECISION (t
);
372 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
373 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_SATURATING (t
));
375 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_UNSIGNED (t
));
378 simple_type_specifier (t
);
379 if (TYPE_PRECISION (t
) != prec
)
382 pp_decimal_int (this, prec
);
390 translate_string (TYPE_UNSIGNED (t
)
391 ? "<unnamed-unsigned:"
392 : "<unnamed-signed:");
395 translate_string ("<unnamed-float:");
397 case FIXED_POINT_TYPE
:
398 translate_string ("<unnamed-fixed:");
403 pp_decimal_int (this, prec
);
413 translate_string ("<typedef-error>");
419 if (code
== UNION_TYPE
)
420 pp_c_ws_string (this, "union");
421 else if (code
== RECORD_TYPE
)
422 pp_c_ws_string (this, "struct");
423 else if (code
== ENUMERAL_TYPE
)
424 pp_c_ws_string (this, "enum");
426 translate_string ("<tag-error>");
429 id_expression (TYPE_NAME (t
));
431 translate_string ("<anonymous>");
435 pp_unsupported_tree (this, t
);
440 /* specifier-qualifier-list:
441 type-specifier specifier-qualifier-list-opt
442 type-qualifier specifier-qualifier-list-opt
445 Implementation note: Because of the non-linearities in array or
446 function declarations, this routine prints not just the
447 specifier-qualifier-list of such entities or types of such entities,
448 but also the 'pointer' production part of their declarators. The
449 remaining part is done by declarator() or abstract_declarator(). */
452 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
454 const enum tree_code code
= TREE_CODE (t
);
456 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
457 pp_c_type_qualifier_list (pp
, t
);
463 /* Get the types-specifier of this type. */
464 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
465 pp_c_specifier_qualifier_list (pp
, pointee
);
466 if (TREE_CODE (pointee
) == ARRAY_TYPE
467 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
469 pp_c_whitespace (pp
);
470 pp_c_left_paren (pp
);
471 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
473 else if (!c_dialect_cxx ())
474 pp_c_whitespace (pp
);
475 pp_ptr_operator (pp
, t
);
481 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
486 if (code
== COMPLEX_TYPE
)
487 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
488 ? "_Complex" : "__complex__"));
489 else if (code
== VECTOR_TYPE
)
491 pp_c_ws_string (pp
, "__vector");
492 pp_c_left_paren (pp
);
493 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
494 pp_c_right_paren (pp
);
495 pp_c_whitespace (pp
);
497 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
501 pp
->simple_type_specifier (t
);
504 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
505 pp_c_type_qualifier_list (pp
, t
);
508 /* parameter-type-list:
513 parameter-declaration
514 parameter-list , parameter-declaration
516 parameter-declaration:
517 declaration-specifiers declarator
518 declaration-specifiers abstract-declarator(opt) */
521 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
523 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
524 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
525 pp_c_left_paren (pp
);
526 if (parms
== void_list_node
)
527 pp_c_ws_string (pp
, "void");
531 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
534 pp_separate_with (pp
, ',');
536 pp
->declaration_specifiers
537 (want_parm_decl
? parms
: TREE_VALUE (parms
));
539 pp
->declarator (parms
);
541 pp
->abstract_declarator (TREE_VALUE (parms
));
544 pp_c_right_paren (pp
);
547 /* abstract-declarator:
549 pointer(opt) direct-abstract-declarator */
552 c_pretty_printer::abstract_declarator (tree t
)
554 if (TREE_CODE (t
) == POINTER_TYPE
)
556 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
557 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
558 pp_c_right_paren (this);
562 direct_abstract_declarator (t
);
565 /* direct-abstract-declarator:
566 ( abstract-declarator )
567 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
568 direct-abstract-declarator(opt) [ * ]
569 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
572 c_pretty_printer::direct_abstract_declarator (tree t
)
574 switch (TREE_CODE (t
))
577 abstract_declarator (t
);
581 pp_c_parameter_type_list (this, t
);
582 direct_abstract_declarator (TREE_TYPE (t
));
586 pp_c_left_bracket (this);
587 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
589 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
590 tree type
= TREE_TYPE (maxval
);
592 if (tree_fits_shwi_p (maxval
))
593 pp_wide_integer (this, tree_to_shwi (maxval
) + 1);
595 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
596 build_int_cst (type
, 1)));
598 pp_c_right_bracket (this);
599 direct_abstract_declarator (TREE_TYPE (t
));
602 case IDENTIFIER_NODE
:
607 case FIXED_POINT_TYPE
:
617 pp_unsupported_tree (this, t
);
623 specifier-qualifier-list abstract-declarator(opt) */
626 c_pretty_printer::type_id (tree t
)
628 pp_c_specifier_qualifier_list (this, t
);
629 abstract_declarator (t
);
632 /* storage-class-specifier:
640 c_pretty_printer::storage_class_specifier (tree t
)
642 if (TREE_CODE (t
) == TYPE_DECL
)
643 pp_c_ws_string (this, "typedef");
646 if (DECL_REGISTER (t
))
647 pp_c_ws_string (this, "register");
648 else if (TREE_STATIC (t
) && TREE_CODE (t
) == VAR_DECL
)
649 pp_c_ws_string (this, "static");
653 /* function-specifier:
657 c_pretty_printer::function_specifier (tree t
)
659 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
660 pp_c_ws_string (this, "inline");
663 /* declaration-specifiers:
664 storage-class-specifier declaration-specifiers(opt)
665 type-specifier declaration-specifiers(opt)
666 type-qualifier declaration-specifiers(opt)
667 function-specifier declaration-specifiers(opt) */
670 c_pretty_printer::declaration_specifiers (tree t
)
672 storage_class_specifier (t
);
673 function_specifier (t
);
674 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
680 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
681 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
682 direct-declarator [ type-qualifier-list static assignment-expression ]
683 direct-declarator [ type-qualifier-list * ]
684 direct-declarator ( parameter-type-list )
685 direct-declarator ( identifier-list(opt) ) */
688 c_pretty_printer::direct_declarator (tree t
)
690 switch (TREE_CODE (t
))
697 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
698 pp_c_tree_decl_identifier (this, t
);
703 abstract_declarator (TREE_TYPE (t
));
707 pp_parameter_list (this, t
);
708 abstract_declarator (TREE_TYPE (t
));
712 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
713 pp_c_tree_decl_identifier (this, t
);
714 if (flags
& pp_c_flag_abstract
)
715 abstract_declarator (TREE_TYPE (t
));
718 pp_parameter_list (this, t
);
719 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
725 case FIXED_POINT_TYPE
:
732 pp_unsupported_tree (this, t
);
739 pointer(opt) direct-declarator */
742 c_pretty_printer::declarator (tree t
)
744 switch (TREE_CODE (t
))
748 case FIXED_POINT_TYPE
:
761 direct_declarator (t
);
766 pp_unsupported_tree (this, t
);
772 declaration-specifiers init-declarator-list(opt) ; */
775 c_pretty_printer::declaration (tree t
)
777 declaration_specifiers (t
);
778 pp_c_init_declarator (this, t
);
781 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
784 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
786 if (attributes
== NULL_TREE
)
789 pp_c_ws_string (pp
, "__attribute__");
790 pp_c_left_paren (pp
);
791 pp_c_left_paren (pp
);
792 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
794 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
795 if (TREE_VALUE (attributes
))
796 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
798 if (TREE_CHAIN (attributes
))
799 pp_separate_with (pp
, ',');
801 pp_c_right_paren (pp
);
802 pp_c_right_paren (pp
);
805 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
806 marked to be displayed on disgnostic. */
809 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
811 bool is_first
= true;
816 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
818 const struct attribute_spec
*as
;
819 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
820 if (!as
|| as
->affects_type_identity
== false)
824 pp_c_ws_string (pp
, "__attribute__");
825 pp_c_left_paren (pp
);
826 pp_c_left_paren (pp
);
831 pp_separate_with (pp
, ',');
833 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
835 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
840 pp_c_right_paren (pp
);
841 pp_c_right_paren (pp
);
842 pp_c_whitespace (pp
);
846 /* function-definition:
847 declaration-specifiers declarator compound-statement */
850 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
852 pp
->declaration_specifiers (t
);
854 pp_needs_newline (pp
) = true;
855 pp
->statement (DECL_SAVED_TREE (t
));
856 pp_newline_and_flush (pp
);
862 /* Print out a c-char. This is called solely for characters which are
863 in the *target* execution character set. We ought to convert them
864 back to the *host* execution character set before printing, but we
865 have no way to do this at present. A decent compromise is to print
866 all characters as if they were in the host execution character set,
867 and not attempt to recover any named escape characters, but render
868 all unprintables as octal escapes. If the host and target character
869 sets are the same, this produces relatively readable output. If they
870 are not the same, strings may appear as gibberish, but that's okay
871 (in fact, it may well be what the reader wants, e.g. if they are looking
872 to see if conversion to the target character set happened correctly).
874 A special case: we need to prefix \, ", and ' with backslashes. It is
875 correct to do so for the *host*'s \, ", and ', because the rest of the
876 file appears in the host character set. */
879 pp_c_char (c_pretty_printer
*pp
, int c
)
885 case '\\': pp_string (pp
, "\\\\"); break;
886 case '\'': pp_string (pp
, "\\\'"); break;
887 case '\"': pp_string (pp
, "\\\""); break;
888 default: pp_character (pp
, c
);
892 pp_scalar (pp
, "\\%03o", (unsigned) c
);
895 /* Print out a STRING literal. */
898 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
900 const char *p
= TREE_STRING_POINTER (s
);
901 int n
= TREE_STRING_LENGTH (s
) - 1;
904 for (i
= 0; i
< n
; ++i
)
905 pp_c_char (pp
, p
[i
]);
909 /* Pretty-print an INTEGER literal. */
912 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
914 /* We are going to compare the type of I to other types using
915 pointer comparison so we need to use its canonical type. */
917 TYPE_CANONICAL (TREE_TYPE (i
))
918 ? TYPE_CANONICAL (TREE_TYPE (i
))
921 if (tree_fits_shwi_p (i
))
922 pp_wide_integer (pp
, tree_to_shwi (i
));
923 else if (tree_fits_uhwi_p (i
))
924 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
929 if (wi::lt_p (i
, 0, TYPE_SIGN (TREE_TYPE (i
))))
934 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
935 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
937 if (TYPE_UNSIGNED (type
))
938 pp_character (pp
, 'u');
939 if (type
== long_integer_type_node
|| type
== long_unsigned_type_node
)
940 pp_character (pp
, 'l');
941 else if (type
== long_long_integer_type_node
942 || type
== long_long_unsigned_type_node
)
943 pp_string (pp
, "ll");
944 else if (type
== int128_integer_type_node
945 || type
== int128_unsigned_type_node
)
946 pp_string (pp
, "I128");
949 /* Print out a CHARACTER literal. */
952 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
955 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
959 /* Print out a BOOLEAN literal. */
962 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
964 if (b
== boolean_false_node
)
966 if (c_dialect_cxx ())
967 pp_c_ws_string (pp
, "false");
968 else if (flag_isoc99
)
969 pp_c_ws_string (pp
, "_False");
971 pp_unsupported_tree (pp
, b
);
973 else if (b
== boolean_true_node
)
975 if (c_dialect_cxx ())
976 pp_c_ws_string (pp
, "true");
977 else if (flag_isoc99
)
978 pp_c_ws_string (pp
, "_True");
980 pp_unsupported_tree (pp
, b
);
982 else if (TREE_CODE (b
) == INTEGER_CST
)
983 pp_c_integer_constant (pp
, b
);
985 pp_unsupported_tree (pp
, b
);
988 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
989 false; that means the value was obtained by a cast, in which case
990 print out the type-id part of the cast-expression -- the casted value
991 is then printed by pp_c_integer_literal. */
994 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
996 bool value_is_named
= true;
997 tree type
= TREE_TYPE (e
);
1000 /* Find the name of this constant. */
1001 for (value
= TYPE_VALUES (type
);
1002 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
1003 value
= TREE_CHAIN (value
))
1006 if (value
!= NULL_TREE
)
1007 pp
->id_expression (TREE_PURPOSE (value
));
1010 /* Value must have been cast. */
1011 pp_c_type_cast (pp
, type
);
1012 value_is_named
= false;
1015 return value_is_named
;
1018 /* Print out a REAL value as a decimal-floating-constant. */
1021 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1023 const struct real_format
*fmt
1024 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1026 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1027 bool is_decimal
= floating_cst
.decimal
;
1029 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1030 log10(2) to 7 significant digits. */
1031 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1033 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1034 sizeof (pp_buffer (pp
)->digit_buffer
),
1037 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1038 if (TREE_TYPE (r
) == float_type_node
)
1039 pp_character (pp
, 'f');
1040 else if (TREE_TYPE (r
) == long_double_type_node
)
1041 pp_character (pp
, 'l');
1042 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1043 pp_string (pp
, "dl");
1044 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1045 pp_string (pp
, "dd");
1046 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1047 pp_string (pp
, "df");
1050 /* Print out a FIXED value as a decimal-floating-constant. */
1053 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1055 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1056 sizeof (pp_buffer (pp
)->digit_buffer
));
1057 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1060 /* Pretty-print a compound literal expression. GNU extensions include
1061 vector constants. */
1064 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1066 tree type
= TREE_TYPE (e
);
1067 pp_c_type_cast (pp
, type
);
1069 switch (TREE_CODE (type
))
1076 pp_c_brace_enclosed_initializer_list (pp
, e
);
1080 pp_unsupported_tree (pp
, e
);
1085 /* Pretty-print a COMPLEX_EXPR expression. */
1088 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1090 /* Handle a few common special cases, otherwise fallback
1091 to printing it as compound literal. */
1092 tree type
= TREE_TYPE (e
);
1093 tree realexpr
= TREE_OPERAND (e
, 0);
1094 tree imagexpr
= TREE_OPERAND (e
, 1);
1096 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1097 if (TREE_CODE (realexpr
) == NOP_EXPR
1098 && TREE_CODE (imagexpr
) == NOP_EXPR
1099 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1100 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1101 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1102 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1103 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1104 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1106 pp_c_type_cast (pp
, type
);
1107 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1111 /* Cast of an scalar expression to COMPLEX_TYPE. */
1112 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1113 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1115 pp_c_type_cast (pp
, type
);
1116 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1117 realexpr
= TREE_OPERAND (realexpr
, 0);
1118 pp
->expression (realexpr
);
1122 pp_c_compound_literal (pp
, e
);
1128 fixed-point-constant
1129 enumeration-constant
1130 character-constant */
1133 c_pretty_printer::constant (tree e
)
1135 const enum tree_code code
= TREE_CODE (e
);
1141 tree type
= TREE_TYPE (e
);
1142 if (type
== boolean_type_node
)
1143 pp_c_bool_constant (this, e
);
1144 else if (type
== char_type_node
)
1145 pp_c_character_constant (this, e
);
1146 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1147 && pp_c_enumeration_constant (this, e
))
1150 pp_c_integer_constant (this, e
);
1155 pp_c_floating_constant (this, e
);
1159 pp_c_fixed_constant (this, e
);
1163 pp_c_string_literal (this, e
);
1167 /* Sometimes, we are confused and we think a complex literal
1168 is a constant. Such thing is a compound literal which
1169 grammatically belongs to postfix-expr production. */
1170 pp_c_compound_literal (this, e
);
1174 pp_unsupported_tree (this, e
);
1179 /* Pretty-print a string such as an identifier, without changing its
1180 encoding, preceded by whitespace is necessary. */
1183 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1185 pp_c_maybe_whitespace (pp
);
1186 pp_string (pp
, str
);
1187 pp
->padding
= pp_before
;
1191 c_pretty_printer::translate_string (const char *gmsgid
)
1193 if (pp_translate_identifiers (this))
1194 pp_c_ws_string (this, _(gmsgid
));
1196 pp_c_ws_string (this, gmsgid
);
1199 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1200 that need converting to the locale encoding, preceded by whitespace
1204 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1206 pp_c_maybe_whitespace (pp
);
1207 pp_identifier (pp
, id
);
1208 pp
->padding
= pp_before
;
1211 /* Pretty-print a C primary-expression.
1219 c_pretty_printer::primary_expression (tree e
)
1221 switch (TREE_CODE (e
))
1229 pp_c_tree_decl_identifier (this, e
);
1232 case IDENTIFIER_NODE
:
1233 pp_c_tree_identifier (this, e
);
1237 translate_string ("<erroneous-expression>");
1241 translate_string ("<return-value>");
1252 pp_c_ws_string (this, "__builtin_memcpy");
1253 pp_c_left_paren (this);
1254 pp_ampersand (this);
1255 primary_expression (TREE_OPERAND (e
, 0));
1256 pp_separate_with (this, ',');
1257 pp_ampersand (this);
1258 initializer (TREE_OPERAND (e
, 1));
1259 if (TREE_OPERAND (e
, 2))
1261 pp_separate_with (this, ',');
1262 expression (TREE_OPERAND (e
, 2));
1264 pp_c_right_paren (this);
1268 /* FIXME: Make sure we won't get into an infinite loop. */
1269 pp_c_left_paren (this);
1271 pp_c_right_paren (this);
1276 /* Print out a C initializer -- also support C compound-literals.
1278 assignment-expression:
1279 { initializer-list }
1280 { initializer-list , } */
1283 c_pretty_printer::initializer (tree e
)
1285 if (TREE_CODE (e
) == CONSTRUCTOR
)
1286 pp_c_brace_enclosed_initializer_list (this, e
);
1293 declarator = initializer */
1296 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1299 /* We don't want to output function definitions here. There are handled
1300 elsewhere (and the syntactic form is bogus anyway). */
1301 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1303 tree init
= DECL_INITIAL (t
);
1304 /* This C++ bit is handled here because it is easier to do so.
1305 In templates, the C++ parser builds a TREE_LIST for a
1306 direct-initialization; the TREE_PURPOSE is the variable to
1307 initialize and the TREE_VALUE is the initializer. */
1308 if (TREE_CODE (init
) == TREE_LIST
)
1310 pp_c_left_paren (pp
);
1311 pp
->expression (TREE_VALUE (init
));
1312 pp_right_paren (pp
);
1319 pp
->initializer (init
);
1324 /* initializer-list:
1325 designation(opt) initializer
1326 initializer-list , designation(opt) initializer
1333 designator-list designator
1336 [ constant-expression ]
1340 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1342 tree type
= TREE_TYPE (e
);
1343 const enum tree_code code
= TREE_CODE (type
);
1345 if (TREE_CODE (e
) == CONSTRUCTOR
)
1347 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1357 tree init
= TREE_OPERAND (e
, 0);
1358 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1360 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1363 pp
->primary_expression (TREE_PURPOSE (init
));
1367 pp_c_left_bracket (pp
);
1368 if (TREE_PURPOSE (init
))
1369 pp
->constant (TREE_PURPOSE (init
));
1370 pp_c_right_bracket (pp
);
1372 pp_c_whitespace (pp
);
1374 pp_c_whitespace (pp
);
1375 pp
->initializer (TREE_VALUE (init
));
1376 if (TREE_CHAIN (init
))
1377 pp_separate_with (pp
, ',');
1383 if (TREE_CODE (e
) == VECTOR_CST
)
1386 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1389 pp_separate_with (pp
, ',');
1390 pp
->expression (VECTOR_CST_ELT (e
, i
));
1398 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1400 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1401 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1402 pp_separate_with (pp
, ',');
1403 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1413 pp_unsupported_tree (pp
, type
);
1416 /* Pretty-print a brace-enclosed initializer-list. */
1419 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1421 pp_c_left_brace (pp
);
1422 pp_c_initializer_list (pp
, l
);
1423 pp_c_right_brace (pp
);
1427 /* This is a convenient function, used to bridge gap between C and C++
1434 c_pretty_printer::id_expression (tree t
)
1436 switch (TREE_CODE (t
))
1445 pp_c_tree_decl_identifier (this, t
);
1448 case IDENTIFIER_NODE
:
1449 pp_c_tree_identifier (this, t
);
1453 pp_unsupported_tree (this, t
);
1458 /* postfix-expression:
1460 postfix-expression [ expression ]
1461 postfix-expression ( argument-expression-list(opt) )
1462 postfix-expression . identifier
1463 postfix-expression -> identifier
1464 postfix-expression ++
1465 postfix-expression --
1466 ( type-name ) { initializer-list }
1467 ( type-name ) { initializer-list , } */
1470 c_pretty_printer::postfix_expression (tree e
)
1472 enum tree_code code
= TREE_CODE (e
);
1475 case POSTINCREMENT_EXPR
:
1476 case POSTDECREMENT_EXPR
:
1477 postfix_expression (TREE_OPERAND (e
, 0));
1478 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1482 postfix_expression (TREE_OPERAND (e
, 0));
1483 pp_c_left_bracket (this);
1484 expression (TREE_OPERAND (e
, 1));
1485 pp_c_right_bracket (this);
1488 case ARRAY_NOTATION_REF
:
1489 postfix_expression (ARRAY_NOTATION_ARRAY (e
));
1490 pp_c_left_bracket (this);
1491 expression (ARRAY_NOTATION_START (e
));
1493 expression (ARRAY_NOTATION_LENGTH (e
));
1495 expression (ARRAY_NOTATION_STRIDE (e
));
1496 pp_c_right_bracket (this);
1501 call_expr_arg_iterator iter
;
1503 postfix_expression (CALL_EXPR_FN (e
));
1504 pp_c_left_paren (this);
1505 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1508 if (more_call_expr_args_p (&iter
))
1509 pp_separate_with (this, ',');
1511 pp_c_right_paren (this);
1515 case UNORDERED_EXPR
:
1516 pp_c_ws_string (this, flag_isoc99
1518 : "__builtin_isunordered");
1522 pp_c_ws_string (this, flag_isoc99
1524 : "!__builtin_isunordered");
1528 pp_c_ws_string (this, flag_isoc99
1530 : "!__builtin_isgreaterequal");
1534 pp_c_ws_string (this, flag_isoc99
1536 : "!__builtin_isgreater");
1540 pp_c_ws_string (this, flag_isoc99
1542 : "!__builtin_islessequal");
1546 pp_c_ws_string (this, flag_isoc99
1548 : "!__builtin_isless");
1552 pp_c_ws_string (this, flag_isoc99
1554 : "!__builtin_islessgreater");
1558 pp_c_ws_string (this, flag_isoc99
1560 : "__builtin_islessgreater");
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 (TREE_CODE (object
) == INDIRECT_REF
)
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));
1660 /* else fall through. */
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 else if (code
== NEGATE_EXPR
)
1752 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1753 pp_complement (this);
1754 else if (code
== TRUTH_NOT_EXPR
)
1755 pp_exclamation (this);
1756 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1760 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1761 && integer_zerop (TREE_OPERAND (e
, 1)))
1762 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1766 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1768 pp_c_left_paren (this);
1769 if (!integer_onep (TYPE_SIZE_UNIT
1770 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1771 pp_c_type_cast (this, ptr_type_node
);
1773 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1774 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1777 pp_c_integer_constant (this,
1778 fold_convert (ssizetype
,
1779 TREE_OPERAND (e
, 1)));
1780 pp_c_right_paren (this);
1787 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1788 pp_c_whitespace (this);
1789 unary_expression (TREE_OPERAND (e
, 0));
1793 postfix_expression (e
);
1800 ( type-name ) cast-expression */
1803 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1805 switch (TREE_CODE (e
))
1808 case FIX_TRUNC_EXPR
:
1810 case VIEW_CONVERT_EXPR
:
1811 pp_c_type_cast (pp
, TREE_TYPE (e
));
1812 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1816 pp
->unary_expression (e
);
1820 /* multiplicative-expression:
1822 multiplicative-expression * cast-expression
1823 multiplicative-expression / cast-expression
1824 multiplicative-expression % cast-expression */
1827 c_pretty_printer::multiplicative_expression (tree e
)
1829 enum tree_code code
= TREE_CODE (e
);
1833 case TRUNC_DIV_EXPR
:
1834 case TRUNC_MOD_EXPR
:
1835 multiplicative_expression (TREE_OPERAND (e
, 0));
1836 pp_c_whitespace (this);
1837 if (code
== MULT_EXPR
)
1839 else if (code
== TRUNC_DIV_EXPR
)
1843 pp_c_whitespace (this);
1844 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1848 pp_c_cast_expression (this, e
);
1853 /* additive-expression:
1854 multiplicative-expression
1855 additive-expression + multiplicative-expression
1856 additive-expression - multiplicative-expression */
1859 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1861 enum tree_code code
= TREE_CODE (e
);
1864 case POINTER_PLUS_EXPR
:
1867 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1868 pp_c_whitespace (pp
);
1869 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1873 pp_c_whitespace (pp
);
1874 pp
->multiplicative_expression (TREE_OPERAND (e
, 1));
1878 pp
->multiplicative_expression (e
);
1883 /* additive-expression:
1885 shift-expression << additive-expression
1886 shift-expression >> additive-expression */
1889 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1891 enum tree_code code
= TREE_CODE (e
);
1896 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1897 pp_c_whitespace (pp
);
1898 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" : ">>");
1899 pp_c_whitespace (pp
);
1900 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1904 pp_c_additive_expression (pp
, e
);
1908 /* relational-expression:
1910 relational-expression < shift-expression
1911 relational-expression > shift-expression
1912 relational-expression <= shift-expression
1913 relational-expression >= shift-expression */
1916 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1918 enum tree_code code
= TREE_CODE (e
);
1925 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1926 pp_c_whitespace (pp
);
1927 if (code
== LT_EXPR
)
1929 else if (code
== GT_EXPR
)
1931 else if (code
== LE_EXPR
)
1933 else if (code
== GE_EXPR
)
1934 pp_greater_equal (pp
);
1935 pp_c_whitespace (pp
);
1936 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1940 pp_c_shift_expression (pp
, e
);
1945 /* equality-expression:
1946 relational-expression
1947 equality-expression == relational-expression
1948 equality-equality != relational-expression */
1951 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1953 enum tree_code code
= TREE_CODE (e
);
1958 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1959 pp_c_whitespace (pp
);
1960 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1961 pp_c_whitespace (pp
);
1962 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1966 pp_c_relational_expression (pp
, e
);
1973 AND-expression & equality-equality */
1976 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1978 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1980 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1981 pp_c_whitespace (pp
);
1983 pp_c_whitespace (pp
);
1984 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1987 pp_c_equality_expression (pp
, e
);
1990 /* exclusive-OR-expression:
1992 exclusive-OR-expression ^ AND-expression */
1995 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
1997 if (TREE_CODE (e
) == BIT_XOR_EXPR
1998 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2000 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2001 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2002 pp_c_maybe_whitespace (pp
);
2004 pp_c_whitespace (pp
);
2006 pp_c_whitespace (pp
);
2007 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2010 pp_c_and_expression (pp
, e
);
2013 /* inclusive-OR-expression:
2014 exclusive-OR-expression
2015 inclusive-OR-expression | exclusive-OR-expression */
2018 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2020 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2022 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2023 pp_c_whitespace (pp
);
2025 pp_c_whitespace (pp
);
2026 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2029 pp_c_exclusive_or_expression (pp
, e
);
2032 /* logical-AND-expression:
2033 inclusive-OR-expression
2034 logical-AND-expression && inclusive-OR-expression */
2037 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2039 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2040 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2042 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2043 pp_c_whitespace (pp
);
2044 pp_ampersand_ampersand (pp
);
2045 pp_c_whitespace (pp
);
2046 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2049 pp_c_inclusive_or_expression (pp
, e
);
2052 /* logical-OR-expression:
2053 logical-AND-expression
2054 logical-OR-expression || logical-AND-expression */
2057 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2059 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2060 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2062 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2063 pp_c_whitespace (pp
);
2065 pp_c_whitespace (pp
);
2066 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2069 pp_c_logical_and_expression (pp
, e
);
2072 /* conditional-expression:
2073 logical-OR-expression
2074 logical-OR-expression ? expression : conditional-expression */
2077 c_pretty_printer::conditional_expression (tree e
)
2079 if (TREE_CODE (e
) == COND_EXPR
)
2081 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2082 pp_c_whitespace (this);
2084 pp_c_whitespace (this);
2085 expression (TREE_OPERAND (e
, 1));
2086 pp_c_whitespace (this);
2088 pp_c_whitespace (this);
2089 conditional_expression (TREE_OPERAND (e
, 2));
2092 pp_c_logical_or_expression (this, e
);
2096 /* assignment-expression:
2097 conditional-expression
2098 unary-expression assignment-operator assignment-expression
2100 assignment-expression: one of
2101 = *= /= %= += -= >>= <<= &= ^= |= */
2104 c_pretty_printer::assignment_expression (tree e
)
2106 if (TREE_CODE (e
) == MODIFY_EXPR
2107 || TREE_CODE (e
) == INIT_EXPR
)
2109 unary_expression (TREE_OPERAND (e
, 0));
2110 pp_c_whitespace (this);
2113 expression (TREE_OPERAND (e
, 1));
2116 conditional_expression (e
);
2120 assignment-expression
2121 expression , assignment-expression
2123 Implementation note: instead of going through the usual recursion
2124 chain, I take the liberty of dispatching nodes to the appropriate
2125 functions. This makes some redundancy, but it worths it. That also
2126 prevents a possible infinite recursion between primary_expression ()
2127 and expression (). */
2130 c_pretty_printer::expression (tree e
)
2132 switch (TREE_CODE (e
))
2135 pp_c_integer_constant (this, e
);
2139 pp_c_floating_constant (this, e
);
2143 pp_c_fixed_constant (this, e
);
2147 pp_c_string_literal (this, e
);
2150 case IDENTIFIER_NODE
:
2159 primary_expression (e
);
2163 if (SSA_NAME_VAR (e
)
2164 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2165 expression (SSA_NAME_VAR (e
));
2167 translate_string ("<unknown>");
2170 case POSTINCREMENT_EXPR
:
2171 case POSTDECREMENT_EXPR
:
2173 case ARRAY_NOTATION_REF
:
2181 case UNORDERED_EXPR
:
2190 case COMPOUND_LITERAL_EXPR
:
2192 postfix_expression (e
);
2201 case TRUTH_NOT_EXPR
:
2202 case PREINCREMENT_EXPR
:
2203 case PREDECREMENT_EXPR
:
2206 unary_expression (e
);
2210 case FIX_TRUNC_EXPR
:
2212 case VIEW_CONVERT_EXPR
:
2213 pp_c_cast_expression (this, e
);
2217 case TRUNC_MOD_EXPR
:
2218 case TRUNC_DIV_EXPR
:
2219 multiplicative_expression (e
);
2224 pp_c_shift_expression (this, e
);
2231 pp_c_relational_expression (this, e
);
2235 pp_c_and_expression (this, e
);
2239 case TRUTH_XOR_EXPR
:
2240 pp_c_exclusive_or_expression (this, e
);
2244 pp_c_inclusive_or_expression (this, e
);
2247 case TRUTH_ANDIF_EXPR
:
2248 case TRUTH_AND_EXPR
:
2249 pp_c_logical_and_expression (this, e
);
2252 case TRUTH_ORIF_EXPR
:
2254 pp_c_logical_or_expression (this, e
);
2259 pp_c_equality_expression (this, e
);
2263 conditional_expression (e
);
2266 case POINTER_PLUS_EXPR
:
2269 pp_c_additive_expression (this, e
);
2274 assignment_expression (e
);
2278 pp_c_left_paren (this);
2279 expression (TREE_OPERAND (e
, 0));
2280 pp_separate_with (this, ',');
2281 assignment_expression (TREE_OPERAND (e
, 1));
2282 pp_c_right_paren (this);
2285 case NON_LVALUE_EXPR
:
2287 expression (TREE_OPERAND (e
, 0));
2291 postfix_expression (TREE_OPERAND (e
, 1));
2296 /* We don't yet have a way of dumping statements in a
2297 human-readable format. */
2298 pp_string (this, "({...})");
2301 case C_MAYBE_CONST_EXPR
:
2302 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2306 pp_unsupported_tree (this, e
);
2316 c_pretty_printer::statement (tree stmt
)
2321 if (pp_needs_newline (this))
2322 pp_newline_and_indent (this, 0);
2324 dump_generic_node (this, stmt
, pp_indentation (this), 0, true);
2328 /* Initialize the PRETTY-PRINTER for handling C codes. */
2330 c_pretty_printer::c_pretty_printer ()
2331 : pretty_printer (),
2335 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2336 ptr_operator
= pp_c_pointer
;
2337 parameter_list
= pp_c_parameter_type_list
;
2341 /* Print the tree T in full, on file FILE. */
2344 print_c_tree (FILE *file
, tree t
)
2346 c_pretty_printer pp
;
2348 pp_needs_newline (&pp
) = true;
2349 pp
.buffer
->stream
= file
;
2351 pp_newline_and_flush (&pp
);
2354 /* Print the tree T in full, on stderr. */
2357 debug_c_tree (tree t
)
2359 print_c_tree (stderr
, t
);
2360 fputc ('\n', stderr
);
2363 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2364 up of T's memory address. */
2367 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2371 gcc_assert (DECL_P (t
));
2374 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2377 static char xname
[8];
2378 sprintf (xname
, "<U%4x>", ((unsigned)((uintptr_t)(t
) & 0xffff)));
2382 pp_c_identifier (pp
, name
);