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 /* Translate if being used for diagnostics, but not for dump files or
34 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
36 /* The pretty-printer code is primarily designed to closely follow
37 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
38 codes we used to have in the past. Following a structured
39 approach (preferably the official grammars) is believed to make it
40 much easier to add extensions and nifty pretty-printing effects that
41 takes expression or declaration contexts into account. */
44 #define pp_c_maybe_whitespace(PP) \
46 if (pp_base (PP)->padding == pp_before) \
47 pp_c_whitespace (PP); \
51 static void pp_c_char (c_pretty_printer
*, int);
53 /* postfix-expression */
54 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
55 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
57 static void pp_c_multiplicative_expression (c_pretty_printer
*, tree
);
58 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
59 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
60 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
61 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
62 static void pp_c_and_expression (c_pretty_printer
*, tree
);
63 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
64 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
65 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
66 static void pp_c_conditional_expression (c_pretty_printer
*, tree
);
67 static void pp_c_assignment_expression (c_pretty_printer
*, tree
);
72 /* Helper functions. */
75 pp_c_whitespace (c_pretty_printer
*pp
)
78 pp_base (pp
)->padding
= pp_none
;
82 pp_c_left_paren (c_pretty_printer
*pp
)
85 pp_base (pp
)->padding
= pp_none
;
89 pp_c_right_paren (c_pretty_printer
*pp
)
92 pp_base (pp
)->padding
= pp_none
;
96 pp_c_left_brace (c_pretty_printer
*pp
)
99 pp_base (pp
)->padding
= pp_none
;
103 pp_c_right_brace (c_pretty_printer
*pp
)
106 pp_base (pp
)->padding
= pp_none
;
110 pp_c_left_bracket (c_pretty_printer
*pp
)
112 pp_left_bracket (pp
);
113 pp_base (pp
)->padding
= pp_none
;
117 pp_c_right_bracket (c_pretty_printer
*pp
)
119 pp_right_bracket (pp
);
120 pp_base (pp
)->padding
= pp_none
;
124 pp_c_dot (c_pretty_printer
*pp
)
127 pp_base (pp
)->padding
= pp_none
;
131 pp_c_ampersand (c_pretty_printer
*pp
)
134 pp_base (pp
)->padding
= pp_none
;
138 pp_c_star (c_pretty_printer
*pp
)
141 pp_base (pp
)->padding
= pp_none
;
145 pp_c_arrow (c_pretty_printer
*pp
)
148 pp_base (pp
)->padding
= pp_none
;
152 pp_c_semicolon (c_pretty_printer
*pp
)
155 pp_base (pp
)->padding
= pp_none
;
159 pp_c_complement (c_pretty_printer
*pp
)
162 pp_base (pp
)->padding
= pp_none
;
166 pp_c_exclamation (c_pretty_printer
*pp
)
169 pp_base (pp
)->padding
= pp_none
;
172 /* Print out the external representation of QUALIFIERS. */
175 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
177 const char *p
= pp_last_position_in_text (pp
);
178 bool previous
= false;
183 /* The C programming language does not have references, but it is much
184 simpler to handle those here rather than going through the same
185 logic in the C++ pretty-printer. */
186 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
187 pp_c_whitespace (pp
);
189 if (qualifiers
& TYPE_QUAL_CONST
)
191 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
195 if (qualifiers
& TYPE_QUAL_VOLATILE
)
198 pp_c_whitespace (pp
);
199 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
203 if (qualifiers
& TYPE_QUAL_RESTRICT
)
206 pp_c_whitespace (pp
);
207 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
208 ? "restrict" : "__restrict__"));
212 /* Pretty-print T using the type-cast notation '( type-name )'. */
215 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
217 pp_c_left_paren (pp
);
219 pp_c_right_paren (pp
);
222 /* We're about to pretty-print a pointer type as indicated by T.
223 Output a whitespace, if needed, preparing for subsequent output. */
226 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
228 if (POINTER_TYPE_P (t
))
230 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
231 if (TREE_CODE (pointee
) != ARRAY_TYPE
232 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
233 pp_c_whitespace (pp
);
240 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
241 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
242 of its type. Take care of possible extensions.
246 type-qualifier-list type-qualifier
251 __restrict__ -- GNU C
252 address-space-qualifier -- GNU C
255 address-space-qualifier:
256 identifier -- GNU C */
259 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
263 if (!t
|| t
== error_mark_node
)
269 qualifiers
= TYPE_QUALS (t
);
270 pp_c_cv_qualifiers (pp
, qualifiers
,
271 TREE_CODE (t
) == FUNCTION_TYPE
);
273 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
275 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
276 pp_c_identifier (pp
, as
);
281 * type-qualifier-list(opt)
282 * type-qualifier-list(opt) pointer */
285 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
287 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
289 switch (TREE_CODE (t
))
292 /* It is easier to handle C++ reference types here. */
294 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
295 pp_c_pointer (pp
, TREE_TYPE (t
));
296 if (TREE_CODE (t
) == POINTER_TYPE
)
300 pp_c_type_qualifier_list (pp
, t
);
303 /* ??? This node is now in GENERIC and so shouldn't be here. But
304 we'll fix that later. */
306 pp_declaration (pp
, DECL_EXPR_DECL (t
));
307 pp_needs_newline (pp
) = true;
311 pp_unsupported_tree (pp
, t
);
328 struct-or-union-specifier
333 simple-type-specifier:
338 pp_c_type_specifier (c_pretty_printer
*pp
, tree t
)
340 const enum tree_code code
= TREE_CODE (t
);
344 pp_c_ws_string (pp
, M_("<type-error>"));
347 case IDENTIFIER_NODE
:
348 pp_c_identifier (pp
, IDENTIFIER_POINTER (t
));
355 case FIXED_POINT_TYPE
:
359 pp_c_type_specifier (pp
, t
);
363 int prec
= TYPE_PRECISION (t
);
364 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
365 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_SATURATING (t
));
367 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_UNSIGNED (t
));
370 pp_c_type_specifier (pp
, t
);
371 if (TYPE_PRECISION (t
) != prec
)
374 pp_decimal_int (pp
, prec
);
382 pp_string (pp
, (TYPE_UNSIGNED (t
)
383 ? M_("<unnamed-unsigned:")
384 : M_("<unnamed-signed:")));
387 pp_string (pp
, M_("<unnamed-float:"));
389 case FIXED_POINT_TYPE
:
390 pp_string (pp
, M_("<unnamed-fixed:"));
395 pp_decimal_int (pp
, prec
);
403 pp_id_expression (pp
, t
);
405 pp_c_ws_string (pp
, M_("<typedef-error>"));
411 if (code
== UNION_TYPE
)
412 pp_c_ws_string (pp
, "union");
413 else if (code
== RECORD_TYPE
)
414 pp_c_ws_string (pp
, "struct");
415 else if (code
== ENUMERAL_TYPE
)
416 pp_c_ws_string (pp
, "enum");
418 pp_c_ws_string (pp
, M_("<tag-error>"));
421 pp_id_expression (pp
, TYPE_NAME (t
));
423 pp_c_ws_string (pp
, M_("<anonymous>"));
427 pp_unsupported_tree (pp
, t
);
432 /* specifier-qualifier-list:
433 type-specifier specifier-qualifier-list-opt
434 type-qualifier specifier-qualifier-list-opt
437 Implementation note: Because of the non-linearities in array or
438 function declarations, this routine prints not just the
439 specifier-qualifier-list of such entities or types of such entities,
440 but also the 'pointer' production part of their declarators. The
441 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
444 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
446 const enum tree_code code
= TREE_CODE (t
);
448 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
449 pp_c_type_qualifier_list (pp
, t
);
455 /* Get the types-specifier of this type. */
456 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
457 pp_c_specifier_qualifier_list (pp
, pointee
);
458 if (TREE_CODE (pointee
) == ARRAY_TYPE
459 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
461 pp_c_whitespace (pp
);
462 pp_c_left_paren (pp
);
463 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
465 else if (!c_dialect_cxx ())
466 pp_c_whitespace (pp
);
467 pp_ptr_operator (pp
, t
);
473 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
478 if (code
== COMPLEX_TYPE
)
479 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
480 ? "_Complex" : "__complex__"));
481 else if (code
== VECTOR_TYPE
)
483 pp_c_ws_string (pp
, "__vector");
484 pp_c_left_paren (pp
);
485 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
486 pp_c_right_paren (pp
);
487 pp_c_whitespace (pp
);
489 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
493 pp_simple_type_specifier (pp
, t
);
496 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
497 pp_c_type_qualifier_list (pp
, t
);
500 /* parameter-type-list:
505 parameter-declaration
506 parameter-list , parameter-declaration
508 parameter-declaration:
509 declaration-specifiers declarator
510 declaration-specifiers abstract-declarator(opt) */
513 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
515 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
516 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
517 pp_c_left_paren (pp
);
518 if (parms
== void_list_node
)
519 pp_c_ws_string (pp
, "void");
523 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
526 pp_separate_with (pp
, ',');
528 pp_declaration_specifiers
529 (pp
, want_parm_decl
? parms
: TREE_VALUE (parms
));
531 pp_declarator (pp
, parms
);
533 pp_abstract_declarator (pp
, TREE_VALUE (parms
));
536 pp_c_right_paren (pp
);
539 /* abstract-declarator:
541 pointer(opt) direct-abstract-declarator */
544 pp_c_abstract_declarator (c_pretty_printer
*pp
, tree t
)
546 if (TREE_CODE (t
) == POINTER_TYPE
)
548 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
549 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
550 pp_c_right_paren (pp
);
554 pp_direct_abstract_declarator (pp
, t
);
557 /* direct-abstract-declarator:
558 ( abstract-declarator )
559 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
560 direct-abstract-declarator(opt) [ * ]
561 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
564 pp_c_direct_abstract_declarator (c_pretty_printer
*pp
, tree t
)
566 switch (TREE_CODE (t
))
569 pp_abstract_declarator (pp
, t
);
573 pp_c_parameter_type_list (pp
, t
);
574 pp_direct_abstract_declarator (pp
, TREE_TYPE (t
));
578 pp_c_left_bracket (pp
);
579 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
581 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
582 tree type
= TREE_TYPE (maxval
);
584 if (host_integerp (maxval
, 0))
585 pp_wide_integer (pp
, tree_low_cst (maxval
, 0) + 1);
587 pp_expression (pp
, fold_build2 (PLUS_EXPR
, type
, maxval
,
588 build_int_cst (type
, 1)));
590 pp_c_right_bracket (pp
);
591 pp_direct_abstract_declarator (pp
, TREE_TYPE (t
));
594 case IDENTIFIER_NODE
:
599 case FIXED_POINT_TYPE
:
609 pp_unsupported_tree (pp
, t
);
615 specifier-qualifier-list abstract-declarator(opt) */
618 pp_c_type_id (c_pretty_printer
*pp
, tree t
)
620 pp_c_specifier_qualifier_list (pp
, t
);
621 pp_abstract_declarator (pp
, t
);
624 /* storage-class-specifier:
632 pp_c_storage_class_specifier (c_pretty_printer
*pp
, tree t
)
634 if (TREE_CODE (t
) == TYPE_DECL
)
635 pp_c_ws_string (pp
, "typedef");
638 if (DECL_REGISTER (t
))
639 pp_c_ws_string (pp
, "register");
640 else if (TREE_STATIC (t
) && TREE_CODE (t
) == VAR_DECL
)
641 pp_c_ws_string (pp
, "static");
645 /* function-specifier:
649 pp_c_function_specifier (c_pretty_printer
*pp
, tree t
)
651 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
652 pp_c_ws_string (pp
, "inline");
655 /* declaration-specifiers:
656 storage-class-specifier declaration-specifiers(opt)
657 type-specifier declaration-specifiers(opt)
658 type-qualifier declaration-specifiers(opt)
659 function-specifier declaration-specifiers(opt) */
662 pp_c_declaration_specifiers (c_pretty_printer
*pp
, tree t
)
664 pp_storage_class_specifier (pp
, t
);
665 pp_function_specifier (pp
, t
);
666 pp_c_specifier_qualifier_list (pp
, DECL_P (t
) ? TREE_TYPE (t
) : t
);
672 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
673 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
674 direct-declarator [ type-qualifier-list static assignment-expression ]
675 direct-declarator [ type-qualifier-list * ]
676 direct-declarator ( parameter-type-list )
677 direct-declarator ( identifier-list(opt) ) */
680 pp_c_direct_declarator (c_pretty_printer
*pp
, tree t
)
682 switch (TREE_CODE (t
))
689 pp_c_space_for_pointer_operator (pp
, TREE_TYPE (t
));
690 pp_c_tree_decl_identifier (pp
, t
);
695 pp_abstract_declarator (pp
, TREE_TYPE (t
));
699 pp_parameter_list (pp
, t
);
700 pp_abstract_declarator (pp
, TREE_TYPE (t
));
704 pp_c_space_for_pointer_operator (pp
, TREE_TYPE (TREE_TYPE (t
)));
705 pp_c_tree_decl_identifier (pp
, t
);
706 if (pp_c_base (pp
)->flags
& pp_c_flag_abstract
)
707 pp_abstract_declarator (pp
, TREE_TYPE (t
));
710 pp_parameter_list (pp
, t
);
711 pp_abstract_declarator (pp
, TREE_TYPE (TREE_TYPE (t
)));
717 case FIXED_POINT_TYPE
:
724 pp_unsupported_tree (pp
, t
);
731 pointer(opt) direct-declarator */
734 pp_c_declarator (c_pretty_printer
*pp
, tree t
)
736 switch (TREE_CODE (t
))
740 case FIXED_POINT_TYPE
:
753 pp_direct_declarator (pp
, t
);
758 pp_unsupported_tree (pp
, t
);
764 declaration-specifiers init-declarator-list(opt) ; */
767 pp_c_declaration (c_pretty_printer
*pp
, tree t
)
769 pp_declaration_specifiers (pp
, t
);
770 pp_c_init_declarator (pp
, t
);
773 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
776 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
778 if (attributes
== NULL_TREE
)
781 pp_c_ws_string (pp
, "__attribute__");
782 pp_c_left_paren (pp
);
783 pp_c_left_paren (pp
);
784 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
786 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
787 if (TREE_VALUE (attributes
))
788 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
790 if (TREE_CHAIN (attributes
))
791 pp_separate_with (pp
, ',');
793 pp_c_right_paren (pp
);
794 pp_c_right_paren (pp
);
797 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
798 marked to be displayed on disgnostic. */
801 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
803 bool is_first
= true;
808 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
810 const struct attribute_spec
*as
;
811 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
812 if (!as
|| as
->affects_type_identity
== false)
816 pp_c_ws_string (pp
, "__attribute__");
817 pp_c_left_paren (pp
);
818 pp_c_left_paren (pp
);
823 pp_separate_with (pp
, ',');
825 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
827 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
832 pp_c_right_paren (pp
);
833 pp_c_right_paren (pp
);
834 pp_c_whitespace (pp
);
838 /* function-definition:
839 declaration-specifiers declarator compound-statement */
842 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
844 pp_declaration_specifiers (pp
, t
);
845 pp_declarator (pp
, t
);
846 pp_needs_newline (pp
) = true;
847 pp_statement (pp
, DECL_SAVED_TREE (t
));
848 pp_newline_and_flush (pp
);
854 /* Print out a c-char. This is called solely for characters which are
855 in the *target* execution character set. We ought to convert them
856 back to the *host* execution character set before printing, but we
857 have no way to do this at present. A decent compromise is to print
858 all characters as if they were in the host execution character set,
859 and not attempt to recover any named escape characters, but render
860 all unprintables as octal escapes. If the host and target character
861 sets are the same, this produces relatively readable output. If they
862 are not the same, strings may appear as gibberish, but that's okay
863 (in fact, it may well be what the reader wants, e.g. if they are looking
864 to see if conversion to the target character set happened correctly).
866 A special case: we need to prefix \, ", and ' with backslashes. It is
867 correct to do so for the *host*'s \, ", and ', because the rest of the
868 file appears in the host character set. */
871 pp_c_char (c_pretty_printer
*pp
, int c
)
877 case '\\': pp_string (pp
, "\\\\"); break;
878 case '\'': pp_string (pp
, "\\\'"); break;
879 case '\"': pp_string (pp
, "\\\""); break;
880 default: pp_character (pp
, c
);
884 pp_scalar (pp
, "\\%03o", (unsigned) c
);
887 /* Print out a STRING literal. */
890 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
892 const char *p
= TREE_STRING_POINTER (s
);
893 int n
= TREE_STRING_LENGTH (s
) - 1;
896 for (i
= 0; i
< n
; ++i
)
897 pp_c_char (pp
, p
[i
]);
901 /* Pretty-print an INTEGER literal. */
904 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
906 /* We are going to compare the type of I to other types using
907 pointer comparison so we need to use its canonical type. */
909 TYPE_CANONICAL (TREE_TYPE (i
))
910 ? TYPE_CANONICAL (TREE_TYPE (i
))
913 if (host_integerp (i
, 0))
914 pp_wide_integer (pp
, TREE_INT_CST_LOW (i
));
915 else if (host_integerp (i
, 1))
916 pp_unsigned_wide_integer (pp
, TREE_INT_CST_LOW (i
));
919 unsigned HOST_WIDE_INT low
= TREE_INT_CST_LOW (i
);
920 HOST_WIDE_INT high
= TREE_INT_CST_HIGH (i
);
921 if (tree_int_cst_sgn (i
) < 0)
923 pp_character (pp
, '-');
927 sprintf (pp_buffer (pp
)->digit_buffer
, HOST_WIDE_INT_PRINT_DOUBLE_HEX
,
928 (unsigned HOST_WIDE_INT
) high
, (unsigned HOST_WIDE_INT
) low
);
929 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
931 if (TYPE_UNSIGNED (type
))
932 pp_character (pp
, 'u');
933 if (type
== long_integer_type_node
|| type
== long_unsigned_type_node
)
934 pp_character (pp
, 'l');
935 else if (type
== long_long_integer_type_node
936 || type
== long_long_unsigned_type_node
)
937 pp_string (pp
, "ll");
938 else if (type
== int128_integer_type_node
939 || type
== int128_unsigned_type_node
)
940 pp_string (pp
, "I128");
943 /* Print out a CHARACTER literal. */
946 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
948 tree type
= TREE_TYPE (c
);
949 if (type
== wchar_type_node
)
950 pp_character (pp
, 'L');
952 if (host_integerp (c
, TYPE_UNSIGNED (type
)))
953 pp_c_char (pp
, tree_low_cst (c
, TYPE_UNSIGNED (type
)));
955 pp_scalar (pp
, "\\x%x", (unsigned) TREE_INT_CST_LOW (c
));
959 /* Print out a BOOLEAN literal. */
962 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
964 if (b
== boolean_false_node
)
966 if (c_dialect_cxx ())
967 pp_c_ws_string (pp
, "false");
968 else if (flag_isoc99
)
969 pp_c_ws_string (pp
, "_False");
971 pp_unsupported_tree (pp
, b
);
973 else if (b
== boolean_true_node
)
975 if (c_dialect_cxx ())
976 pp_c_ws_string (pp
, "true");
977 else if (flag_isoc99
)
978 pp_c_ws_string (pp
, "_True");
980 pp_unsupported_tree (pp
, b
);
982 else if (TREE_CODE (b
) == INTEGER_CST
)
983 pp_c_integer_constant (pp
, b
);
985 pp_unsupported_tree (pp
, b
);
988 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
989 false; that means the value was obtained by a cast, in which case
990 print out the type-id part of the cast-expression -- the casted value
991 is then printed by pp_c_integer_literal. */
994 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
996 bool value_is_named
= true;
997 tree type
= TREE_TYPE (e
);
1000 /* Find the name of this constant. */
1001 for (value
= TYPE_VALUES (type
);
1002 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
1003 value
= TREE_CHAIN (value
))
1006 if (value
!= NULL_TREE
)
1007 pp_id_expression (pp
, TREE_PURPOSE (value
));
1010 /* Value must have been cast. */
1011 pp_c_type_cast (pp
, type
);
1012 value_is_named
= false;
1015 return value_is_named
;
1018 /* Print out a REAL value as a decimal-floating-constant. */
1021 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1023 const struct real_format
*fmt
1024 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1026 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1027 bool is_decimal
= floating_cst
.decimal
;
1029 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1030 log10(2) to 7 significant digits. */
1031 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1033 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1034 sizeof (pp_buffer (pp
)->digit_buffer
),
1037 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1038 if (TREE_TYPE (r
) == float_type_node
)
1039 pp_character (pp
, 'f');
1040 else if (TREE_TYPE (r
) == long_double_type_node
)
1041 pp_character (pp
, 'l');
1042 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1043 pp_string (pp
, "dl");
1044 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1045 pp_string (pp
, "dd");
1046 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1047 pp_string (pp
, "df");
1050 /* Print out a FIXED value as a decimal-floating-constant. */
1053 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1055 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1056 sizeof (pp_buffer (pp
)->digit_buffer
));
1057 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1060 /* Pretty-print a compound literal expression. GNU extensions include
1061 vector constants. */
1064 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1066 tree type
= TREE_TYPE (e
);
1067 pp_c_type_cast (pp
, type
);
1069 switch (TREE_CODE (type
))
1076 pp_c_brace_enclosed_initializer_list (pp
, e
);
1080 pp_unsupported_tree (pp
, e
);
1085 /* Pretty-print a COMPLEX_EXPR expression. */
1088 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1090 /* Handle a few common special cases, otherwise fallback
1091 to printing it as compound literal. */
1092 tree type
= TREE_TYPE (e
);
1093 tree realexpr
= TREE_OPERAND (e
, 0);
1094 tree imagexpr
= TREE_OPERAND (e
, 1);
1096 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1097 if (TREE_CODE (realexpr
) == NOP_EXPR
1098 && TREE_CODE (imagexpr
) == NOP_EXPR
1099 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1100 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1101 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1102 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1103 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1104 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1106 pp_c_type_cast (pp
, type
);
1107 pp_expression (pp
, TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1111 /* Cast of an scalar expression to COMPLEX_TYPE. */
1112 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1113 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1115 pp_c_type_cast (pp
, type
);
1116 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1117 realexpr
= TREE_OPERAND (realexpr
, 0);
1118 pp_expression (pp
, realexpr
);
1122 pp_c_compound_literal (pp
, e
);
1128 fixed-point-constant
1129 enumeration-constant
1130 character-constant */
1133 pp_c_constant (c_pretty_printer
*pp
, tree e
)
1135 const enum tree_code code
= TREE_CODE (e
);
1141 tree type
= TREE_TYPE (e
);
1142 if (type
== boolean_type_node
)
1143 pp_c_bool_constant (pp
, e
);
1144 else if (type
== char_type_node
)
1145 pp_c_character_constant (pp
, e
);
1146 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1147 && pp_c_enumeration_constant (pp
, e
))
1150 pp_c_integer_constant (pp
, e
);
1155 pp_c_floating_constant (pp
, e
);
1159 pp_c_fixed_constant (pp
, e
);
1163 pp_c_string_literal (pp
, e
);
1167 /* Sometimes, we are confused and we think a complex literal
1168 is a constant. Such thing is a compound literal which
1169 grammatically belongs to postfix-expr production. */
1170 pp_c_compound_literal (pp
, e
);
1174 pp_unsupported_tree (pp
, e
);
1179 /* Pretty-print a string such as an identifier, without changing its
1180 encoding, preceded by whitespace is necessary. */
1183 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1185 pp_c_maybe_whitespace (pp
);
1186 pp_string (pp
, str
);
1187 pp_base (pp
)->padding
= pp_before
;
1190 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1191 that need converting to the locale encoding, preceded by whitespace
1195 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1197 pp_c_maybe_whitespace (pp
);
1198 pp_identifier (pp
, id
);
1199 pp_base (pp
)->padding
= pp_before
;
1202 /* Pretty-print a C primary-expression.
1210 pp_c_primary_expression (c_pretty_printer
*pp
, tree e
)
1212 switch (TREE_CODE (e
))
1220 pp_c_tree_decl_identifier (pp
, e
);
1223 case IDENTIFIER_NODE
:
1224 pp_c_tree_identifier (pp
, e
);
1228 pp_c_ws_string (pp
, M_("<erroneous-expression>"));
1232 pp_c_ws_string (pp
, M_("<return-value>"));
1239 pp_c_constant (pp
, e
);
1243 pp_c_ws_string (pp
, "__builtin_memcpy");
1244 pp_c_left_paren (pp
);
1246 pp_primary_expression (pp
, TREE_OPERAND (e
, 0));
1247 pp_separate_with (pp
, ',');
1249 pp_initializer (pp
, TREE_OPERAND (e
, 1));
1250 if (TREE_OPERAND (e
, 2))
1252 pp_separate_with (pp
, ',');
1253 pp_c_expression (pp
, TREE_OPERAND (e
, 2));
1255 pp_c_right_paren (pp
);
1259 /* FIXME: Make sure we won't get into an infinite loop. */
1260 pp_c_left_paren (pp
);
1261 pp_expression (pp
, e
);
1262 pp_c_right_paren (pp
);
1267 /* Print out a C initializer -- also support C compound-literals.
1269 assignment-expression:
1270 { initializer-list }
1271 { initializer-list , } */
1274 pp_c_initializer (c_pretty_printer
*pp
, tree e
)
1276 if (TREE_CODE (e
) == CONSTRUCTOR
)
1277 pp_c_brace_enclosed_initializer_list (pp
, e
);
1279 pp_expression (pp
, e
);
1284 declarator = initializer */
1287 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1289 pp_declarator (pp
, t
);
1290 /* We don't want to output function definitions here. There are handled
1291 elsewhere (and the syntactic form is bogus anyway). */
1292 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1294 tree init
= DECL_INITIAL (t
);
1295 /* This C++ bit is handled here because it is easier to do so.
1296 In templates, the C++ parser builds a TREE_LIST for a
1297 direct-initialization; the TREE_PURPOSE is the variable to
1298 initialize and the TREE_VALUE is the initializer. */
1299 if (TREE_CODE (init
) == TREE_LIST
)
1301 pp_c_left_paren (pp
);
1302 pp_expression (pp
, TREE_VALUE (init
));
1303 pp_right_paren (pp
);
1310 pp_c_initializer (pp
, init
);
1315 /* initializer-list:
1316 designation(opt) initializer
1317 initializer-list , designation(opt) initializer
1324 designator-list designator
1327 [ constant-expression ]
1331 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1333 tree type
= TREE_TYPE (e
);
1334 const enum tree_code code
= TREE_CODE (type
);
1336 if (TREE_CODE (e
) == CONSTRUCTOR
)
1338 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1348 tree init
= TREE_OPERAND (e
, 0);
1349 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1351 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1354 pp_c_primary_expression (pp
, TREE_PURPOSE (init
));
1358 pp_c_left_bracket (pp
);
1359 if (TREE_PURPOSE (init
))
1360 pp_c_constant (pp
, TREE_PURPOSE (init
));
1361 pp_c_right_bracket (pp
);
1363 pp_c_whitespace (pp
);
1365 pp_c_whitespace (pp
);
1366 pp_initializer (pp
, TREE_VALUE (init
));
1367 if (TREE_CHAIN (init
))
1368 pp_separate_with (pp
, ',');
1374 if (TREE_CODE (e
) == VECTOR_CST
)
1377 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1380 pp_separate_with (pp
, ',');
1381 pp_expression (pp
, VECTOR_CST_ELT (e
, i
));
1389 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1391 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1392 pp_expression (pp
, cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1393 pp_separate_with (pp
, ',');
1394 pp_expression (pp
, cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1404 pp_unsupported_tree (pp
, type
);
1407 /* Pretty-print a brace-enclosed initializer-list. */
1410 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1412 pp_c_left_brace (pp
);
1413 pp_c_initializer_list (pp
, l
);
1414 pp_c_right_brace (pp
);
1418 /* This is a convenient function, used to bridge gap between C and C++
1425 pp_c_id_expression (c_pretty_printer
*pp
, tree t
)
1427 switch (TREE_CODE (t
))
1436 pp_c_tree_decl_identifier (pp
, t
);
1439 case IDENTIFIER_NODE
:
1440 pp_c_tree_identifier (pp
, t
);
1444 pp_unsupported_tree (pp
, t
);
1449 /* postfix-expression:
1451 postfix-expression [ expression ]
1452 postfix-expression ( argument-expression-list(opt) )
1453 postfix-expression . identifier
1454 postfix-expression -> identifier
1455 postfix-expression ++
1456 postfix-expression --
1457 ( type-name ) { initializer-list }
1458 ( type-name ) { initializer-list , } */
1461 pp_c_postfix_expression (c_pretty_printer
*pp
, tree e
)
1463 enum tree_code code
= TREE_CODE (e
);
1466 case POSTINCREMENT_EXPR
:
1467 case POSTDECREMENT_EXPR
:
1468 pp_postfix_expression (pp
, TREE_OPERAND (e
, 0));
1469 pp_string (pp
, code
== POSTINCREMENT_EXPR
? "++" : "--");
1473 pp_postfix_expression (pp
, TREE_OPERAND (e
, 0));
1474 pp_c_left_bracket (pp
);
1475 pp_expression (pp
, TREE_OPERAND (e
, 1));
1476 pp_c_right_bracket (pp
);
1481 call_expr_arg_iterator iter
;
1483 pp_postfix_expression (pp
, CALL_EXPR_FN (e
));
1484 pp_c_left_paren (pp
);
1485 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1487 pp_expression (pp
, arg
);
1488 if (more_call_expr_args_p (&iter
))
1489 pp_separate_with (pp
, ',');
1491 pp_c_right_paren (pp
);
1495 case UNORDERED_EXPR
:
1496 pp_c_ws_string (pp
, flag_isoc99
1498 : "__builtin_isunordered");
1502 pp_c_ws_string (pp
, flag_isoc99
1504 : "!__builtin_isunordered");
1508 pp_c_ws_string (pp
, flag_isoc99
1510 : "!__builtin_isgreaterequal");
1514 pp_c_ws_string (pp
, flag_isoc99
1516 : "!__builtin_isgreater");
1520 pp_c_ws_string (pp
, flag_isoc99
1522 : "!__builtin_islessequal");
1526 pp_c_ws_string (pp
, flag_isoc99
1528 : "!__builtin_isless");
1532 pp_c_ws_string (pp
, flag_isoc99
1534 : "!__builtin_islessgreater");
1538 pp_c_ws_string (pp
, flag_isoc99
1540 : "__builtin_islessgreater");
1544 pp_c_left_paren (pp
);
1545 pp_expression (pp
, TREE_OPERAND (e
, 0));
1546 pp_separate_with (pp
, ',');
1547 pp_expression (pp
, TREE_OPERAND (e
, 1));
1548 pp_c_right_paren (pp
);
1552 pp_c_ws_string (pp
, "__builtin_abs");
1553 pp_c_left_paren (pp
);
1554 pp_expression (pp
, TREE_OPERAND (e
, 0));
1555 pp_c_right_paren (pp
);
1560 tree object
= TREE_OPERAND (e
, 0);
1561 if (TREE_CODE (object
) == INDIRECT_REF
)
1563 pp_postfix_expression (pp
, TREE_OPERAND (object
, 0));
1568 pp_postfix_expression (pp
, object
);
1571 pp_expression (pp
, TREE_OPERAND (e
, 1));
1577 tree type
= TREE_TYPE (e
);
1579 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1581 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1583 HOST_WIDE_INT bitpos
= tree_low_cst (TREE_OPERAND (e
, 2), 0);
1584 HOST_WIDE_INT size
= tree_low_cst (TYPE_SIZE (type
), 0);
1585 if ((bitpos
% size
) == 0)
1587 pp_c_left_paren (pp
);
1588 pp_c_left_paren (pp
);
1589 pp_type_id (pp
, type
);
1591 pp_c_right_paren (pp
);
1592 pp_c_ampersand (pp
);
1593 pp_expression (pp
, TREE_OPERAND (e
, 0));
1594 pp_c_right_paren (pp
);
1595 pp_c_left_bracket (pp
);
1596 pp_wide_integer (pp
, bitpos
/ size
);
1597 pp_c_right_bracket (pp
);
1601 pp_unsupported_tree (pp
, e
);
1606 pp_c_expression (pp
, e
);
1611 pp_c_compound_literal (pp
, e
);
1615 pp_c_complex_expr (pp
, e
);
1618 case COMPOUND_LITERAL_EXPR
:
1619 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1622 pp_initializer (pp
, e
);
1626 pp_c_ws_string (pp
, "__builtin_va_arg");
1627 pp_c_left_paren (pp
);
1628 pp_assignment_expression (pp
, TREE_OPERAND (e
, 0));
1629 pp_separate_with (pp
, ',');
1630 pp_type_id (pp
, TREE_TYPE (e
));
1631 pp_c_right_paren (pp
);
1635 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1637 pp_c_id_expression (pp
, TREE_OPERAND (e
, 0));
1640 /* else fall through. */
1643 pp_primary_expression (pp
, e
);
1648 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1651 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1653 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1655 pp_expression (pp
, TREE_VALUE (e
));
1657 pp_separate_with (pp
, ',');
1661 /* Print out V, which contains the elements of a constructor. */
1664 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1666 unsigned HOST_WIDE_INT ix
;
1669 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1671 pp_expression (pp
, value
);
1672 if (ix
!= vec_safe_length (v
) - 1)
1673 pp_separate_with (pp
, ',');
1677 /* Print out an expression-list in parens, as if it were the argument
1678 list to a function. */
1681 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1683 pp_c_left_paren (pp
);
1684 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1685 pp_c_expression_list (pp
, t
);
1686 pp_c_right_paren (pp
);
1689 /* unary-expression:
1693 unary-operator cast-expression
1694 sizeof unary-expression
1697 unary-operator: one of
1702 __alignof__ unary-expression
1703 __alignof__ ( type-id )
1704 __real__ unary-expression
1705 __imag__ unary-expression */
1708 pp_c_unary_expression (c_pretty_printer
*pp
, tree e
)
1710 enum tree_code code
= TREE_CODE (e
);
1713 case PREINCREMENT_EXPR
:
1714 case PREDECREMENT_EXPR
:
1715 pp_string (pp
, code
== PREINCREMENT_EXPR
? "++" : "--");
1716 pp_c_unary_expression (pp
, TREE_OPERAND (e
, 0));
1723 case TRUTH_NOT_EXPR
:
1725 /* String literal are used by address. */
1726 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1728 else if (code
== INDIRECT_REF
)
1730 else if (code
== NEGATE_EXPR
)
1732 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1734 else if (code
== TRUTH_NOT_EXPR
)
1735 pp_exclamation (pp
);
1736 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1740 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1741 && integer_zerop (TREE_OPERAND (e
, 1)))
1742 pp_c_expression (pp
, TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1746 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1748 pp_c_left_paren (pp
);
1749 if (!integer_onep (TYPE_SIZE_UNIT
1750 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1751 pp_c_type_cast (pp
, ptr_type_node
);
1753 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1754 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1757 pp_c_integer_constant (pp
,
1758 fold_convert (ssizetype
,
1759 TREE_OPERAND (e
, 1)));
1760 pp_c_right_paren (pp
);
1767 pp_c_ws_string (pp
, code
== REALPART_EXPR
? "__real__" : "__imag__");
1768 pp_c_whitespace (pp
);
1769 pp_unary_expression (pp
, TREE_OPERAND (e
, 0));
1773 pp_postfix_expression (pp
, e
);
1780 ( type-name ) cast-expression */
1783 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1785 switch (TREE_CODE (e
))
1788 case FIX_TRUNC_EXPR
:
1790 case VIEW_CONVERT_EXPR
:
1791 pp_c_type_cast (pp
, TREE_TYPE (e
));
1792 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1796 pp_unary_expression (pp
, e
);
1800 /* multiplicative-expression:
1802 multiplicative-expression * cast-expression
1803 multiplicative-expression / cast-expression
1804 multiplicative-expression % cast-expression */
1807 pp_c_multiplicative_expression (c_pretty_printer
*pp
, tree e
)
1809 enum tree_code code
= TREE_CODE (e
);
1813 case TRUNC_DIV_EXPR
:
1814 case TRUNC_MOD_EXPR
:
1815 pp_multiplicative_expression (pp
, TREE_OPERAND (e
, 0));
1816 pp_c_whitespace (pp
);
1817 if (code
== MULT_EXPR
)
1819 else if (code
== TRUNC_DIV_EXPR
)
1823 pp_c_whitespace (pp
);
1824 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 1));
1828 pp_c_cast_expression (pp
, e
);
1833 /* additive-expression:
1834 multiplicative-expression
1835 additive-expression + multiplicative-expression
1836 additive-expression - multiplicative-expression */
1839 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1841 enum tree_code code
= TREE_CODE (e
);
1844 case POINTER_PLUS_EXPR
:
1847 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1848 pp_c_whitespace (pp
);
1849 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1853 pp_c_whitespace (pp
);
1854 pp_multiplicative_expression (pp
, TREE_OPERAND (e
, 1));
1858 pp_multiplicative_expression (pp
, e
);
1863 /* additive-expression:
1865 shift-expression << additive-expression
1866 shift-expression >> additive-expression */
1869 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1871 enum tree_code code
= TREE_CODE (e
);
1876 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1877 pp_c_whitespace (pp
);
1878 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" : ">>");
1879 pp_c_whitespace (pp
);
1880 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1884 pp_c_additive_expression (pp
, e
);
1888 /* relational-expression:
1890 relational-expression < shift-expression
1891 relational-expression > shift-expression
1892 relational-expression <= shift-expression
1893 relational-expression >= shift-expression */
1896 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1898 enum tree_code code
= TREE_CODE (e
);
1905 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1906 pp_c_whitespace (pp
);
1907 if (code
== LT_EXPR
)
1909 else if (code
== GT_EXPR
)
1911 else if (code
== LE_EXPR
)
1912 pp_string (pp
, "<=");
1913 else if (code
== GE_EXPR
)
1914 pp_string (pp
, ">=");
1915 pp_c_whitespace (pp
);
1916 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1920 pp_c_shift_expression (pp
, e
);
1925 /* equality-expression:
1926 relational-expression
1927 equality-expression == relational-expression
1928 equality-equality != relational-expression */
1931 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1933 enum tree_code code
= TREE_CODE (e
);
1938 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1939 pp_c_whitespace (pp
);
1940 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1941 pp_c_whitespace (pp
);
1942 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1946 pp_c_relational_expression (pp
, e
);
1953 AND-expression & equality-equality */
1956 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1958 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1960 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1961 pp_c_whitespace (pp
);
1963 pp_c_whitespace (pp
);
1964 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1967 pp_c_equality_expression (pp
, e
);
1970 /* exclusive-OR-expression:
1972 exclusive-OR-expression ^ AND-expression */
1975 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
1977 if (TREE_CODE (e
) == BIT_XOR_EXPR
1978 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
1980 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
1981 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
1982 pp_c_maybe_whitespace (pp
);
1984 pp_c_whitespace (pp
);
1986 pp_c_whitespace (pp
);
1987 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
1990 pp_c_and_expression (pp
, e
);
1993 /* inclusive-OR-expression:
1994 exclusive-OR-expression
1995 inclusive-OR-expression | exclusive-OR-expression */
1998 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2000 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2002 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2003 pp_c_whitespace (pp
);
2005 pp_c_whitespace (pp
);
2006 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2009 pp_c_exclusive_or_expression (pp
, e
);
2012 /* logical-AND-expression:
2013 inclusive-OR-expression
2014 logical-AND-expression && inclusive-OR-expression */
2017 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2019 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2020 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2022 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2023 pp_c_whitespace (pp
);
2024 pp_string (pp
, "&&");
2025 pp_c_whitespace (pp
);
2026 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2029 pp_c_inclusive_or_expression (pp
, e
);
2032 /* logical-OR-expression:
2033 logical-AND-expression
2034 logical-OR-expression || logical-AND-expression */
2037 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2039 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2040 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2042 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2043 pp_c_whitespace (pp
);
2044 pp_string (pp
, "||");
2045 pp_c_whitespace (pp
);
2046 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2049 pp_c_logical_and_expression (pp
, e
);
2052 /* conditional-expression:
2053 logical-OR-expression
2054 logical-OR-expression ? expression : conditional-expression */
2057 pp_c_conditional_expression (c_pretty_printer
*pp
, tree e
)
2059 if (TREE_CODE (e
) == COND_EXPR
)
2061 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2062 pp_c_whitespace (pp
);
2064 pp_c_whitespace (pp
);
2065 pp_expression (pp
, TREE_OPERAND (e
, 1));
2066 pp_c_whitespace (pp
);
2068 pp_c_whitespace (pp
);
2069 pp_c_conditional_expression (pp
, TREE_OPERAND (e
, 2));
2072 pp_c_logical_or_expression (pp
, e
);
2076 /* assignment-expression:
2077 conditional-expression
2078 unary-expression assignment-operator assignment-expression
2080 assignment-expression: one of
2081 = *= /= %= += -= >>= <<= &= ^= |= */
2084 pp_c_assignment_expression (c_pretty_printer
*pp
, tree e
)
2086 if (TREE_CODE (e
) == MODIFY_EXPR
2087 || TREE_CODE (e
) == INIT_EXPR
)
2089 pp_c_unary_expression (pp
, TREE_OPERAND (e
, 0));
2090 pp_c_whitespace (pp
);
2093 pp_c_expression (pp
, TREE_OPERAND (e
, 1));
2096 pp_c_conditional_expression (pp
, e
);
2100 assignment-expression
2101 expression , assignment-expression
2103 Implementation note: instead of going through the usual recursion
2104 chain, I take the liberty of dispatching nodes to the appropriate
2105 functions. This makes some redundancy, but it worths it. That also
2106 prevents a possible infinite recursion between pp_c_primary_expression ()
2107 and pp_c_expression (). */
2110 pp_c_expression (c_pretty_printer
*pp
, tree e
)
2112 switch (TREE_CODE (e
))
2115 pp_c_integer_constant (pp
, e
);
2119 pp_c_floating_constant (pp
, e
);
2123 pp_c_fixed_constant (pp
, e
);
2127 pp_c_string_literal (pp
, e
);
2130 case IDENTIFIER_NODE
:
2139 pp_primary_expression (pp
, e
);
2143 if (SSA_NAME_VAR (e
)
2144 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2145 pp_c_expression (pp
, SSA_NAME_VAR (e
));
2147 pp_c_ws_string (pp
, M_("<unknown>"));
2150 case POSTINCREMENT_EXPR
:
2151 case POSTDECREMENT_EXPR
:
2160 case UNORDERED_EXPR
:
2169 case COMPOUND_LITERAL_EXPR
:
2171 pp_postfix_expression (pp
, e
);
2180 case TRUTH_NOT_EXPR
:
2181 case PREINCREMENT_EXPR
:
2182 case PREDECREMENT_EXPR
:
2185 pp_c_unary_expression (pp
, e
);
2189 case FIX_TRUNC_EXPR
:
2191 case VIEW_CONVERT_EXPR
:
2192 pp_c_cast_expression (pp
, e
);
2196 case TRUNC_MOD_EXPR
:
2197 case TRUNC_DIV_EXPR
:
2198 pp_multiplicative_expression (pp
, e
);
2203 pp_c_shift_expression (pp
, e
);
2210 pp_c_relational_expression (pp
, e
);
2214 pp_c_and_expression (pp
, e
);
2218 case TRUTH_XOR_EXPR
:
2219 pp_c_exclusive_or_expression (pp
, e
);
2223 pp_c_inclusive_or_expression (pp
, e
);
2226 case TRUTH_ANDIF_EXPR
:
2227 case TRUTH_AND_EXPR
:
2228 pp_c_logical_and_expression (pp
, e
);
2231 case TRUTH_ORIF_EXPR
:
2233 pp_c_logical_or_expression (pp
, e
);
2238 pp_c_equality_expression (pp
, e
);
2242 pp_conditional_expression (pp
, e
);
2245 case POINTER_PLUS_EXPR
:
2248 pp_c_additive_expression (pp
, e
);
2253 pp_assignment_expression (pp
, e
);
2257 pp_c_left_paren (pp
);
2258 pp_expression (pp
, TREE_OPERAND (e
, 0));
2259 pp_separate_with (pp
, ',');
2260 pp_assignment_expression (pp
, TREE_OPERAND (e
, 1));
2261 pp_c_right_paren (pp
);
2264 case NON_LVALUE_EXPR
:
2266 pp_expression (pp
, TREE_OPERAND (e
, 0));
2270 pp_postfix_expression (pp
, TREE_OPERAND (e
, 1));
2275 /* We don't yet have a way of dumping statements in a
2276 human-readable format. */
2277 pp_string (pp
, "({...})");
2280 case C_MAYBE_CONST_EXPR
:
2281 pp_c_expression (pp
, C_MAYBE_CONST_EXPR_EXPR (e
));
2285 pp_unsupported_tree (pp
, e
);
2295 pp_c_statement (c_pretty_printer
*pp
, tree stmt
)
2300 if (pp_needs_newline (pp
))
2301 pp_newline_and_indent (pp
, 0);
2303 dump_generic_node (pp_base (pp
), stmt
, pp_indentation (pp
), 0, true);
2307 /* Initialize the PRETTY-PRINTER for handling C codes. */
2310 pp_c_pretty_printer_init (c_pretty_printer
*pp
)
2312 pp
->offset_list
= 0;
2316 pp
->declaration
= pp_c_declaration
;
2317 pp
->declaration_specifiers
= pp_c_declaration_specifiers
;
2318 pp
->declarator
= pp_c_declarator
;
2319 pp
->direct_declarator
= pp_c_direct_declarator
;
2320 pp
->type_specifier_seq
= pp_c_specifier_qualifier_list
;
2321 pp
->abstract_declarator
= pp_c_abstract_declarator
;
2322 pp
->direct_abstract_declarator
= pp_c_direct_abstract_declarator
;
2323 pp
->ptr_operator
= pp_c_pointer
;
2324 pp
->parameter_list
= pp_c_parameter_type_list
;
2325 pp
->type_id
= pp_c_type_id
;
2326 pp
->simple_type_specifier
= pp_c_type_specifier
;
2327 pp
->function_specifier
= pp_c_function_specifier
;
2328 pp
->storage_class_specifier
= pp_c_storage_class_specifier
;
2330 pp
->statement
= pp_c_statement
;
2332 pp
->constant
= pp_c_constant
;
2333 pp
->id_expression
= pp_c_id_expression
;
2334 pp
->primary_expression
= pp_c_primary_expression
;
2335 pp
->postfix_expression
= pp_c_postfix_expression
;
2336 pp
->unary_expression
= pp_c_unary_expression
;
2337 pp
->initializer
= pp_c_initializer
;
2338 pp
->multiplicative_expression
= pp_c_multiplicative_expression
;
2339 pp
->conditional_expression
= pp_c_conditional_expression
;
2340 pp
->assignment_expression
= pp_c_assignment_expression
;
2341 pp
->expression
= pp_c_expression
;
2345 /* Print the tree T in full, on file FILE. */
2348 print_c_tree (FILE *file
, tree t
)
2350 static c_pretty_printer pp_rec
;
2351 static bool initialized
= 0;
2352 c_pretty_printer
*pp
= &pp_rec
;
2357 pp_construct (pp_base (pp
), NULL
, 0);
2358 pp_c_pretty_printer_init (pp
);
2359 pp_needs_newline (pp
) = true;
2361 pp_base (pp
)->buffer
->stream
= file
;
2363 pp_statement (pp
, t
);
2365 pp_newline_and_flush (pp
);
2368 /* Print the tree T in full, on stderr. */
2371 debug_c_tree (tree t
)
2373 print_c_tree (stderr
, t
);
2374 fputc ('\n', stderr
);
2377 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2378 up of T's memory address. */
2381 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2385 gcc_assert (DECL_P (t
));
2388 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2391 static char xname
[8];
2392 sprintf (xname
, "<U%4x>", ((unsigned)((uintptr_t)(t
) & 0xffff)));
2396 pp_c_identifier (pp
, name
);