1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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"
27 #include "c-pretty-print.h"
28 #include "tree-pretty-print.h"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
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
);
171 bool previous
= false;
176 /* The C programming language does not have references, but it is much
177 simpler to handle those here rather than going through the same
178 logic in the C++ pretty-printer. */
179 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
180 pp_c_whitespace (pp
);
182 if (qualifiers
& TYPE_QUAL_ATOMIC
)
184 pp_c_ws_string (pp
, "_Atomic");
188 if (qualifiers
& TYPE_QUAL_CONST
)
191 pp_c_whitespace (pp
);
192 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
196 if (qualifiers
& TYPE_QUAL_VOLATILE
)
199 pp_c_whitespace (pp
);
200 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
204 if (qualifiers
& TYPE_QUAL_RESTRICT
)
207 pp_c_whitespace (pp
);
208 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
209 ? "restrict" : "__restrict__"));
213 if (qualifiers
& TYPE_QUAL_RELAXED
)
216 pp_c_whitespace (pp
);
217 pp_c_ws_string (pp
, "relaxed");
221 if (qualifiers
& TYPE_QUAL_STRICT
)
224 pp_c_whitespace (pp
);
225 pp_c_ws_string (pp
, "strict");
229 if (qualifiers
& TYPE_QUAL_SHARED
)
232 pp_c_whitespace (pp
);
233 pp_c_ws_string (pp
, "shared");
239 /* Pretty-print T using the type-cast notation '( type-name )'. */
242 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
244 pp_c_left_paren (pp
);
246 pp_c_right_paren (pp
);
249 /* We're about to pretty-print a pointer type as indicated by T.
250 Output a whitespace, if needed, preparing for subsequent output. */
253 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
255 if (POINTER_TYPE_P (t
))
257 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
258 if (TREE_CODE (pointee
) != ARRAY_TYPE
259 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
260 pp_c_whitespace (pp
);
267 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
268 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
269 of its type. Take care of possible extensions.
273 type-qualifier-list type-qualifier
278 __restrict__ -- GNU C
279 address-space-qualifier -- GNU C
283 address-space-qualifier:
284 identifier -- GNU C */
287 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
291 if (!t
|| t
== error_mark_node
)
297 qualifiers
= TYPE_QUALS (t
);
298 pp_c_cv_qualifiers (pp
, qualifiers
,
299 TREE_CODE (t
) == FUNCTION_TYPE
);
301 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
303 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
304 pp_c_identifier (pp
, as
);
309 * type-qualifier-list(opt)
310 * type-qualifier-list(opt) pointer */
313 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
315 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
317 switch (TREE_CODE (t
))
320 /* It is easier to handle C++ reference types here. */
322 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
323 pp_c_pointer (pp
, TREE_TYPE (t
));
324 if (TREE_CODE (t
) == POINTER_TYPE
)
328 pp_c_type_qualifier_list (pp
, t
);
331 /* ??? This node is now in GENERIC and so shouldn't be here. But
332 we'll fix that later. */
334 pp
->declaration (DECL_EXPR_DECL (t
));
335 pp_needs_newline (pp
) = true;
339 pp_unsupported_tree (pp
, t
);
343 /* simple-type-specifier:
359 struct-or-union-specifier
364 simple-type-specifier:
369 c_pretty_printer::simple_type_specifier (tree t
)
371 const enum tree_code code
= TREE_CODE (t
);
375 translate_string ("<type-error>");
378 case IDENTIFIER_NODE
:
379 pp_c_identifier (this, IDENTIFIER_POINTER (t
));
386 case FIXED_POINT_TYPE
:
390 simple_type_specifier (t
);
394 int prec
= TYPE_PRECISION (t
);
395 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
396 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_SATURATING (t
));
398 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_UNSIGNED (t
));
401 simple_type_specifier (t
);
402 if (TYPE_PRECISION (t
) != prec
)
405 pp_decimal_int (this, prec
);
413 translate_string (TYPE_UNSIGNED (t
)
414 ? "<unnamed-unsigned:"
415 : "<unnamed-signed:");
418 translate_string ("<unnamed-float:");
420 case FIXED_POINT_TYPE
:
421 translate_string ("<unnamed-fixed:");
426 pp_decimal_int (this, prec
);
436 translate_string ("<typedef-error>");
442 if (code
== UNION_TYPE
)
443 pp_c_ws_string (this, "union");
444 else if (code
== RECORD_TYPE
)
445 pp_c_ws_string (this, "struct");
446 else if (code
== ENUMERAL_TYPE
)
447 pp_c_ws_string (this, "enum");
449 translate_string ("<tag-error>");
452 id_expression (TYPE_NAME (t
));
454 translate_string ("<anonymous>");
458 pp_unsupported_tree (this, t
);
463 /* specifier-qualifier-list:
464 type-specifier specifier-qualifier-list-opt
465 type-qualifier specifier-qualifier-list-opt
468 Implementation note: Because of the non-linearities in array or
469 function declarations, this routine prints not just the
470 specifier-qualifier-list of such entities or types of such entities,
471 but also the 'pointer' production part of their declarators. The
472 remaining part is done by declarator() or abstract_declarator(). */
475 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
477 const enum tree_code code
= TREE_CODE (t
);
479 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
480 pp_c_type_qualifier_list (pp
, t
);
486 /* Get the types-specifier of this type. */
487 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
488 pp_c_specifier_qualifier_list (pp
, pointee
);
489 if (TREE_CODE (pointee
) == ARRAY_TYPE
490 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
492 pp_c_whitespace (pp
);
493 pp_c_left_paren (pp
);
494 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
496 else if (!c_dialect_cxx ())
497 pp_c_whitespace (pp
);
498 pp_ptr_operator (pp
, t
);
504 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
509 if (code
== COMPLEX_TYPE
)
510 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
511 ? "_Complex" : "__complex__"));
512 else if (code
== VECTOR_TYPE
)
514 pp_c_ws_string (pp
, "__vector");
515 pp_c_left_paren (pp
);
516 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
517 pp_c_right_paren (pp
);
518 pp_c_whitespace (pp
);
520 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
524 pp
->simple_type_specifier (t
);
527 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
528 pp_c_type_qualifier_list (pp
, t
);
531 /* parameter-type-list:
536 parameter-declaration
537 parameter-list , parameter-declaration
539 parameter-declaration:
540 declaration-specifiers declarator
541 declaration-specifiers abstract-declarator(opt) */
544 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
546 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
547 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
548 pp_c_left_paren (pp
);
549 if (parms
== void_list_node
)
550 pp_c_ws_string (pp
, "void");
554 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
557 pp_separate_with (pp
, ',');
559 pp
->declaration_specifiers
560 (want_parm_decl
? parms
: TREE_VALUE (parms
));
562 pp
->declarator (parms
);
564 pp
->abstract_declarator (TREE_VALUE (parms
));
567 pp_c_right_paren (pp
);
570 /* abstract-declarator:
572 pointer(opt) direct-abstract-declarator */
575 c_pretty_printer::abstract_declarator (tree t
)
577 if (TREE_CODE (t
) == POINTER_TYPE
)
579 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
580 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
581 pp_c_right_paren (this);
585 direct_abstract_declarator (t
);
588 /* direct-abstract-declarator:
589 ( abstract-declarator )
590 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
591 direct-abstract-declarator(opt) [ * ]
592 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
595 c_pretty_printer::direct_abstract_declarator (tree t
)
597 switch (TREE_CODE (t
))
600 abstract_declarator (t
);
604 pp_c_parameter_type_list (this, t
);
605 direct_abstract_declarator (TREE_TYPE (t
));
609 pp_c_left_bracket (this);
610 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
612 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
613 tree type
= TREE_TYPE (maxval
);
615 if (host_integerp (maxval
, 0))
616 pp_wide_integer (this, tree_low_cst (maxval
, 0) + 1);
618 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
619 build_int_cst (type
, 1)));
621 pp_c_right_bracket (this);
622 direct_abstract_declarator (TREE_TYPE (t
));
625 case IDENTIFIER_NODE
:
630 case FIXED_POINT_TYPE
:
640 pp_unsupported_tree (this, t
);
646 specifier-qualifier-list abstract-declarator(opt) */
649 c_pretty_printer::type_id (tree t
)
651 pp_c_specifier_qualifier_list (this, t
);
652 abstract_declarator (t
);
655 /* storage-class-specifier:
663 c_pretty_printer::storage_class_specifier (tree t
)
665 if (TREE_CODE (t
) == TYPE_DECL
)
666 pp_c_ws_string (this, "typedef");
669 if (DECL_REGISTER (t
))
670 pp_c_ws_string (this, "register");
671 else if (TREE_STATIC (t
) && TREE_CODE (t
) == VAR_DECL
)
672 pp_c_ws_string (this, "static");
676 /* function-specifier:
680 c_pretty_printer::function_specifier (tree t
)
682 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
683 pp_c_ws_string (this, "inline");
686 /* declaration-specifiers:
687 storage-class-specifier declaration-specifiers(opt)
688 type-specifier declaration-specifiers(opt)
689 type-qualifier declaration-specifiers(opt)
690 function-specifier declaration-specifiers(opt) */
693 c_pretty_printer::declaration_specifiers (tree t
)
695 storage_class_specifier (t
);
696 function_specifier (t
);
697 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
703 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
704 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
705 direct-declarator [ type-qualifier-list static assignment-expression ]
706 direct-declarator [ type-qualifier-list * ]
707 direct-declarator ( parameter-type-list )
708 direct-declarator ( identifier-list(opt) ) */
711 c_pretty_printer::direct_declarator (tree t
)
713 switch (TREE_CODE (t
))
720 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
721 pp_c_tree_decl_identifier (this, t
);
726 abstract_declarator (TREE_TYPE (t
));
730 pp_parameter_list (this, t
);
731 abstract_declarator (TREE_TYPE (t
));
735 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
736 pp_c_tree_decl_identifier (this, t
);
737 if (flags
& pp_c_flag_abstract
)
738 abstract_declarator (TREE_TYPE (t
));
741 pp_parameter_list (this, t
);
742 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
748 case FIXED_POINT_TYPE
:
755 pp_unsupported_tree (this, t
);
762 pointer(opt) direct-declarator */
765 c_pretty_printer::declarator (tree t
)
767 switch (TREE_CODE (t
))
771 case FIXED_POINT_TYPE
:
784 direct_declarator (t
);
789 pp_unsupported_tree (this, t
);
795 declaration-specifiers init-declarator-list(opt) ; */
798 c_pretty_printer::declaration (tree t
)
800 declaration_specifiers (t
);
801 pp_c_init_declarator (this, t
);
804 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
807 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
809 if (attributes
== NULL_TREE
)
812 pp_c_ws_string (pp
, "__attribute__");
813 pp_c_left_paren (pp
);
814 pp_c_left_paren (pp
);
815 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
817 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
818 if (TREE_VALUE (attributes
))
819 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
821 if (TREE_CHAIN (attributes
))
822 pp_separate_with (pp
, ',');
824 pp_c_right_paren (pp
);
825 pp_c_right_paren (pp
);
828 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
829 marked to be displayed on disgnostic. */
832 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
834 bool is_first
= true;
839 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
841 const struct attribute_spec
*as
;
842 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
843 if (!as
|| as
->affects_type_identity
== false)
847 pp_c_ws_string (pp
, "__attribute__");
848 pp_c_left_paren (pp
);
849 pp_c_left_paren (pp
);
854 pp_separate_with (pp
, ',');
856 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
858 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
863 pp_c_right_paren (pp
);
864 pp_c_right_paren (pp
);
865 pp_c_whitespace (pp
);
869 /* function-definition:
870 declaration-specifiers declarator compound-statement */
873 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
875 pp
->declaration_specifiers (t
);
877 pp_needs_newline (pp
) = true;
878 pp
->statement (DECL_SAVED_TREE (t
));
879 pp_newline_and_flush (pp
);
885 /* Print out a c-char. This is called solely for characters which are
886 in the *target* execution character set. We ought to convert them
887 back to the *host* execution character set before printing, but we
888 have no way to do this at present. A decent compromise is to print
889 all characters as if they were in the host execution character set,
890 and not attempt to recover any named escape characters, but render
891 all unprintables as octal escapes. If the host and target character
892 sets are the same, this produces relatively readable output. If they
893 are not the same, strings may appear as gibberish, but that's okay
894 (in fact, it may well be what the reader wants, e.g. if they are looking
895 to see if conversion to the target character set happened correctly).
897 A special case: we need to prefix \, ", and ' with backslashes. It is
898 correct to do so for the *host*'s \, ", and ', because the rest of the
899 file appears in the host character set. */
902 pp_c_char (c_pretty_printer
*pp
, int c
)
908 case '\\': pp_string (pp
, "\\\\"); break;
909 case '\'': pp_string (pp
, "\\\'"); break;
910 case '\"': pp_string (pp
, "\\\""); break;
911 default: pp_character (pp
, c
);
915 pp_scalar (pp
, "\\%03o", (unsigned) c
);
918 /* Print out a STRING literal. */
921 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
923 const char *p
= TREE_STRING_POINTER (s
);
924 int n
= TREE_STRING_LENGTH (s
) - 1;
927 for (i
= 0; i
< n
; ++i
)
928 pp_c_char (pp
, p
[i
]);
932 /* Pretty-print an INTEGER literal. */
935 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
937 /* We are going to compare the type of I to other types using
938 pointer comparison so we need to use its canonical type. */
940 TYPE_CANONICAL (TREE_TYPE (i
))
941 ? TYPE_CANONICAL (TREE_TYPE (i
))
944 if (host_integerp (i
, 0))
945 pp_wide_integer (pp
, TREE_INT_CST_LOW (i
));
946 else if (host_integerp (i
, 1))
947 pp_unsigned_wide_integer (pp
, TREE_INT_CST_LOW (i
));
950 unsigned HOST_WIDE_INT low
= TREE_INT_CST_LOW (i
);
951 HOST_WIDE_INT high
= TREE_INT_CST_HIGH (i
);
952 if (tree_int_cst_sgn (i
) < 0)
958 sprintf (pp_buffer (pp
)->digit_buffer
, HOST_WIDE_INT_PRINT_DOUBLE_HEX
,
959 (unsigned HOST_WIDE_INT
) high
, (unsigned HOST_WIDE_INT
) low
);
960 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
962 if (TYPE_UNSIGNED (type
))
963 pp_character (pp
, 'u');
964 if (type
== long_integer_type_node
|| type
== long_unsigned_type_node
)
965 pp_character (pp
, 'l');
966 else if (type
== long_long_integer_type_node
967 || type
== long_long_unsigned_type_node
)
968 pp_string (pp
, "ll");
969 else if (type
== int128_integer_type_node
970 || type
== int128_unsigned_type_node
)
971 pp_string (pp
, "I128");
974 /* Print out a CHARACTER literal. */
977 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
979 tree type
= TREE_TYPE (c
);
980 if (type
== wchar_type_node
)
981 pp_character (pp
, 'L');
983 if (host_integerp (c
, TYPE_UNSIGNED (type
)))
984 pp_c_char (pp
, tree_low_cst (c
, TYPE_UNSIGNED (type
)));
986 pp_scalar (pp
, "\\x%x", (unsigned) TREE_INT_CST_LOW (c
));
990 /* Print out a BOOLEAN literal. */
993 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
995 if (b
== boolean_false_node
)
997 if (c_dialect_cxx ())
998 pp_c_ws_string (pp
, "false");
999 else if (flag_isoc99
)
1000 pp_c_ws_string (pp
, "_False");
1002 pp_unsupported_tree (pp
, b
);
1004 else if (b
== boolean_true_node
)
1006 if (c_dialect_cxx ())
1007 pp_c_ws_string (pp
, "true");
1008 else if (flag_isoc99
)
1009 pp_c_ws_string (pp
, "_True");
1011 pp_unsupported_tree (pp
, b
);
1013 else if (TREE_CODE (b
) == INTEGER_CST
)
1014 pp_c_integer_constant (pp
, b
);
1016 pp_unsupported_tree (pp
, b
);
1019 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
1020 false; that means the value was obtained by a cast, in which case
1021 print out the type-id part of the cast-expression -- the casted value
1022 is then printed by pp_c_integer_literal. */
1025 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
1027 bool value_is_named
= true;
1028 tree type
= TREE_TYPE (e
);
1031 /* Find the name of this constant. */
1032 for (value
= TYPE_VALUES (type
);
1033 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
1034 value
= TREE_CHAIN (value
))
1037 if (value
!= NULL_TREE
)
1038 pp
->id_expression (TREE_PURPOSE (value
));
1041 /* Value must have been cast. */
1042 pp_c_type_cast (pp
, type
);
1043 value_is_named
= false;
1046 return value_is_named
;
1049 /* Print out a REAL value as a decimal-floating-constant. */
1052 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1054 const struct real_format
*fmt
1055 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1057 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1058 bool is_decimal
= floating_cst
.decimal
;
1060 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1061 log10(2) to 7 significant digits. */
1062 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1064 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1065 sizeof (pp_buffer (pp
)->digit_buffer
),
1068 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1069 if (TREE_TYPE (r
) == float_type_node
)
1070 pp_character (pp
, 'f');
1071 else if (TREE_TYPE (r
) == long_double_type_node
)
1072 pp_character (pp
, 'l');
1073 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1074 pp_string (pp
, "dl");
1075 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1076 pp_string (pp
, "dd");
1077 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1078 pp_string (pp
, "df");
1081 /* Print out a FIXED value as a decimal-floating-constant. */
1084 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1086 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1087 sizeof (pp_buffer (pp
)->digit_buffer
));
1088 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1091 /* Pretty-print a compound literal expression. GNU extensions include
1092 vector constants. */
1095 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1097 tree type
= TREE_TYPE (e
);
1098 pp_c_type_cast (pp
, type
);
1100 switch (TREE_CODE (type
))
1107 pp_c_brace_enclosed_initializer_list (pp
, e
);
1111 pp_unsupported_tree (pp
, e
);
1116 /* Pretty-print a COMPLEX_EXPR expression. */
1119 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1121 /* Handle a few common special cases, otherwise fallback
1122 to printing it as compound literal. */
1123 tree type
= TREE_TYPE (e
);
1124 tree realexpr
= TREE_OPERAND (e
, 0);
1125 tree imagexpr
= TREE_OPERAND (e
, 1);
1127 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1128 if (TREE_CODE (realexpr
) == NOP_EXPR
1129 && TREE_CODE (imagexpr
) == NOP_EXPR
1130 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1131 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1132 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1133 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1134 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1135 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1137 pp_c_type_cast (pp
, type
);
1138 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1142 /* Cast of an scalar expression to COMPLEX_TYPE. */
1143 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1144 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1146 pp_c_type_cast (pp
, type
);
1147 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1148 realexpr
= TREE_OPERAND (realexpr
, 0);
1149 pp
->expression (realexpr
);
1153 pp_c_compound_literal (pp
, e
);
1159 fixed-point-constant
1160 enumeration-constant
1161 character-constant */
1164 c_pretty_printer::constant (tree e
)
1166 const enum tree_code code
= TREE_CODE (e
);
1172 tree type
= TREE_TYPE (e
);
1173 if (type
== boolean_type_node
)
1174 pp_c_bool_constant (this, e
);
1175 else if (type
== char_type_node
)
1176 pp_c_character_constant (this, e
);
1177 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1178 && pp_c_enumeration_constant (this, e
))
1181 pp_c_integer_constant (this, e
);
1186 pp_c_floating_constant (this, e
);
1190 pp_c_fixed_constant (this, e
);
1194 pp_c_string_literal (this, e
);
1198 /* Sometimes, we are confused and we think a complex literal
1199 is a constant. Such thing is a compound literal which
1200 grammatically belongs to postfix-expr production. */
1201 pp_c_compound_literal (this, e
);
1205 pp_unsupported_tree (this, e
);
1210 /* Pretty-print a string such as an identifier, without changing its
1211 encoding, preceded by whitespace is necessary. */
1214 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1216 pp_c_maybe_whitespace (pp
);
1217 pp_string (pp
, str
);
1218 pp
->padding
= pp_before
;
1222 c_pretty_printer::translate_string (const char *gmsgid
)
1224 if (pp_translate_identifiers (this))
1225 pp_c_ws_string (this, _(gmsgid
));
1227 pp_c_ws_string (this, gmsgid
);
1230 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1231 that need converting to the locale encoding, preceded by whitespace
1235 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1237 pp_c_maybe_whitespace (pp
);
1238 pp_identifier (pp
, id
);
1239 pp
->padding
= pp_before
;
1242 /* Pretty-print a C primary-expression.
1250 c_pretty_printer::primary_expression (tree e
)
1252 switch (TREE_CODE (e
))
1260 pp_c_tree_decl_identifier (this, e
);
1263 case IDENTIFIER_NODE
:
1264 pp_c_tree_identifier (this, e
);
1268 translate_string ("<erroneous-expression>");
1272 translate_string ("<return-value>");
1283 pp_c_ws_string (this, "__builtin_memcpy");
1284 pp_c_left_paren (this);
1285 pp_ampersand (this);
1286 primary_expression (TREE_OPERAND (e
, 0));
1287 pp_separate_with (this, ',');
1288 pp_ampersand (this);
1289 initializer (TREE_OPERAND (e
, 1));
1290 if (TREE_OPERAND (e
, 2))
1292 pp_separate_with (this, ',');
1293 expression (TREE_OPERAND (e
, 2));
1295 pp_c_right_paren (this);
1299 /* FIXME: Make sure we won't get into an infinite loop. */
1300 pp_c_left_paren (this);
1302 pp_c_right_paren (this);
1307 /* Print out a C initializer -- also support C compound-literals.
1309 assignment-expression:
1310 { initializer-list }
1311 { initializer-list , } */
1314 c_pretty_printer::initializer (tree e
)
1316 if (TREE_CODE (e
) == CONSTRUCTOR
)
1317 pp_c_brace_enclosed_initializer_list (this, e
);
1324 declarator = initializer */
1327 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1330 /* We don't want to output function definitions here. There are handled
1331 elsewhere (and the syntactic form is bogus anyway). */
1332 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1334 tree init
= DECL_INITIAL (t
);
1335 /* This C++ bit is handled here because it is easier to do so.
1336 In templates, the C++ parser builds a TREE_LIST for a
1337 direct-initialization; the TREE_PURPOSE is the variable to
1338 initialize and the TREE_VALUE is the initializer. */
1339 if (TREE_CODE (init
) == TREE_LIST
)
1341 pp_c_left_paren (pp
);
1342 pp
->expression (TREE_VALUE (init
));
1343 pp_right_paren (pp
);
1350 pp
->initializer (init
);
1355 /* initializer-list:
1356 designation(opt) initializer
1357 initializer-list , designation(opt) initializer
1364 designator-list designator
1367 [ constant-expression ]
1371 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1373 tree type
= TREE_TYPE (e
);
1374 const enum tree_code code
= TREE_CODE (type
);
1376 if (TREE_CODE (e
) == CONSTRUCTOR
)
1378 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1388 tree init
= TREE_OPERAND (e
, 0);
1389 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1391 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1394 pp
->primary_expression (TREE_PURPOSE (init
));
1398 pp_c_left_bracket (pp
);
1399 if (TREE_PURPOSE (init
))
1400 pp
->constant (TREE_PURPOSE (init
));
1401 pp_c_right_bracket (pp
);
1403 pp_c_whitespace (pp
);
1405 pp_c_whitespace (pp
);
1406 pp
->initializer (TREE_VALUE (init
));
1407 if (TREE_CHAIN (init
))
1408 pp_separate_with (pp
, ',');
1414 if (TREE_CODE (e
) == VECTOR_CST
)
1417 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1420 pp_separate_with (pp
, ',');
1421 pp
->expression (VECTOR_CST_ELT (e
, i
));
1429 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1431 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1432 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1433 pp_separate_with (pp
, ',');
1434 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1444 pp_unsupported_tree (pp
, type
);
1447 /* Pretty-print a brace-enclosed initializer-list. */
1450 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1452 pp_c_left_brace (pp
);
1453 pp_c_initializer_list (pp
, l
);
1454 pp_c_right_brace (pp
);
1458 /* This is a convenient function, used to bridge gap between C and C++
1465 c_pretty_printer::id_expression (tree t
)
1467 switch (TREE_CODE (t
))
1476 pp_c_tree_decl_identifier (this, t
);
1479 case IDENTIFIER_NODE
:
1480 pp_c_tree_identifier (this, t
);
1484 pp_unsupported_tree (this, t
);
1489 /* postfix-expression:
1491 postfix-expression [ expression ]
1492 postfix-expression ( argument-expression-list(opt) )
1493 postfix-expression . identifier
1494 postfix-expression -> identifier
1495 postfix-expression ++
1496 postfix-expression --
1497 ( type-name ) { initializer-list }
1498 ( type-name ) { initializer-list , } */
1501 c_pretty_printer::postfix_expression (tree e
)
1503 enum tree_code code
= TREE_CODE (e
);
1506 case POSTINCREMENT_EXPR
:
1507 case POSTDECREMENT_EXPR
:
1508 postfix_expression (TREE_OPERAND (e
, 0));
1509 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1513 postfix_expression (TREE_OPERAND (e
, 0));
1514 pp_c_left_bracket (this);
1515 expression (TREE_OPERAND (e
, 1));
1516 pp_c_right_bracket (this);
1519 case ARRAY_NOTATION_REF
:
1520 postfix_expression (ARRAY_NOTATION_ARRAY (e
));
1521 pp_c_left_bracket (this);
1522 expression (ARRAY_NOTATION_START (e
));
1524 expression (ARRAY_NOTATION_LENGTH (e
));
1526 expression (ARRAY_NOTATION_STRIDE (e
));
1527 pp_c_right_bracket (this);
1532 call_expr_arg_iterator iter
;
1534 postfix_expression (CALL_EXPR_FN (e
));
1535 pp_c_left_paren (this);
1536 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1539 if (more_call_expr_args_p (&iter
))
1540 pp_separate_with (this, ',');
1542 pp_c_right_paren (this);
1546 case UNORDERED_EXPR
:
1547 pp_c_ws_string (this, flag_isoc99
1549 : "__builtin_isunordered");
1553 pp_c_ws_string (this, flag_isoc99
1555 : "!__builtin_isunordered");
1559 pp_c_ws_string (this, flag_isoc99
1561 : "!__builtin_isgreaterequal");
1565 pp_c_ws_string (this, flag_isoc99
1567 : "!__builtin_isgreater");
1571 pp_c_ws_string (this, flag_isoc99
1573 : "!__builtin_islessequal");
1577 pp_c_ws_string (this, flag_isoc99
1579 : "!__builtin_isless");
1583 pp_c_ws_string (this, flag_isoc99
1585 : "!__builtin_islessgreater");
1589 pp_c_ws_string (this, flag_isoc99
1591 : "__builtin_islessgreater");
1595 pp_c_left_paren (this);
1596 expression (TREE_OPERAND (e
, 0));
1597 pp_separate_with (this, ',');
1598 expression (TREE_OPERAND (e
, 1));
1599 pp_c_right_paren (this);
1603 pp_c_ws_string (this, "__builtin_abs");
1604 pp_c_left_paren (this);
1605 expression (TREE_OPERAND (e
, 0));
1606 pp_c_right_paren (this);
1611 tree object
= TREE_OPERAND (e
, 0);
1612 if (TREE_CODE (object
) == INDIRECT_REF
)
1614 postfix_expression (TREE_OPERAND (object
, 0));
1619 postfix_expression (object
);
1622 expression (TREE_OPERAND (e
, 1));
1628 tree type
= TREE_TYPE (e
);
1630 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1632 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1634 HOST_WIDE_INT bitpos
= tree_low_cst (TREE_OPERAND (e
, 2), 0);
1635 HOST_WIDE_INT size
= tree_low_cst (TYPE_SIZE (type
), 0);
1636 if ((bitpos
% size
) == 0)
1638 pp_c_left_paren (this);
1639 pp_c_left_paren (this);
1642 pp_c_right_paren (this);
1643 pp_c_ampersand (this);
1644 expression (TREE_OPERAND (e
, 0));
1645 pp_c_right_paren (this);
1646 pp_c_left_bracket (this);
1647 pp_wide_integer (this, bitpos
/ size
);
1648 pp_c_right_bracket (this);
1652 pp_unsupported_tree (this, e
);
1662 pp_c_compound_literal (this, e
);
1666 pp_c_complex_expr (this, e
);
1669 case COMPOUND_LITERAL_EXPR
:
1670 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1677 pp_c_ws_string (this, "__builtin_va_arg");
1678 pp_c_left_paren (this);
1679 assignment_expression (TREE_OPERAND (e
, 0));
1680 pp_separate_with (this, ',');
1681 type_id (TREE_TYPE (e
));
1682 pp_c_right_paren (this);
1686 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1688 id_expression (TREE_OPERAND (e
, 0));
1691 /* else fall through. */
1694 primary_expression (e
);
1699 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1702 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1704 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1706 pp
->expression (TREE_VALUE (e
));
1708 pp_separate_with (pp
, ',');
1712 /* Print out V, which contains the elements of a constructor. */
1715 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1717 unsigned HOST_WIDE_INT ix
;
1720 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1722 pp
->expression (value
);
1723 if (ix
!= vec_safe_length (v
) - 1)
1724 pp_separate_with (pp
, ',');
1728 /* Print out an expression-list in parens, as if it were the argument
1729 list to a function. */
1732 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1734 pp_c_left_paren (pp
);
1735 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1736 pp_c_expression_list (pp
, t
);
1737 pp_c_right_paren (pp
);
1740 /* unary-expression:
1744 unary-operator cast-expression
1745 sizeof unary-expression
1748 unary-operator: one of
1753 __alignof__ unary-expression
1754 __alignof__ ( type-id )
1755 __real__ unary-expression
1756 __imag__ unary-expression */
1759 c_pretty_printer::unary_expression (tree e
)
1761 enum tree_code code
= TREE_CODE (e
);
1764 case PREINCREMENT_EXPR
:
1765 case PREDECREMENT_EXPR
:
1766 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1767 unary_expression (TREE_OPERAND (e
, 0));
1774 case TRUTH_NOT_EXPR
:
1776 /* String literal are used by address. */
1777 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1778 pp_ampersand (this);
1779 else if (code
== INDIRECT_REF
)
1781 else if (code
== NEGATE_EXPR
)
1783 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1784 pp_complement (this);
1785 else if (code
== TRUTH_NOT_EXPR
)
1786 pp_exclamation (this);
1787 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1791 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1792 && integer_zerop (TREE_OPERAND (e
, 1)))
1793 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1797 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1799 pp_c_left_paren (this);
1800 if (!integer_onep (TYPE_SIZE_UNIT
1801 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1802 pp_c_type_cast (this, ptr_type_node
);
1804 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1805 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1808 pp_c_integer_constant (this,
1809 fold_convert (ssizetype
,
1810 TREE_OPERAND (e
, 1)));
1811 pp_c_right_paren (this);
1818 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1819 pp_c_whitespace (this);
1820 unary_expression (TREE_OPERAND (e
, 0));
1824 postfix_expression (e
);
1831 ( type-name ) cast-expression */
1834 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1836 switch (TREE_CODE (e
))
1839 case FIX_TRUNC_EXPR
:
1841 case VIEW_CONVERT_EXPR
:
1842 pp_c_type_cast (pp
, TREE_TYPE (e
));
1843 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1847 pp
->unary_expression (e
);
1851 /* multiplicative-expression:
1853 multiplicative-expression * cast-expression
1854 multiplicative-expression / cast-expression
1855 multiplicative-expression % cast-expression */
1858 c_pretty_printer::multiplicative_expression (tree e
)
1860 enum tree_code code
= TREE_CODE (e
);
1864 case TRUNC_DIV_EXPR
:
1865 case TRUNC_MOD_EXPR
:
1866 multiplicative_expression (TREE_OPERAND (e
, 0));
1867 pp_c_whitespace (this);
1868 if (code
== MULT_EXPR
)
1870 else if (code
== TRUNC_DIV_EXPR
)
1874 pp_c_whitespace (this);
1875 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1879 pp_c_cast_expression (this, e
);
1884 /* additive-expression:
1885 multiplicative-expression
1886 additive-expression + multiplicative-expression
1887 additive-expression - multiplicative-expression */
1890 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1892 enum tree_code code
= TREE_CODE (e
);
1895 case POINTER_PLUS_EXPR
:
1898 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1899 pp_c_whitespace (pp
);
1900 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1904 pp_c_whitespace (pp
);
1905 pp
->multiplicative_expression (TREE_OPERAND (e
, 1));
1909 pp
->multiplicative_expression (e
);
1914 /* additive-expression:
1916 shift-expression << additive-expression
1917 shift-expression >> additive-expression */
1920 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1922 enum tree_code code
= TREE_CODE (e
);
1927 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1928 pp_c_whitespace (pp
);
1929 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" : ">>");
1930 pp_c_whitespace (pp
);
1931 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1935 pp_c_additive_expression (pp
, e
);
1939 /* relational-expression:
1941 relational-expression < shift-expression
1942 relational-expression > shift-expression
1943 relational-expression <= shift-expression
1944 relational-expression >= shift-expression */
1947 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1949 enum tree_code code
= TREE_CODE (e
);
1956 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1957 pp_c_whitespace (pp
);
1958 if (code
== LT_EXPR
)
1960 else if (code
== GT_EXPR
)
1962 else if (code
== LE_EXPR
)
1964 else if (code
== GE_EXPR
)
1965 pp_greater_equal (pp
);
1966 pp_c_whitespace (pp
);
1967 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1971 pp_c_shift_expression (pp
, e
);
1976 /* equality-expression:
1977 relational-expression
1978 equality-expression == relational-expression
1979 equality-equality != relational-expression */
1982 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1984 enum tree_code code
= TREE_CODE (e
);
1989 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1990 pp_c_whitespace (pp
);
1991 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1992 pp_c_whitespace (pp
);
1993 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1997 pp_c_relational_expression (pp
, e
);
2004 AND-expression & equality-equality */
2007 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
2009 if (TREE_CODE (e
) == BIT_AND_EXPR
)
2011 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
2012 pp_c_whitespace (pp
);
2014 pp_c_whitespace (pp
);
2015 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
2018 pp_c_equality_expression (pp
, e
);
2021 /* exclusive-OR-expression:
2023 exclusive-OR-expression ^ AND-expression */
2026 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2028 if (TREE_CODE (e
) == BIT_XOR_EXPR
2029 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2031 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2032 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2033 pp_c_maybe_whitespace (pp
);
2035 pp_c_whitespace (pp
);
2037 pp_c_whitespace (pp
);
2038 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2041 pp_c_and_expression (pp
, e
);
2044 /* inclusive-OR-expression:
2045 exclusive-OR-expression
2046 inclusive-OR-expression | exclusive-OR-expression */
2049 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2051 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2053 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2054 pp_c_whitespace (pp
);
2056 pp_c_whitespace (pp
);
2057 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2060 pp_c_exclusive_or_expression (pp
, e
);
2063 /* logical-AND-expression:
2064 inclusive-OR-expression
2065 logical-AND-expression && inclusive-OR-expression */
2068 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2070 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2071 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2073 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2074 pp_c_whitespace (pp
);
2075 pp_ampersand_ampersand (pp
);
2076 pp_c_whitespace (pp
);
2077 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2080 pp_c_inclusive_or_expression (pp
, e
);
2083 /* logical-OR-expression:
2084 logical-AND-expression
2085 logical-OR-expression || logical-AND-expression */
2088 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2090 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2091 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2093 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2094 pp_c_whitespace (pp
);
2096 pp_c_whitespace (pp
);
2097 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2100 pp_c_logical_and_expression (pp
, e
);
2103 /* conditional-expression:
2104 logical-OR-expression
2105 logical-OR-expression ? expression : conditional-expression */
2108 c_pretty_printer::conditional_expression (tree e
)
2110 if (TREE_CODE (e
) == COND_EXPR
)
2112 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2113 pp_c_whitespace (this);
2115 pp_c_whitespace (this);
2116 expression (TREE_OPERAND (e
, 1));
2117 pp_c_whitespace (this);
2119 pp_c_whitespace (this);
2120 conditional_expression (TREE_OPERAND (e
, 2));
2123 pp_c_logical_or_expression (this, e
);
2127 /* assignment-expression:
2128 conditional-expression
2129 unary-expression assignment-operator assignment-expression
2131 assignment-expression: one of
2132 = *= /= %= += -= >>= <<= &= ^= |= */
2135 c_pretty_printer::assignment_expression (tree e
)
2137 if (TREE_CODE (e
) == MODIFY_EXPR
2138 || TREE_CODE (e
) == INIT_EXPR
)
2140 unary_expression (TREE_OPERAND (e
, 0));
2141 pp_c_whitespace (this);
2144 expression (TREE_OPERAND (e
, 1));
2147 conditional_expression (e
);
2151 assignment-expression
2152 expression , assignment-expression
2154 Implementation note: instead of going through the usual recursion
2155 chain, I take the liberty of dispatching nodes to the appropriate
2156 functions. This makes some redundancy, but it worths it. That also
2157 prevents a possible infinite recursion between primary_expression ()
2158 and expression (). */
2161 c_pretty_printer::expression (tree e
)
2163 switch (TREE_CODE (e
))
2166 pp_c_integer_constant (this, e
);
2170 pp_c_floating_constant (this, e
);
2174 pp_c_fixed_constant (this, e
);
2178 pp_c_string_literal (this, e
);
2181 case IDENTIFIER_NODE
:
2190 primary_expression (e
);
2194 if (SSA_NAME_VAR (e
)
2195 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2196 expression (SSA_NAME_VAR (e
));
2198 translate_string ("<unknown>");
2201 case POSTINCREMENT_EXPR
:
2202 case POSTDECREMENT_EXPR
:
2204 case ARRAY_NOTATION_REF
:
2212 case UNORDERED_EXPR
:
2221 case COMPOUND_LITERAL_EXPR
:
2223 postfix_expression (e
);
2232 case TRUTH_NOT_EXPR
:
2233 case PREINCREMENT_EXPR
:
2234 case PREDECREMENT_EXPR
:
2237 unary_expression (e
);
2241 case FIX_TRUNC_EXPR
:
2243 case VIEW_CONVERT_EXPR
:
2244 pp_c_cast_expression (this, e
);
2248 case TRUNC_MOD_EXPR
:
2249 case TRUNC_DIV_EXPR
:
2250 multiplicative_expression (e
);
2255 pp_c_shift_expression (this, e
);
2262 pp_c_relational_expression (this, e
);
2266 pp_c_and_expression (this, e
);
2270 case TRUTH_XOR_EXPR
:
2271 pp_c_exclusive_or_expression (this, e
);
2275 pp_c_inclusive_or_expression (this, e
);
2278 case TRUTH_ANDIF_EXPR
:
2279 case TRUTH_AND_EXPR
:
2280 pp_c_logical_and_expression (this, e
);
2283 case TRUTH_ORIF_EXPR
:
2285 pp_c_logical_or_expression (this, e
);
2290 pp_c_equality_expression (this, e
);
2294 conditional_expression (e
);
2297 case POINTER_PLUS_EXPR
:
2300 pp_c_additive_expression (this, e
);
2305 assignment_expression (e
);
2309 pp_c_left_paren (this);
2310 expression (TREE_OPERAND (e
, 0));
2311 pp_separate_with (this, ',');
2312 assignment_expression (TREE_OPERAND (e
, 1));
2313 pp_c_right_paren (this);
2316 case NON_LVALUE_EXPR
:
2318 expression (TREE_OPERAND (e
, 0));
2322 postfix_expression (TREE_OPERAND (e
, 1));
2327 /* We don't yet have a way of dumping statements in a
2328 human-readable format. */
2329 pp_string (this, "({...})");
2332 case C_MAYBE_CONST_EXPR
:
2333 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2337 pp_unsupported_tree (this, e
);
2347 c_pretty_printer::statement (tree stmt
)
2352 if (pp_needs_newline (this))
2353 pp_newline_and_indent (this, 0);
2355 dump_generic_node (this, stmt
, pp_indentation (this), 0, true);
2359 /* Initialize the PRETTY-PRINTER for handling C codes. */
2361 c_pretty_printer::c_pretty_printer ()
2362 : pretty_printer (),
2366 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2367 ptr_operator
= pp_c_pointer
;
2368 parameter_list
= pp_c_parameter_type_list
;
2372 /* Print the tree T in full, on file FILE. */
2375 print_c_tree (FILE *file
, tree t
)
2377 c_pretty_printer pp
;
2379 pp_needs_newline (&pp
) = true;
2380 pp
.buffer
->stream
= file
;
2382 pp_newline_and_flush (&pp
);
2385 /* Print the tree T in full, on stderr. */
2388 debug_c_tree (tree t
)
2390 print_c_tree (stderr
, t
);
2391 fputc ('\n', stderr
);
2394 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2395 up of T's memory address. */
2398 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2402 gcc_assert (DECL_P (t
));
2405 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2408 static char xname
[8];
2409 sprintf (xname
, "<U%4x>", ((unsigned)((uintptr_t)(t
) & 0xffff)));
2413 pp_c_identifier (pp
, name
);