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 a VOID_CST (void_node). */
912 pp_c_void_constant (c_pretty_printer
*pp
)
914 pp_c_type_cast (pp
, void_type_node
);
918 /* Pretty-print an INTEGER literal. */
921 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
923 /* We are going to compare the type of I to other types using
924 pointer comparison so we need to use its canonical type. */
926 TYPE_CANONICAL (TREE_TYPE (i
))
927 ? TYPE_CANONICAL (TREE_TYPE (i
))
930 if (tree_fits_shwi_p (i
))
931 pp_wide_integer (pp
, tree_to_shwi (i
));
932 else if (tree_fits_uhwi_p (i
))
933 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
938 if (wi::lt_p (i
, 0, TYPE_SIGN (TREE_TYPE (i
))))
943 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
944 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
946 if (TYPE_UNSIGNED (type
))
947 pp_character (pp
, 'u');
948 if (type
== long_integer_type_node
|| type
== long_unsigned_type_node
)
949 pp_character (pp
, 'l');
950 else if (type
== long_long_integer_type_node
951 || type
== long_long_unsigned_type_node
)
952 pp_string (pp
, "ll");
953 else if (type
== int128_integer_type_node
954 || type
== int128_unsigned_type_node
)
955 pp_string (pp
, "I128");
958 /* Print out a CHARACTER literal. */
961 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
964 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
968 /* Print out a BOOLEAN literal. */
971 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
973 if (b
== boolean_false_node
)
975 if (c_dialect_cxx ())
976 pp_c_ws_string (pp
, "false");
977 else if (flag_isoc99
)
978 pp_c_ws_string (pp
, "_False");
980 pp_unsupported_tree (pp
, b
);
982 else if (b
== boolean_true_node
)
984 if (c_dialect_cxx ())
985 pp_c_ws_string (pp
, "true");
986 else if (flag_isoc99
)
987 pp_c_ws_string (pp
, "_True");
989 pp_unsupported_tree (pp
, b
);
991 else if (TREE_CODE (b
) == INTEGER_CST
)
992 pp_c_integer_constant (pp
, b
);
994 pp_unsupported_tree (pp
, b
);
997 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
998 false; that means the value was obtained by a cast, in which case
999 print out the type-id part of the cast-expression -- the casted value
1000 is then printed by pp_c_integer_literal. */
1003 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
1005 bool value_is_named
= true;
1006 tree type
= TREE_TYPE (e
);
1009 /* Find the name of this constant. */
1010 for (value
= TYPE_VALUES (type
);
1011 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
1012 value
= TREE_CHAIN (value
))
1015 if (value
!= NULL_TREE
)
1016 pp
->id_expression (TREE_PURPOSE (value
));
1019 /* Value must have been cast. */
1020 pp_c_type_cast (pp
, type
);
1021 value_is_named
= false;
1024 return value_is_named
;
1027 /* Print out a REAL value as a decimal-floating-constant. */
1030 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1032 const struct real_format
*fmt
1033 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1035 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1036 bool is_decimal
= floating_cst
.decimal
;
1038 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1039 log10(2) to 7 significant digits. */
1040 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1042 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1043 sizeof (pp_buffer (pp
)->digit_buffer
),
1046 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1047 if (TREE_TYPE (r
) == float_type_node
)
1048 pp_character (pp
, 'f');
1049 else if (TREE_TYPE (r
) == long_double_type_node
)
1050 pp_character (pp
, 'l');
1051 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1052 pp_string (pp
, "dl");
1053 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1054 pp_string (pp
, "dd");
1055 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1056 pp_string (pp
, "df");
1059 /* Print out a FIXED value as a decimal-floating-constant. */
1062 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1064 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1065 sizeof (pp_buffer (pp
)->digit_buffer
));
1066 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1069 /* Pretty-print a compound literal expression. GNU extensions include
1070 vector constants. */
1073 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1075 tree type
= TREE_TYPE (e
);
1076 pp_c_type_cast (pp
, type
);
1078 switch (TREE_CODE (type
))
1085 pp_c_brace_enclosed_initializer_list (pp
, e
);
1089 pp_unsupported_tree (pp
, e
);
1094 /* Pretty-print a COMPLEX_EXPR expression. */
1097 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1099 /* Handle a few common special cases, otherwise fallback
1100 to printing it as compound literal. */
1101 tree type
= TREE_TYPE (e
);
1102 tree realexpr
= TREE_OPERAND (e
, 0);
1103 tree imagexpr
= TREE_OPERAND (e
, 1);
1105 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1106 if (TREE_CODE (realexpr
) == NOP_EXPR
1107 && TREE_CODE (imagexpr
) == NOP_EXPR
1108 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1109 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1110 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1111 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1112 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1113 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1115 pp_c_type_cast (pp
, type
);
1116 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1120 /* Cast of an scalar expression to COMPLEX_TYPE. */
1121 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1122 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1124 pp_c_type_cast (pp
, type
);
1125 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1126 realexpr
= TREE_OPERAND (realexpr
, 0);
1127 pp
->expression (realexpr
);
1131 pp_c_compound_literal (pp
, e
);
1137 fixed-point-constant
1138 enumeration-constant
1139 character-constant */
1142 c_pretty_printer::constant (tree e
)
1144 const enum tree_code code
= TREE_CODE (e
);
1149 pp_c_void_constant (this);
1154 tree type
= TREE_TYPE (e
);
1155 if (type
== boolean_type_node
)
1156 pp_c_bool_constant (this, e
);
1157 else if (type
== char_type_node
)
1158 pp_c_character_constant (this, e
);
1159 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1160 && pp_c_enumeration_constant (this, e
))
1163 pp_c_integer_constant (this, e
);
1168 pp_c_floating_constant (this, e
);
1172 pp_c_fixed_constant (this, e
);
1176 pp_c_string_literal (this, e
);
1180 /* Sometimes, we are confused and we think a complex literal
1181 is a constant. Such thing is a compound literal which
1182 grammatically belongs to postfix-expr production. */
1183 pp_c_compound_literal (this, e
);
1187 pp_unsupported_tree (this, e
);
1192 /* Pretty-print a string such as an identifier, without changing its
1193 encoding, preceded by whitespace is necessary. */
1196 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1198 pp_c_maybe_whitespace (pp
);
1199 pp_string (pp
, str
);
1200 pp
->padding
= pp_before
;
1204 c_pretty_printer::translate_string (const char *gmsgid
)
1206 if (pp_translate_identifiers (this))
1207 pp_c_ws_string (this, _(gmsgid
));
1209 pp_c_ws_string (this, gmsgid
);
1212 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1213 that need converting to the locale encoding, preceded by whitespace
1217 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1219 pp_c_maybe_whitespace (pp
);
1220 pp_identifier (pp
, id
);
1221 pp
->padding
= pp_before
;
1224 /* Pretty-print a C primary-expression.
1232 c_pretty_printer::primary_expression (tree e
)
1234 switch (TREE_CODE (e
))
1242 pp_c_tree_decl_identifier (this, e
);
1245 case IDENTIFIER_NODE
:
1246 pp_c_tree_identifier (this, e
);
1250 translate_string ("<erroneous-expression>");
1254 translate_string ("<return-value>");
1266 pp_c_ws_string (this, "__builtin_memcpy");
1267 pp_c_left_paren (this);
1268 pp_ampersand (this);
1269 primary_expression (TREE_OPERAND (e
, 0));
1270 pp_separate_with (this, ',');
1271 pp_ampersand (this);
1272 initializer (TREE_OPERAND (e
, 1));
1273 if (TREE_OPERAND (e
, 2))
1275 pp_separate_with (this, ',');
1276 expression (TREE_OPERAND (e
, 2));
1278 pp_c_right_paren (this);
1282 /* FIXME: Make sure we won't get into an infinite loop. */
1283 pp_c_left_paren (this);
1285 pp_c_right_paren (this);
1290 /* Print out a C initializer -- also support C compound-literals.
1292 assignment-expression:
1293 { initializer-list }
1294 { initializer-list , } */
1297 c_pretty_printer::initializer (tree e
)
1299 if (TREE_CODE (e
) == CONSTRUCTOR
)
1300 pp_c_brace_enclosed_initializer_list (this, e
);
1307 declarator = initializer */
1310 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1313 /* We don't want to output function definitions here. There are handled
1314 elsewhere (and the syntactic form is bogus anyway). */
1315 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1317 tree init
= DECL_INITIAL (t
);
1318 /* This C++ bit is handled here because it is easier to do so.
1319 In templates, the C++ parser builds a TREE_LIST for a
1320 direct-initialization; the TREE_PURPOSE is the variable to
1321 initialize and the TREE_VALUE is the initializer. */
1322 if (TREE_CODE (init
) == TREE_LIST
)
1324 pp_c_left_paren (pp
);
1325 pp
->expression (TREE_VALUE (init
));
1326 pp_right_paren (pp
);
1333 pp
->initializer (init
);
1338 /* initializer-list:
1339 designation(opt) initializer
1340 initializer-list , designation(opt) initializer
1347 designator-list designator
1350 [ constant-expression ]
1354 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1356 tree type
= TREE_TYPE (e
);
1357 const enum tree_code code
= TREE_CODE (type
);
1359 if (TREE_CODE (e
) == CONSTRUCTOR
)
1361 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1371 tree init
= TREE_OPERAND (e
, 0);
1372 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1374 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1377 pp
->primary_expression (TREE_PURPOSE (init
));
1381 pp_c_left_bracket (pp
);
1382 if (TREE_PURPOSE (init
))
1383 pp
->constant (TREE_PURPOSE (init
));
1384 pp_c_right_bracket (pp
);
1386 pp_c_whitespace (pp
);
1388 pp_c_whitespace (pp
);
1389 pp
->initializer (TREE_VALUE (init
));
1390 if (TREE_CHAIN (init
))
1391 pp_separate_with (pp
, ',');
1397 if (TREE_CODE (e
) == VECTOR_CST
)
1400 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1403 pp_separate_with (pp
, ',');
1404 pp
->expression (VECTOR_CST_ELT (e
, i
));
1412 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1414 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1415 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1416 pp_separate_with (pp
, ',');
1417 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1427 pp_unsupported_tree (pp
, type
);
1430 /* Pretty-print a brace-enclosed initializer-list. */
1433 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1435 pp_c_left_brace (pp
);
1436 pp_c_initializer_list (pp
, l
);
1437 pp_c_right_brace (pp
);
1441 /* This is a convenient function, used to bridge gap between C and C++
1448 c_pretty_printer::id_expression (tree t
)
1450 switch (TREE_CODE (t
))
1459 pp_c_tree_decl_identifier (this, t
);
1462 case IDENTIFIER_NODE
:
1463 pp_c_tree_identifier (this, t
);
1467 pp_unsupported_tree (this, t
);
1472 /* postfix-expression:
1474 postfix-expression [ expression ]
1475 postfix-expression ( argument-expression-list(opt) )
1476 postfix-expression . identifier
1477 postfix-expression -> identifier
1478 postfix-expression ++
1479 postfix-expression --
1480 ( type-name ) { initializer-list }
1481 ( type-name ) { initializer-list , } */
1484 c_pretty_printer::postfix_expression (tree e
)
1486 enum tree_code code
= TREE_CODE (e
);
1489 case POSTINCREMENT_EXPR
:
1490 case POSTDECREMENT_EXPR
:
1491 postfix_expression (TREE_OPERAND (e
, 0));
1492 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1496 postfix_expression (TREE_OPERAND (e
, 0));
1497 pp_c_left_bracket (this);
1498 expression (TREE_OPERAND (e
, 1));
1499 pp_c_right_bracket (this);
1502 case ARRAY_NOTATION_REF
:
1503 postfix_expression (ARRAY_NOTATION_ARRAY (e
));
1504 pp_c_left_bracket (this);
1505 expression (ARRAY_NOTATION_START (e
));
1507 expression (ARRAY_NOTATION_LENGTH (e
));
1509 expression (ARRAY_NOTATION_STRIDE (e
));
1510 pp_c_right_bracket (this);
1515 call_expr_arg_iterator iter
;
1517 postfix_expression (CALL_EXPR_FN (e
));
1518 pp_c_left_paren (this);
1519 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1522 if (more_call_expr_args_p (&iter
))
1523 pp_separate_with (this, ',');
1525 pp_c_right_paren (this);
1529 case UNORDERED_EXPR
:
1530 pp_c_ws_string (this, flag_isoc99
1532 : "__builtin_isunordered");
1536 pp_c_ws_string (this, flag_isoc99
1538 : "!__builtin_isunordered");
1542 pp_c_ws_string (this, flag_isoc99
1544 : "!__builtin_isgreaterequal");
1548 pp_c_ws_string (this, flag_isoc99
1550 : "!__builtin_isgreater");
1554 pp_c_ws_string (this, flag_isoc99
1556 : "!__builtin_islessequal");
1560 pp_c_ws_string (this, flag_isoc99
1562 : "!__builtin_isless");
1566 pp_c_ws_string (this, flag_isoc99
1568 : "!__builtin_islessgreater");
1572 pp_c_ws_string (this, flag_isoc99
1574 : "__builtin_islessgreater");
1578 pp_c_left_paren (this);
1579 expression (TREE_OPERAND (e
, 0));
1580 pp_separate_with (this, ',');
1581 expression (TREE_OPERAND (e
, 1));
1582 pp_c_right_paren (this);
1586 pp_c_ws_string (this, "__builtin_abs");
1587 pp_c_left_paren (this);
1588 expression (TREE_OPERAND (e
, 0));
1589 pp_c_right_paren (this);
1594 tree object
= TREE_OPERAND (e
, 0);
1595 if (TREE_CODE (object
) == INDIRECT_REF
)
1597 postfix_expression (TREE_OPERAND (object
, 0));
1602 postfix_expression (object
);
1605 expression (TREE_OPERAND (e
, 1));
1611 tree type
= TREE_TYPE (e
);
1613 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1615 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1617 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1618 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1619 if ((bitpos
% size
) == 0)
1621 pp_c_left_paren (this);
1622 pp_c_left_paren (this);
1625 pp_c_right_paren (this);
1626 pp_c_ampersand (this);
1627 expression (TREE_OPERAND (e
, 0));
1628 pp_c_right_paren (this);
1629 pp_c_left_bracket (this);
1630 pp_wide_integer (this, bitpos
/ size
);
1631 pp_c_right_bracket (this);
1635 pp_unsupported_tree (this, e
);
1645 pp_c_compound_literal (this, e
);
1649 pp_c_complex_expr (this, e
);
1652 case COMPOUND_LITERAL_EXPR
:
1653 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1660 pp_c_ws_string (this, "__builtin_va_arg");
1661 pp_c_left_paren (this);
1662 assignment_expression (TREE_OPERAND (e
, 0));
1663 pp_separate_with (this, ',');
1664 type_id (TREE_TYPE (e
));
1665 pp_c_right_paren (this);
1669 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1671 id_expression (TREE_OPERAND (e
, 0));
1674 /* else fall through. */
1677 primary_expression (e
);
1682 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1685 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1687 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1689 pp
->expression (TREE_VALUE (e
));
1691 pp_separate_with (pp
, ',');
1695 /* Print out V, which contains the elements of a constructor. */
1698 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1700 unsigned HOST_WIDE_INT ix
;
1703 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1705 pp
->expression (value
);
1706 if (ix
!= vec_safe_length (v
) - 1)
1707 pp_separate_with (pp
, ',');
1711 /* Print out an expression-list in parens, as if it were the argument
1712 list to a function. */
1715 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1717 pp_c_left_paren (pp
);
1718 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1719 pp_c_expression_list (pp
, t
);
1720 pp_c_right_paren (pp
);
1723 /* unary-expression:
1727 unary-operator cast-expression
1728 sizeof unary-expression
1731 unary-operator: one of
1736 __alignof__ unary-expression
1737 __alignof__ ( type-id )
1738 __real__ unary-expression
1739 __imag__ unary-expression */
1742 c_pretty_printer::unary_expression (tree e
)
1744 enum tree_code code
= TREE_CODE (e
);
1747 case PREINCREMENT_EXPR
:
1748 case PREDECREMENT_EXPR
:
1749 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1750 unary_expression (TREE_OPERAND (e
, 0));
1757 case TRUTH_NOT_EXPR
:
1759 /* String literal are used by address. */
1760 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1761 pp_ampersand (this);
1762 else if (code
== INDIRECT_REF
)
1764 else if (code
== NEGATE_EXPR
)
1766 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1767 pp_complement (this);
1768 else if (code
== TRUTH_NOT_EXPR
)
1769 pp_exclamation (this);
1770 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1774 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1775 && integer_zerop (TREE_OPERAND (e
, 1)))
1776 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1780 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1782 pp_c_left_paren (this);
1783 if (!integer_onep (TYPE_SIZE_UNIT
1784 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1785 pp_c_type_cast (this, ptr_type_node
);
1787 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1788 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1791 pp_c_integer_constant (this,
1792 fold_convert (ssizetype
,
1793 TREE_OPERAND (e
, 1)));
1794 pp_c_right_paren (this);
1801 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1802 pp_c_whitespace (this);
1803 unary_expression (TREE_OPERAND (e
, 0));
1807 postfix_expression (e
);
1814 ( type-name ) cast-expression */
1817 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1819 switch (TREE_CODE (e
))
1822 case FIX_TRUNC_EXPR
:
1824 case VIEW_CONVERT_EXPR
:
1825 pp_c_type_cast (pp
, TREE_TYPE (e
));
1826 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1830 pp
->unary_expression (e
);
1834 /* multiplicative-expression:
1836 multiplicative-expression * cast-expression
1837 multiplicative-expression / cast-expression
1838 multiplicative-expression % cast-expression */
1841 c_pretty_printer::multiplicative_expression (tree e
)
1843 enum tree_code code
= TREE_CODE (e
);
1847 case TRUNC_DIV_EXPR
:
1848 case TRUNC_MOD_EXPR
:
1849 multiplicative_expression (TREE_OPERAND (e
, 0));
1850 pp_c_whitespace (this);
1851 if (code
== MULT_EXPR
)
1853 else if (code
== TRUNC_DIV_EXPR
)
1857 pp_c_whitespace (this);
1858 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1862 pp_c_cast_expression (this, e
);
1867 /* additive-expression:
1868 multiplicative-expression
1869 additive-expression + multiplicative-expression
1870 additive-expression - multiplicative-expression */
1873 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1875 enum tree_code code
= TREE_CODE (e
);
1878 case POINTER_PLUS_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
);
1910 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1911 pp_c_whitespace (pp
);
1912 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" : ">>");
1913 pp_c_whitespace (pp
);
1914 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1918 pp_c_additive_expression (pp
, e
);
1922 /* relational-expression:
1924 relational-expression < shift-expression
1925 relational-expression > shift-expression
1926 relational-expression <= shift-expression
1927 relational-expression >= shift-expression */
1930 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1932 enum tree_code code
= TREE_CODE (e
);
1939 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1940 pp_c_whitespace (pp
);
1941 if (code
== LT_EXPR
)
1943 else if (code
== GT_EXPR
)
1945 else if (code
== LE_EXPR
)
1947 else if (code
== GE_EXPR
)
1948 pp_greater_equal (pp
);
1949 pp_c_whitespace (pp
);
1950 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1954 pp_c_shift_expression (pp
, e
);
1959 /* equality-expression:
1960 relational-expression
1961 equality-expression == relational-expression
1962 equality-equality != relational-expression */
1965 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1967 enum tree_code code
= TREE_CODE (e
);
1972 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1973 pp_c_whitespace (pp
);
1974 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1975 pp_c_whitespace (pp
);
1976 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1980 pp_c_relational_expression (pp
, e
);
1987 AND-expression & equality-equality */
1990 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1992 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1994 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1995 pp_c_whitespace (pp
);
1997 pp_c_whitespace (pp
);
1998 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
2001 pp_c_equality_expression (pp
, e
);
2004 /* exclusive-OR-expression:
2006 exclusive-OR-expression ^ AND-expression */
2009 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2011 if (TREE_CODE (e
) == BIT_XOR_EXPR
2012 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2014 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2015 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2016 pp_c_maybe_whitespace (pp
);
2018 pp_c_whitespace (pp
);
2020 pp_c_whitespace (pp
);
2021 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2024 pp_c_and_expression (pp
, e
);
2027 /* inclusive-OR-expression:
2028 exclusive-OR-expression
2029 inclusive-OR-expression | exclusive-OR-expression */
2032 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2034 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2036 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2037 pp_c_whitespace (pp
);
2039 pp_c_whitespace (pp
);
2040 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2043 pp_c_exclusive_or_expression (pp
, e
);
2046 /* logical-AND-expression:
2047 inclusive-OR-expression
2048 logical-AND-expression && inclusive-OR-expression */
2051 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2053 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2054 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2056 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2057 pp_c_whitespace (pp
);
2058 pp_ampersand_ampersand (pp
);
2059 pp_c_whitespace (pp
);
2060 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2063 pp_c_inclusive_or_expression (pp
, e
);
2066 /* logical-OR-expression:
2067 logical-AND-expression
2068 logical-OR-expression || logical-AND-expression */
2071 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2073 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2074 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2076 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2077 pp_c_whitespace (pp
);
2079 pp_c_whitespace (pp
);
2080 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2083 pp_c_logical_and_expression (pp
, e
);
2086 /* conditional-expression:
2087 logical-OR-expression
2088 logical-OR-expression ? expression : conditional-expression */
2091 c_pretty_printer::conditional_expression (tree e
)
2093 if (TREE_CODE (e
) == COND_EXPR
)
2095 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2096 pp_c_whitespace (this);
2098 pp_c_whitespace (this);
2099 expression (TREE_OPERAND (e
, 1));
2100 pp_c_whitespace (this);
2102 pp_c_whitespace (this);
2103 conditional_expression (TREE_OPERAND (e
, 2));
2106 pp_c_logical_or_expression (this, e
);
2110 /* assignment-expression:
2111 conditional-expression
2112 unary-expression assignment-operator assignment-expression
2114 assignment-expression: one of
2115 = *= /= %= += -= >>= <<= &= ^= |= */
2118 c_pretty_printer::assignment_expression (tree e
)
2120 if (TREE_CODE (e
) == MODIFY_EXPR
2121 || TREE_CODE (e
) == INIT_EXPR
)
2123 unary_expression (TREE_OPERAND (e
, 0));
2124 pp_c_whitespace (this);
2127 expression (TREE_OPERAND (e
, 1));
2130 conditional_expression (e
);
2134 assignment-expression
2135 expression , assignment-expression
2137 Implementation note: instead of going through the usual recursion
2138 chain, I take the liberty of dispatching nodes to the appropriate
2139 functions. This makes some redundancy, but it worths it. That also
2140 prevents a possible infinite recursion between primary_expression ()
2141 and expression (). */
2144 c_pretty_printer::expression (tree e
)
2146 switch (TREE_CODE (e
))
2149 pp_c_void_constant (this);
2153 pp_c_integer_constant (this, e
);
2157 pp_c_floating_constant (this, e
);
2161 pp_c_fixed_constant (this, e
);
2165 pp_c_string_literal (this, e
);
2168 case IDENTIFIER_NODE
:
2177 primary_expression (e
);
2181 if (SSA_NAME_VAR (e
)
2182 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2183 expression (SSA_NAME_VAR (e
));
2185 translate_string ("<unknown>");
2188 case POSTINCREMENT_EXPR
:
2189 case POSTDECREMENT_EXPR
:
2191 case ARRAY_NOTATION_REF
:
2199 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 multiplicative_expression (e
);
2242 pp_c_shift_expression (this, e
);
2249 pp_c_relational_expression (this, e
);
2253 pp_c_and_expression (this, e
);
2257 case TRUTH_XOR_EXPR
:
2258 pp_c_exclusive_or_expression (this, e
);
2262 pp_c_inclusive_or_expression (this, e
);
2265 case TRUTH_ANDIF_EXPR
:
2266 case TRUTH_AND_EXPR
:
2267 pp_c_logical_and_expression (this, e
);
2270 case TRUTH_ORIF_EXPR
:
2272 pp_c_logical_or_expression (this, e
);
2277 pp_c_equality_expression (this, e
);
2281 conditional_expression (e
);
2284 case POINTER_PLUS_EXPR
:
2287 pp_c_additive_expression (this, e
);
2292 assignment_expression (e
);
2296 pp_c_left_paren (this);
2297 expression (TREE_OPERAND (e
, 0));
2298 pp_separate_with (this, ',');
2299 assignment_expression (TREE_OPERAND (e
, 1));
2300 pp_c_right_paren (this);
2303 case NON_LVALUE_EXPR
:
2305 expression (TREE_OPERAND (e
, 0));
2309 postfix_expression (TREE_OPERAND (e
, 1));
2314 /* We don't yet have a way of dumping statements in a
2315 human-readable format. */
2316 pp_string (this, "({...})");
2319 case C_MAYBE_CONST_EXPR
:
2320 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2324 pp_unsupported_tree (this, e
);
2334 c_pretty_printer::statement (tree stmt
)
2339 if (pp_needs_newline (this))
2340 pp_newline_and_indent (this, 0);
2342 dump_generic_node (this, stmt
, pp_indentation (this), 0, true);
2346 /* Initialize the PRETTY-PRINTER for handling C codes. */
2348 c_pretty_printer::c_pretty_printer ()
2349 : pretty_printer (),
2353 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2354 ptr_operator
= pp_c_pointer
;
2355 parameter_list
= pp_c_parameter_type_list
;
2359 /* Print the tree T in full, on file FILE. */
2362 print_c_tree (FILE *file
, tree t
)
2364 c_pretty_printer pp
;
2366 pp_needs_newline (&pp
) = true;
2367 pp
.buffer
->stream
= file
;
2369 pp_newline_and_flush (&pp
);
2372 /* Print the tree T in full, on stderr. */
2375 debug_c_tree (tree t
)
2377 print_c_tree (stderr
, t
);
2378 fputc ('\n', stderr
);
2381 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2382 up of T's memory address. */
2385 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2389 gcc_assert (DECL_P (t
));
2392 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2395 static char xname
[8];
2396 sprintf (xname
, "<U%4x>", ((unsigned)((uintptr_t)(t
) & 0xffff)));
2400 pp_c_identifier (pp
, name
);