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_CONST
)
184 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
188 if (qualifiers
& TYPE_QUAL_VOLATILE
)
191 pp_c_whitespace (pp
);
192 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
196 if (qualifiers
& TYPE_QUAL_RESTRICT
)
199 pp_c_whitespace (pp
);
200 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
201 ? "restrict" : "__restrict__"));
205 /* Pretty-print T using the type-cast notation '( type-name )'. */
208 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
210 pp_c_left_paren (pp
);
212 pp_c_right_paren (pp
);
215 /* We're about to pretty-print a pointer type as indicated by T.
216 Output a whitespace, if needed, preparing for subsequent output. */
219 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
221 if (POINTER_TYPE_P (t
))
223 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
224 if (TREE_CODE (pointee
) != ARRAY_TYPE
225 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
226 pp_c_whitespace (pp
);
233 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
234 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
235 of its type. Take care of possible extensions.
239 type-qualifier-list type-qualifier
244 __restrict__ -- GNU C
245 address-space-qualifier -- GNU C
248 address-space-qualifier:
249 identifier -- GNU C */
252 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
256 if (!t
|| t
== error_mark_node
)
262 qualifiers
= TYPE_QUALS (t
);
263 pp_c_cv_qualifiers (pp
, qualifiers
,
264 TREE_CODE (t
) == FUNCTION_TYPE
);
266 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
268 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
269 pp_c_identifier (pp
, as
);
274 * type-qualifier-list(opt)
275 * type-qualifier-list(opt) pointer */
278 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
280 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
282 switch (TREE_CODE (t
))
285 /* It is easier to handle C++ reference types here. */
287 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
288 pp_c_pointer (pp
, TREE_TYPE (t
));
289 if (TREE_CODE (t
) == POINTER_TYPE
)
293 pp_c_type_qualifier_list (pp
, t
);
296 /* ??? This node is now in GENERIC and so shouldn't be here. But
297 we'll fix that later. */
299 pp_declaration (pp
, DECL_EXPR_DECL (t
));
300 pp_needs_newline (pp
) = true;
304 pp_unsupported_tree (pp
, t
);
321 struct-or-union-specifier
326 simple-type-specifier:
331 pp_c_type_specifier (c_pretty_printer
*pp
, tree t
)
333 const enum tree_code code
= TREE_CODE (t
);
337 pp
->translate_string ("<type-error>");
340 case IDENTIFIER_NODE
:
341 pp_c_identifier (pp
, IDENTIFIER_POINTER (t
));
348 case FIXED_POINT_TYPE
:
352 pp_c_type_specifier (pp
, t
);
356 int prec
= TYPE_PRECISION (t
);
357 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
358 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_SATURATING (t
));
360 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_UNSIGNED (t
));
363 pp_c_type_specifier (pp
, t
);
364 if (TYPE_PRECISION (t
) != prec
)
367 pp_decimal_int (pp
, prec
);
375 pp
->translate_string (TYPE_UNSIGNED (t
)
376 ? "<unnamed-unsigned:"
377 : "<unnamed-signed:");
380 pp
->translate_string ("<unnamed-float:");
382 case FIXED_POINT_TYPE
:
383 pp
->translate_string ("<unnamed-fixed:");
388 pp_decimal_int (pp
, prec
);
396 pp_id_expression (pp
, t
);
398 pp
->translate_string ("<typedef-error>");
404 if (code
== UNION_TYPE
)
405 pp_c_ws_string (pp
, "union");
406 else if (code
== RECORD_TYPE
)
407 pp_c_ws_string (pp
, "struct");
408 else if (code
== ENUMERAL_TYPE
)
409 pp_c_ws_string (pp
, "enum");
411 pp
->translate_string ("<tag-error>");
414 pp_id_expression (pp
, TYPE_NAME (t
));
416 pp
->translate_string ("<anonymous>");
420 pp_unsupported_tree (pp
, t
);
425 /* specifier-qualifier-list:
426 type-specifier specifier-qualifier-list-opt
427 type-qualifier specifier-qualifier-list-opt
430 Implementation note: Because of the non-linearities in array or
431 function declarations, this routine prints not just the
432 specifier-qualifier-list of such entities or types of such entities,
433 but also the 'pointer' production part of their declarators. The
434 remaining part is done by pp_declarator or pp_abstract_declarator. */
437 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
439 const enum tree_code code
= TREE_CODE (t
);
441 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
442 pp_c_type_qualifier_list (pp
, t
);
448 /* Get the types-specifier of this type. */
449 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
450 pp_c_specifier_qualifier_list (pp
, pointee
);
451 if (TREE_CODE (pointee
) == ARRAY_TYPE
452 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
454 pp_c_whitespace (pp
);
455 pp_c_left_paren (pp
);
456 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
458 else if (!c_dialect_cxx ())
459 pp_c_whitespace (pp
);
460 pp_ptr_operator (pp
, t
);
466 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
471 if (code
== COMPLEX_TYPE
)
472 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
473 ? "_Complex" : "__complex__"));
474 else if (code
== VECTOR_TYPE
)
476 pp_c_ws_string (pp
, "__vector");
477 pp_c_left_paren (pp
);
478 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
479 pp_c_right_paren (pp
);
480 pp_c_whitespace (pp
);
482 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
486 pp_simple_type_specifier (pp
, t
);
489 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
490 pp_c_type_qualifier_list (pp
, t
);
493 /* parameter-type-list:
498 parameter-declaration
499 parameter-list , parameter-declaration
501 parameter-declaration:
502 declaration-specifiers declarator
503 declaration-specifiers abstract-declarator(opt) */
506 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
508 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
509 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
510 pp_c_left_paren (pp
);
511 if (parms
== void_list_node
)
512 pp_c_ws_string (pp
, "void");
516 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
519 pp_separate_with (pp
, ',');
521 pp_declaration_specifiers
522 (pp
, want_parm_decl
? parms
: TREE_VALUE (parms
));
524 pp_declarator (pp
, parms
);
526 pp_abstract_declarator (pp
, TREE_VALUE (parms
));
529 pp_c_right_paren (pp
);
532 /* abstract-declarator:
534 pointer(opt) direct-abstract-declarator */
537 c_pretty_printer::abstract_declarator (tree t
)
539 if (TREE_CODE (t
) == POINTER_TYPE
)
541 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
542 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
543 pp_c_right_paren (this);
547 direct_abstract_declarator (t
);
550 /* direct-abstract-declarator:
551 ( abstract-declarator )
552 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
553 direct-abstract-declarator(opt) [ * ]
554 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
557 c_pretty_printer::direct_abstract_declarator (tree t
)
559 switch (TREE_CODE (t
))
562 abstract_declarator (t
);
566 pp_c_parameter_type_list (this, t
);
567 direct_abstract_declarator (TREE_TYPE (t
));
571 pp_c_left_bracket (this);
572 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
574 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
575 tree type
= TREE_TYPE (maxval
);
577 if (host_integerp (maxval
, 0))
578 pp_wide_integer (this, tree_low_cst (maxval
, 0) + 1);
580 pp_expression (this, fold_build2 (PLUS_EXPR
, type
, maxval
,
581 build_int_cst (type
, 1)));
583 pp_c_right_bracket (this);
584 direct_abstract_declarator (TREE_TYPE (t
));
587 case IDENTIFIER_NODE
:
592 case FIXED_POINT_TYPE
:
602 pp_unsupported_tree (this, t
);
608 specifier-qualifier-list abstract-declarator(opt) */
611 pp_c_type_id (c_pretty_printer
*pp
, tree t
)
613 pp_c_specifier_qualifier_list (pp
, t
);
614 pp_abstract_declarator (pp
, t
);
617 /* storage-class-specifier:
625 pp_c_storage_class_specifier (c_pretty_printer
*pp
, tree t
)
627 if (TREE_CODE (t
) == TYPE_DECL
)
628 pp_c_ws_string (pp
, "typedef");
631 if (DECL_REGISTER (t
))
632 pp_c_ws_string (pp
, "register");
633 else if (TREE_STATIC (t
) && TREE_CODE (t
) == VAR_DECL
)
634 pp_c_ws_string (pp
, "static");
638 /* function-specifier:
642 c_pretty_printer::function_specifier (tree t
)
644 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
645 pp_c_ws_string (this, "inline");
648 /* declaration-specifiers:
649 storage-class-specifier declaration-specifiers(opt)
650 type-specifier declaration-specifiers(opt)
651 type-qualifier declaration-specifiers(opt)
652 function-specifier declaration-specifiers(opt) */
655 c_pretty_printer::declaration_specifiers (tree t
)
657 pp_storage_class_specifier (this, t
);
658 pp_function_specifier (this, t
);
659 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
665 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
666 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
667 direct-declarator [ type-qualifier-list static assignment-expression ]
668 direct-declarator [ type-qualifier-list * ]
669 direct-declarator ( parameter-type-list )
670 direct-declarator ( identifier-list(opt) ) */
673 c_pretty_printer::direct_declarator (tree t
)
675 switch (TREE_CODE (t
))
682 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
683 pp_c_tree_decl_identifier (this, t
);
688 abstract_declarator (TREE_TYPE (t
));
692 pp_parameter_list (this, t
);
693 abstract_declarator (TREE_TYPE (t
));
697 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
698 pp_c_tree_decl_identifier (this, t
);
699 if (flags
& pp_c_flag_abstract
)
700 abstract_declarator (TREE_TYPE (t
));
703 pp_parameter_list (this, t
);
704 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
710 case FIXED_POINT_TYPE
:
717 pp_unsupported_tree (this, t
);
724 pointer(opt) direct-declarator */
727 c_pretty_printer::declarator (tree t
)
729 switch (TREE_CODE (t
))
733 case FIXED_POINT_TYPE
:
746 pp_direct_declarator (this, t
);
751 pp_unsupported_tree (this, t
);
757 declaration-specifiers init-declarator-list(opt) ; */
760 c_pretty_printer::declaration (tree t
)
762 declaration_specifiers (t
);
763 pp_c_init_declarator (this, t
);
766 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
769 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
771 if (attributes
== NULL_TREE
)
774 pp_c_ws_string (pp
, "__attribute__");
775 pp_c_left_paren (pp
);
776 pp_c_left_paren (pp
);
777 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
779 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
780 if (TREE_VALUE (attributes
))
781 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
783 if (TREE_CHAIN (attributes
))
784 pp_separate_with (pp
, ',');
786 pp_c_right_paren (pp
);
787 pp_c_right_paren (pp
);
790 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
791 marked to be displayed on disgnostic. */
794 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
796 bool is_first
= true;
801 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
803 const struct attribute_spec
*as
;
804 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
805 if (!as
|| as
->affects_type_identity
== false)
809 pp_c_ws_string (pp
, "__attribute__");
810 pp_c_left_paren (pp
);
811 pp_c_left_paren (pp
);
816 pp_separate_with (pp
, ',');
818 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
820 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
825 pp_c_right_paren (pp
);
826 pp_c_right_paren (pp
);
827 pp_c_whitespace (pp
);
831 /* function-definition:
832 declaration-specifiers declarator compound-statement */
835 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
837 pp_declaration_specifiers (pp
, t
);
838 pp_declarator (pp
, t
);
839 pp_needs_newline (pp
) = true;
840 pp
->statement (DECL_SAVED_TREE (t
));
841 pp_newline_and_flush (pp
);
847 /* Print out a c-char. This is called solely for characters which are
848 in the *target* execution character set. We ought to convert them
849 back to the *host* execution character set before printing, but we
850 have no way to do this at present. A decent compromise is to print
851 all characters as if they were in the host execution character set,
852 and not attempt to recover any named escape characters, but render
853 all unprintables as octal escapes. If the host and target character
854 sets are the same, this produces relatively readable output. If they
855 are not the same, strings may appear as gibberish, but that's okay
856 (in fact, it may well be what the reader wants, e.g. if they are looking
857 to see if conversion to the target character set happened correctly).
859 A special case: we need to prefix \, ", and ' with backslashes. It is
860 correct to do so for the *host*'s \, ", and ', because the rest of the
861 file appears in the host character set. */
864 pp_c_char (c_pretty_printer
*pp
, int c
)
870 case '\\': pp_string (pp
, "\\\\"); break;
871 case '\'': pp_string (pp
, "\\\'"); break;
872 case '\"': pp_string (pp
, "\\\""); break;
873 default: pp_character (pp
, c
);
877 pp_scalar (pp
, "\\%03o", (unsigned) c
);
880 /* Print out a STRING literal. */
883 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
885 const char *p
= TREE_STRING_POINTER (s
);
886 int n
= TREE_STRING_LENGTH (s
) - 1;
889 for (i
= 0; i
< n
; ++i
)
890 pp_c_char (pp
, p
[i
]);
894 /* Pretty-print an INTEGER literal. */
897 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
899 /* We are going to compare the type of I to other types using
900 pointer comparison so we need to use its canonical type. */
902 TYPE_CANONICAL (TREE_TYPE (i
))
903 ? TYPE_CANONICAL (TREE_TYPE (i
))
906 if (host_integerp (i
, 0))
907 pp_wide_integer (pp
, TREE_INT_CST_LOW (i
));
908 else if (host_integerp (i
, 1))
909 pp_unsigned_wide_integer (pp
, TREE_INT_CST_LOW (i
));
912 unsigned HOST_WIDE_INT low
= TREE_INT_CST_LOW (i
);
913 HOST_WIDE_INT high
= TREE_INT_CST_HIGH (i
);
914 if (tree_int_cst_sgn (i
) < 0)
920 sprintf (pp_buffer (pp
)->digit_buffer
, HOST_WIDE_INT_PRINT_DOUBLE_HEX
,
921 (unsigned HOST_WIDE_INT
) high
, (unsigned HOST_WIDE_INT
) low
);
922 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
924 if (TYPE_UNSIGNED (type
))
925 pp_character (pp
, 'u');
926 if (type
== long_integer_type_node
|| type
== long_unsigned_type_node
)
927 pp_character (pp
, 'l');
928 else if (type
== long_long_integer_type_node
929 || type
== long_long_unsigned_type_node
)
930 pp_string (pp
, "ll");
931 else if (type
== int128_integer_type_node
932 || type
== int128_unsigned_type_node
)
933 pp_string (pp
, "I128");
936 /* Print out a CHARACTER literal. */
939 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
941 tree type
= TREE_TYPE (c
);
942 if (type
== wchar_type_node
)
943 pp_character (pp
, 'L');
945 if (host_integerp (c
, TYPE_UNSIGNED (type
)))
946 pp_c_char (pp
, tree_low_cst (c
, TYPE_UNSIGNED (type
)));
948 pp_scalar (pp
, "\\x%x", (unsigned) TREE_INT_CST_LOW (c
));
952 /* Print out a BOOLEAN literal. */
955 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
957 if (b
== boolean_false_node
)
959 if (c_dialect_cxx ())
960 pp_c_ws_string (pp
, "false");
961 else if (flag_isoc99
)
962 pp_c_ws_string (pp
, "_False");
964 pp_unsupported_tree (pp
, b
);
966 else if (b
== boolean_true_node
)
968 if (c_dialect_cxx ())
969 pp_c_ws_string (pp
, "true");
970 else if (flag_isoc99
)
971 pp_c_ws_string (pp
, "_True");
973 pp_unsupported_tree (pp
, b
);
975 else if (TREE_CODE (b
) == INTEGER_CST
)
976 pp_c_integer_constant (pp
, b
);
978 pp_unsupported_tree (pp
, b
);
981 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
982 false; that means the value was obtained by a cast, in which case
983 print out the type-id part of the cast-expression -- the casted value
984 is then printed by pp_c_integer_literal. */
987 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
989 bool value_is_named
= true;
990 tree type
= TREE_TYPE (e
);
993 /* Find the name of this constant. */
994 for (value
= TYPE_VALUES (type
);
995 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
996 value
= TREE_CHAIN (value
))
999 if (value
!= NULL_TREE
)
1000 pp_id_expression (pp
, TREE_PURPOSE (value
));
1003 /* Value must have been cast. */
1004 pp_c_type_cast (pp
, type
);
1005 value_is_named
= false;
1008 return value_is_named
;
1011 /* Print out a REAL value as a decimal-floating-constant. */
1014 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1016 const struct real_format
*fmt
1017 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1019 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1020 bool is_decimal
= floating_cst
.decimal
;
1022 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1023 log10(2) to 7 significant digits. */
1024 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1026 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1027 sizeof (pp_buffer (pp
)->digit_buffer
),
1030 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1031 if (TREE_TYPE (r
) == float_type_node
)
1032 pp_character (pp
, 'f');
1033 else if (TREE_TYPE (r
) == long_double_type_node
)
1034 pp_character (pp
, 'l');
1035 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1036 pp_string (pp
, "dl");
1037 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1038 pp_string (pp
, "dd");
1039 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1040 pp_string (pp
, "df");
1043 /* Print out a FIXED value as a decimal-floating-constant. */
1046 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1048 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1049 sizeof (pp_buffer (pp
)->digit_buffer
));
1050 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1053 /* Pretty-print a compound literal expression. GNU extensions include
1054 vector constants. */
1057 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1059 tree type
= TREE_TYPE (e
);
1060 pp_c_type_cast (pp
, type
);
1062 switch (TREE_CODE (type
))
1069 pp_c_brace_enclosed_initializer_list (pp
, e
);
1073 pp_unsupported_tree (pp
, e
);
1078 /* Pretty-print a COMPLEX_EXPR expression. */
1081 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1083 /* Handle a few common special cases, otherwise fallback
1084 to printing it as compound literal. */
1085 tree type
= TREE_TYPE (e
);
1086 tree realexpr
= TREE_OPERAND (e
, 0);
1087 tree imagexpr
= TREE_OPERAND (e
, 1);
1089 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1090 if (TREE_CODE (realexpr
) == NOP_EXPR
1091 && TREE_CODE (imagexpr
) == NOP_EXPR
1092 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1093 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1094 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1095 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1096 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1097 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1099 pp_c_type_cast (pp
, type
);
1100 pp_expression (pp
, TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1104 /* Cast of an scalar expression to COMPLEX_TYPE. */
1105 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1106 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1108 pp_c_type_cast (pp
, type
);
1109 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1110 realexpr
= TREE_OPERAND (realexpr
, 0);
1111 pp_expression (pp
, realexpr
);
1115 pp_c_compound_literal (pp
, e
);
1121 fixed-point-constant
1122 enumeration-constant
1123 character-constant */
1126 c_pretty_printer::constant (tree e
)
1128 const enum tree_code code
= TREE_CODE (e
);
1134 tree type
= TREE_TYPE (e
);
1135 if (type
== boolean_type_node
)
1136 pp_c_bool_constant (this, e
);
1137 else if (type
== char_type_node
)
1138 pp_c_character_constant (this, e
);
1139 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1140 && pp_c_enumeration_constant (this, e
))
1143 pp_c_integer_constant (this, e
);
1148 pp_c_floating_constant (this, e
);
1152 pp_c_fixed_constant (this, e
);
1156 pp_c_string_literal (this, e
);
1160 /* Sometimes, we are confused and we think a complex literal
1161 is a constant. Such thing is a compound literal which
1162 grammatically belongs to postfix-expr production. */
1163 pp_c_compound_literal (this, e
);
1167 pp_unsupported_tree (this, e
);
1172 /* Pretty-print a string such as an identifier, without changing its
1173 encoding, preceded by whitespace is necessary. */
1176 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1178 pp_c_maybe_whitespace (pp
);
1179 pp_string (pp
, str
);
1180 pp
->padding
= pp_before
;
1184 c_pretty_printer::translate_string (const char *gmsgid
)
1186 if (pp_translate_identifiers (this))
1187 pp_c_ws_string (this, _(gmsgid
));
1189 pp_c_ws_string (this, gmsgid
);
1192 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1193 that need converting to the locale encoding, preceded by whitespace
1197 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1199 pp_c_maybe_whitespace (pp
);
1200 pp_identifier (pp
, id
);
1201 pp
->padding
= pp_before
;
1204 /* Pretty-print a C primary-expression.
1212 c_pretty_printer::primary_expression (tree e
)
1214 switch (TREE_CODE (e
))
1222 pp_c_tree_decl_identifier (this, e
);
1225 case IDENTIFIER_NODE
:
1226 pp_c_tree_identifier (this, e
);
1230 translate_string ("<erroneous-expression>");
1234 translate_string ("<return-value>");
1245 pp_c_ws_string (this, "__builtin_memcpy");
1246 pp_c_left_paren (this);
1247 pp_ampersand (this);
1248 primary_expression (TREE_OPERAND (e
, 0));
1249 pp_separate_with (this, ',');
1250 pp_ampersand (this);
1251 pp_initializer (this, TREE_OPERAND (e
, 1));
1252 if (TREE_OPERAND (e
, 2))
1254 pp_separate_with (this, ',');
1255 expression (TREE_OPERAND (e
, 2));
1257 pp_c_right_paren (this);
1261 /* FIXME: Make sure we won't get into an infinite loop. */
1262 pp_c_left_paren (this);
1263 pp_expression (this, e
);
1264 pp_c_right_paren (this);
1269 /* Print out a C initializer -- also support C compound-literals.
1271 assignment-expression:
1272 { initializer-list }
1273 { initializer-list , } */
1276 pp_c_initializer (c_pretty_printer
*pp
, tree e
)
1278 if (TREE_CODE (e
) == CONSTRUCTOR
)
1279 pp_c_brace_enclosed_initializer_list (pp
, e
);
1281 pp_expression (pp
, e
);
1286 declarator = initializer */
1289 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1291 pp_declarator (pp
, t
);
1292 /* We don't want to output function definitions here. There are handled
1293 elsewhere (and the syntactic form is bogus anyway). */
1294 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1296 tree init
= DECL_INITIAL (t
);
1297 /* This C++ bit is handled here because it is easier to do so.
1298 In templates, the C++ parser builds a TREE_LIST for a
1299 direct-initialization; the TREE_PURPOSE is the variable to
1300 initialize and the TREE_VALUE is the initializer. */
1301 if (TREE_CODE (init
) == TREE_LIST
)
1303 pp_c_left_paren (pp
);
1304 pp_expression (pp
, TREE_VALUE (init
));
1305 pp_right_paren (pp
);
1312 pp_c_initializer (pp
, init
);
1317 /* initializer-list:
1318 designation(opt) initializer
1319 initializer-list , designation(opt) initializer
1326 designator-list designator
1329 [ constant-expression ]
1333 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1335 tree type
= TREE_TYPE (e
);
1336 const enum tree_code code
= TREE_CODE (type
);
1338 if (TREE_CODE (e
) == CONSTRUCTOR
)
1340 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1350 tree init
= TREE_OPERAND (e
, 0);
1351 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1353 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1356 pp_primary_expression (pp
, TREE_PURPOSE (init
));
1360 pp_c_left_bracket (pp
);
1361 if (TREE_PURPOSE (init
))
1362 pp_constant (pp
, TREE_PURPOSE (init
));
1363 pp_c_right_bracket (pp
);
1365 pp_c_whitespace (pp
);
1367 pp_c_whitespace (pp
);
1368 pp_initializer (pp
, TREE_VALUE (init
));
1369 if (TREE_CHAIN (init
))
1370 pp_separate_with (pp
, ',');
1376 if (TREE_CODE (e
) == VECTOR_CST
)
1379 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1382 pp_separate_with (pp
, ',');
1383 pp_expression (pp
, VECTOR_CST_ELT (e
, i
));
1391 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1393 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1394 pp_expression (pp
, cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1395 pp_separate_with (pp
, ',');
1396 pp_expression (pp
, cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1406 pp_unsupported_tree (pp
, type
);
1409 /* Pretty-print a brace-enclosed initializer-list. */
1412 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1414 pp_c_left_brace (pp
);
1415 pp_c_initializer_list (pp
, l
);
1416 pp_c_right_brace (pp
);
1420 /* This is a convenient function, used to bridge gap between C and C++
1427 c_pretty_printer::id_expression (tree t
)
1429 switch (TREE_CODE (t
))
1438 pp_c_tree_decl_identifier (this, t
);
1441 case IDENTIFIER_NODE
:
1442 pp_c_tree_identifier (this, t
);
1446 pp_unsupported_tree (this, t
);
1451 /* postfix-expression:
1453 postfix-expression [ expression ]
1454 postfix-expression ( argument-expression-list(opt) )
1455 postfix-expression . identifier
1456 postfix-expression -> identifier
1457 postfix-expression ++
1458 postfix-expression --
1459 ( type-name ) { initializer-list }
1460 ( type-name ) { initializer-list , } */
1463 c_pretty_printer::postfix_expression (tree e
)
1465 enum tree_code code
= TREE_CODE (e
);
1468 case POSTINCREMENT_EXPR
:
1469 case POSTDECREMENT_EXPR
:
1470 postfix_expression (TREE_OPERAND (e
, 0));
1471 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1475 postfix_expression (TREE_OPERAND (e
, 0));
1476 pp_c_left_bracket (this);
1477 pp_expression (this, TREE_OPERAND (e
, 1));
1478 pp_c_right_bracket (this);
1481 case ARRAY_NOTATION_REF
:
1482 postfix_expression (ARRAY_NOTATION_ARRAY (e
));
1483 pp_c_left_bracket (this);
1484 pp_expression (this, ARRAY_NOTATION_START (e
));
1486 pp_expression (this, ARRAY_NOTATION_LENGTH (e
));
1488 pp_expression (this, ARRAY_NOTATION_STRIDE (e
));
1489 pp_c_right_bracket (this);
1494 call_expr_arg_iterator iter
;
1496 postfix_expression (CALL_EXPR_FN (e
));
1497 pp_c_left_paren (this);
1498 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1500 pp_expression (this, arg
);
1501 if (more_call_expr_args_p (&iter
))
1502 pp_separate_with (this, ',');
1504 pp_c_right_paren (this);
1508 case UNORDERED_EXPR
:
1509 pp_c_ws_string (this, flag_isoc99
1511 : "__builtin_isunordered");
1515 pp_c_ws_string (this, flag_isoc99
1517 : "!__builtin_isunordered");
1521 pp_c_ws_string (this, flag_isoc99
1523 : "!__builtin_isgreaterequal");
1527 pp_c_ws_string (this, flag_isoc99
1529 : "!__builtin_isgreater");
1533 pp_c_ws_string (this, flag_isoc99
1535 : "!__builtin_islessequal");
1539 pp_c_ws_string (this, flag_isoc99
1541 : "!__builtin_isless");
1545 pp_c_ws_string (this, flag_isoc99
1547 : "!__builtin_islessgreater");
1551 pp_c_ws_string (this, flag_isoc99
1553 : "__builtin_islessgreater");
1557 pp_c_left_paren (this);
1558 pp_expression (this, TREE_OPERAND (e
, 0));
1559 pp_separate_with (this, ',');
1560 pp_expression (this, TREE_OPERAND (e
, 1));
1561 pp_c_right_paren (this);
1565 pp_c_ws_string (this, "__builtin_abs");
1566 pp_c_left_paren (this);
1567 pp_expression (this, TREE_OPERAND (e
, 0));
1568 pp_c_right_paren (this);
1573 tree object
= TREE_OPERAND (e
, 0);
1574 if (TREE_CODE (object
) == INDIRECT_REF
)
1576 postfix_expression (TREE_OPERAND (object
, 0));
1581 postfix_expression (object
);
1584 pp_expression (this, TREE_OPERAND (e
, 1));
1590 tree type
= TREE_TYPE (e
);
1592 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1594 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1596 HOST_WIDE_INT bitpos
= tree_low_cst (TREE_OPERAND (e
, 2), 0);
1597 HOST_WIDE_INT size
= tree_low_cst (TYPE_SIZE (type
), 0);
1598 if ((bitpos
% size
) == 0)
1600 pp_c_left_paren (this);
1601 pp_c_left_paren (this);
1602 pp_type_id (this, type
);
1604 pp_c_right_paren (this);
1605 pp_c_ampersand (this);
1606 pp_expression (this, TREE_OPERAND (e
, 0));
1607 pp_c_right_paren (this);
1608 pp_c_left_bracket (this);
1609 pp_wide_integer (this, bitpos
/ size
);
1610 pp_c_right_bracket (this);
1614 pp_unsupported_tree (this, e
);
1624 pp_c_compound_literal (this, e
);
1628 pp_c_complex_expr (this, e
);
1631 case COMPOUND_LITERAL_EXPR
:
1632 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1635 pp_initializer (this, e
);
1639 pp_c_ws_string (this, "__builtin_va_arg");
1640 pp_c_left_paren (this);
1641 assignment_expression (TREE_OPERAND (e
, 0));
1642 pp_separate_with (this, ',');
1643 pp_type_id (this, TREE_TYPE (e
));
1644 pp_c_right_paren (this);
1648 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1650 id_expression (TREE_OPERAND (e
, 0));
1653 /* else fall through. */
1656 primary_expression (e
);
1661 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1664 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1666 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1668 pp_expression (pp
, TREE_VALUE (e
));
1670 pp_separate_with (pp
, ',');
1674 /* Print out V, which contains the elements of a constructor. */
1677 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1679 unsigned HOST_WIDE_INT ix
;
1682 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1684 pp_expression (pp
, value
);
1685 if (ix
!= vec_safe_length (v
) - 1)
1686 pp_separate_with (pp
, ',');
1690 /* Print out an expression-list in parens, as if it were the argument
1691 list to a function. */
1694 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1696 pp_c_left_paren (pp
);
1697 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1698 pp_c_expression_list (pp
, t
);
1699 pp_c_right_paren (pp
);
1702 /* unary-expression:
1706 unary-operator cast-expression
1707 sizeof unary-expression
1710 unary-operator: one of
1715 __alignof__ unary-expression
1716 __alignof__ ( type-id )
1717 __real__ unary-expression
1718 __imag__ unary-expression */
1721 c_pretty_printer::unary_expression (tree e
)
1723 enum tree_code code
= TREE_CODE (e
);
1726 case PREINCREMENT_EXPR
:
1727 case PREDECREMENT_EXPR
:
1728 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1729 unary_expression (TREE_OPERAND (e
, 0));
1736 case TRUTH_NOT_EXPR
:
1738 /* String literal are used by address. */
1739 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1740 pp_ampersand (this);
1741 else if (code
== INDIRECT_REF
)
1743 else if (code
== NEGATE_EXPR
)
1745 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1746 pp_complement (this);
1747 else if (code
== TRUTH_NOT_EXPR
)
1748 pp_exclamation (this);
1749 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1753 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1754 && integer_zerop (TREE_OPERAND (e
, 1)))
1755 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1759 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1761 pp_c_left_paren (this);
1762 if (!integer_onep (TYPE_SIZE_UNIT
1763 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1764 pp_c_type_cast (this, ptr_type_node
);
1766 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1767 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1770 pp_c_integer_constant (this,
1771 fold_convert (ssizetype
,
1772 TREE_OPERAND (e
, 1)));
1773 pp_c_right_paren (this);
1780 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1781 pp_c_whitespace (this);
1782 unary_expression (TREE_OPERAND (e
, 0));
1786 postfix_expression (e
);
1793 ( type-name ) cast-expression */
1796 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1798 switch (TREE_CODE (e
))
1801 case FIX_TRUNC_EXPR
:
1803 case VIEW_CONVERT_EXPR
:
1804 pp_c_type_cast (pp
, TREE_TYPE (e
));
1805 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1809 pp_unary_expression (pp
, e
);
1813 /* multiplicative-expression:
1815 multiplicative-expression * cast-expression
1816 multiplicative-expression / cast-expression
1817 multiplicative-expression % cast-expression */
1820 c_pretty_printer::multiplicative_expression (tree e
)
1822 enum tree_code code
= TREE_CODE (e
);
1826 case TRUNC_DIV_EXPR
:
1827 case TRUNC_MOD_EXPR
:
1828 multiplicative_expression (TREE_OPERAND (e
, 0));
1829 pp_c_whitespace (this);
1830 if (code
== MULT_EXPR
)
1832 else if (code
== TRUNC_DIV_EXPR
)
1836 pp_c_whitespace (this);
1837 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1841 pp_c_cast_expression (this, e
);
1846 /* additive-expression:
1847 multiplicative-expression
1848 additive-expression + multiplicative-expression
1849 additive-expression - multiplicative-expression */
1852 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1854 enum tree_code code
= TREE_CODE (e
);
1857 case POINTER_PLUS_EXPR
:
1860 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1861 pp_c_whitespace (pp
);
1862 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1866 pp_c_whitespace (pp
);
1867 pp_multiplicative_expression (pp
, TREE_OPERAND (e
, 1));
1871 pp_multiplicative_expression (pp
, e
);
1876 /* additive-expression:
1878 shift-expression << additive-expression
1879 shift-expression >> additive-expression */
1882 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1884 enum tree_code code
= TREE_CODE (e
);
1889 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1890 pp_c_whitespace (pp
);
1891 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" : ">>");
1892 pp_c_whitespace (pp
);
1893 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1897 pp_c_additive_expression (pp
, e
);
1901 /* relational-expression:
1903 relational-expression < shift-expression
1904 relational-expression > shift-expression
1905 relational-expression <= shift-expression
1906 relational-expression >= shift-expression */
1909 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1911 enum tree_code code
= TREE_CODE (e
);
1918 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1919 pp_c_whitespace (pp
);
1920 if (code
== LT_EXPR
)
1922 else if (code
== GT_EXPR
)
1924 else if (code
== LE_EXPR
)
1926 else if (code
== GE_EXPR
)
1927 pp_greater_equal (pp
);
1928 pp_c_whitespace (pp
);
1929 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1933 pp_c_shift_expression (pp
, e
);
1938 /* equality-expression:
1939 relational-expression
1940 equality-expression == relational-expression
1941 equality-equality != relational-expression */
1944 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1946 enum tree_code code
= TREE_CODE (e
);
1951 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1952 pp_c_whitespace (pp
);
1953 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1954 pp_c_whitespace (pp
);
1955 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1959 pp_c_relational_expression (pp
, e
);
1966 AND-expression & equality-equality */
1969 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1971 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1973 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1974 pp_c_whitespace (pp
);
1976 pp_c_whitespace (pp
);
1977 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1980 pp_c_equality_expression (pp
, e
);
1983 /* exclusive-OR-expression:
1985 exclusive-OR-expression ^ AND-expression */
1988 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
1990 if (TREE_CODE (e
) == BIT_XOR_EXPR
1991 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
1993 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
1994 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
1995 pp_c_maybe_whitespace (pp
);
1997 pp_c_whitespace (pp
);
1999 pp_c_whitespace (pp
);
2000 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2003 pp_c_and_expression (pp
, e
);
2006 /* inclusive-OR-expression:
2007 exclusive-OR-expression
2008 inclusive-OR-expression | exclusive-OR-expression */
2011 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2013 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2015 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2016 pp_c_whitespace (pp
);
2018 pp_c_whitespace (pp
);
2019 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2022 pp_c_exclusive_or_expression (pp
, e
);
2025 /* logical-AND-expression:
2026 inclusive-OR-expression
2027 logical-AND-expression && inclusive-OR-expression */
2030 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2032 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2033 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2035 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2036 pp_c_whitespace (pp
);
2037 pp_ampersand_ampersand (pp
);
2038 pp_c_whitespace (pp
);
2039 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2042 pp_c_inclusive_or_expression (pp
, e
);
2045 /* logical-OR-expression:
2046 logical-AND-expression
2047 logical-OR-expression || logical-AND-expression */
2050 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2052 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2053 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2055 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2056 pp_c_whitespace (pp
);
2058 pp_c_whitespace (pp
);
2059 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2062 pp_c_logical_and_expression (pp
, e
);
2065 /* conditional-expression:
2066 logical-OR-expression
2067 logical-OR-expression ? expression : conditional-expression */
2070 c_pretty_printer::conditional_expression (tree e
)
2072 if (TREE_CODE (e
) == COND_EXPR
)
2074 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2075 pp_c_whitespace (this);
2077 pp_c_whitespace (this);
2078 pp_expression (this, TREE_OPERAND (e
, 1));
2079 pp_c_whitespace (this);
2081 pp_c_whitespace (this);
2082 conditional_expression (TREE_OPERAND (e
, 2));
2085 pp_c_logical_or_expression (this, e
);
2089 /* assignment-expression:
2090 conditional-expression
2091 unary-expression assignment-operator assignment-expression
2093 assignment-expression: one of
2094 = *= /= %= += -= >>= <<= &= ^= |= */
2097 c_pretty_printer::assignment_expression (tree e
)
2099 if (TREE_CODE (e
) == MODIFY_EXPR
2100 || TREE_CODE (e
) == INIT_EXPR
)
2102 unary_expression (TREE_OPERAND (e
, 0));
2103 pp_c_whitespace (this);
2106 pp_expression (this, TREE_OPERAND (e
, 1));
2109 conditional_expression (e
);
2113 assignment-expression
2114 expression , assignment-expression
2116 Implementation note: instead of going through the usual recursion
2117 chain, I take the liberty of dispatching nodes to the appropriate
2118 functions. This makes some redundancy, but it worths it. That also
2119 prevents a possible infinite recursion between pp_primary_expression ()
2120 and pp_expression (). */
2123 c_pretty_printer::expression (tree e
)
2125 switch (TREE_CODE (e
))
2128 pp_c_integer_constant (this, e
);
2132 pp_c_floating_constant (this, e
);
2136 pp_c_fixed_constant (this, e
);
2140 pp_c_string_literal (this, e
);
2143 case IDENTIFIER_NODE
:
2152 primary_expression (e
);
2156 if (SSA_NAME_VAR (e
)
2157 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2158 expression (SSA_NAME_VAR (e
));
2160 translate_string ("<unknown>");
2163 case POSTINCREMENT_EXPR
:
2164 case POSTDECREMENT_EXPR
:
2166 case ARRAY_NOTATION_REF
:
2174 case UNORDERED_EXPR
:
2183 case COMPOUND_LITERAL_EXPR
:
2185 postfix_expression (e
);
2194 case TRUTH_NOT_EXPR
:
2195 case PREINCREMENT_EXPR
:
2196 case PREDECREMENT_EXPR
:
2199 unary_expression (e
);
2203 case FIX_TRUNC_EXPR
:
2205 case VIEW_CONVERT_EXPR
:
2206 pp_c_cast_expression (this, e
);
2210 case TRUNC_MOD_EXPR
:
2211 case TRUNC_DIV_EXPR
:
2212 multiplicative_expression (e
);
2217 pp_c_shift_expression (this, e
);
2224 pp_c_relational_expression (this, e
);
2228 pp_c_and_expression (this, e
);
2232 case TRUTH_XOR_EXPR
:
2233 pp_c_exclusive_or_expression (this, e
);
2237 pp_c_inclusive_or_expression (this, e
);
2240 case TRUTH_ANDIF_EXPR
:
2241 case TRUTH_AND_EXPR
:
2242 pp_c_logical_and_expression (this, e
);
2245 case TRUTH_ORIF_EXPR
:
2247 pp_c_logical_or_expression (this, e
);
2252 pp_c_equality_expression (this, e
);
2256 conditional_expression (e
);
2259 case POINTER_PLUS_EXPR
:
2262 pp_c_additive_expression (this, e
);
2267 assignment_expression (e
);
2271 pp_c_left_paren (this);
2272 expression (TREE_OPERAND (e
, 0));
2273 pp_separate_with (this, ',');
2274 assignment_expression (TREE_OPERAND (e
, 1));
2275 pp_c_right_paren (this);
2278 case NON_LVALUE_EXPR
:
2280 expression (TREE_OPERAND (e
, 0));
2284 postfix_expression (TREE_OPERAND (e
, 1));
2289 /* We don't yet have a way of dumping statements in a
2290 human-readable format. */
2291 pp_string (this, "({...})");
2294 case C_MAYBE_CONST_EXPR
:
2295 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2299 pp_unsupported_tree (this, e
);
2309 c_pretty_printer::statement (tree stmt
)
2314 if (pp_needs_newline (this))
2315 pp_newline_and_indent (this, 0);
2317 dump_generic_node (this, stmt
, pp_indentation (this), 0, true);
2321 /* Initialize the PRETTY-PRINTER for handling C codes. */
2323 c_pretty_printer::c_pretty_printer ()
2328 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2329 ptr_operator
= pp_c_pointer
;
2330 parameter_list
= pp_c_parameter_type_list
;
2331 type_id
= pp_c_type_id
;
2332 simple_type_specifier
= pp_c_type_specifier
;
2333 storage_class_specifier
= pp_c_storage_class_specifier
;
2335 initializer
= pp_c_initializer
;
2339 /* Print the tree T in full, on file FILE. */
2342 print_c_tree (FILE *file
, tree t
)
2344 c_pretty_printer pp
;
2346 pp_needs_newline (&pp
) = true;
2347 pp
.buffer
->stream
= file
;
2349 pp_newline_and_flush (&pp
);
2352 /* Print the tree T in full, on stderr. */
2355 debug_c_tree (tree t
)
2357 print_c_tree (stderr
, t
);
2358 fputc ('\n', stderr
);
2361 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2362 up of T's memory address. */
2365 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2369 gcc_assert (DECL_P (t
));
2372 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2375 static char xname
[8];
2376 sprintf (xname
, "<U%4x>", ((unsigned)((uintptr_t)(t
) & 0xffff)));
2380 pp_c_identifier (pp
, name
);