1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2018 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"
24 #include "c-pretty-print.h"
25 #include "diagnostic.h"
26 #include "stor-layout.h"
27 #include "stringpool.h"
30 #include "tree-pretty-print.h"
33 /* The pretty-printer code is primarily designed to closely follow
34 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
35 codes we used to have in the past. Following a structured
36 approach (preferably the official grammars) is believed to make it
37 much easier to add extensions and nifty pretty-printing effects that
38 takes expression or declaration contexts into account. */
41 #define pp_c_maybe_whitespace(PP) \
43 if ((PP)->padding == pp_before) \
44 pp_c_whitespace (PP); \
48 static void pp_c_char (c_pretty_printer
*, int);
50 /* postfix-expression */
51 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
52 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
54 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
55 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
56 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
57 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
58 static void pp_c_and_expression (c_pretty_printer
*, tree
);
59 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
60 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
61 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
66 /* Helper functions. */
69 pp_c_whitespace (c_pretty_printer
*pp
)
72 pp
->padding
= pp_none
;
76 pp_c_left_paren (c_pretty_printer
*pp
)
79 pp
->padding
= pp_none
;
83 pp_c_right_paren (c_pretty_printer
*pp
)
86 pp
->padding
= pp_none
;
90 pp_c_left_brace (c_pretty_printer
*pp
)
93 pp
->padding
= pp_none
;
97 pp_c_right_brace (c_pretty_printer
*pp
)
100 pp
->padding
= pp_none
;
104 pp_c_left_bracket (c_pretty_printer
*pp
)
106 pp_left_bracket (pp
);
107 pp
->padding
= pp_none
;
111 pp_c_right_bracket (c_pretty_printer
*pp
)
113 pp_right_bracket (pp
);
114 pp
->padding
= pp_none
;
118 pp_c_dot (c_pretty_printer
*pp
)
121 pp
->padding
= pp_none
;
125 pp_c_ampersand (c_pretty_printer
*pp
)
128 pp
->padding
= pp_none
;
132 pp_c_star (c_pretty_printer
*pp
)
135 pp
->padding
= pp_none
;
139 pp_c_arrow (c_pretty_printer
*pp
)
142 pp
->padding
= pp_none
;
146 pp_c_semicolon (c_pretty_printer
*pp
)
149 pp
->padding
= pp_none
;
153 pp_c_complement (c_pretty_printer
*pp
)
156 pp
->padding
= pp_none
;
160 pp_c_exclamation (c_pretty_printer
*pp
)
163 pp
->padding
= pp_none
;
166 /* Print out the external representation of QUALIFIERS. */
169 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
171 const char *p
= pp_last_position_in_text (pp
);
176 /* The C programming language does not have references, but it is much
177 simpler to handle those here rather than going through the same
178 logic in the C++ pretty-printer. */
179 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
180 pp_c_whitespace (pp
);
182 if (qualifiers
& TYPE_QUAL_ATOMIC
)
183 pp_c_ws_string (pp
, "_Atomic");
184 if (qualifiers
& TYPE_QUAL_CONST
)
185 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
186 if (qualifiers
& TYPE_QUAL_VOLATILE
)
187 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
188 if (qualifiers
& TYPE_QUAL_RESTRICT
)
189 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
190 ? "restrict" : "__restrict__"));
193 /* Pretty-print T using the type-cast notation '( type-name )'. */
196 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
198 pp_c_left_paren (pp
);
200 pp_c_right_paren (pp
);
203 /* We're about to pretty-print a pointer type as indicated by T.
204 Output a whitespace, if needed, preparing for subsequent output. */
207 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
209 if (POINTER_TYPE_P (t
))
211 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
212 if (TREE_CODE (pointee
) != ARRAY_TYPE
213 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
214 pp_c_whitespace (pp
);
221 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
222 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
223 of its type. Take care of possible extensions.
227 type-qualifier-list type-qualifier
232 __restrict__ -- GNU C
233 address-space-qualifier -- GNU C
237 address-space-qualifier:
238 identifier -- GNU C */
241 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
245 if (!t
|| t
== error_mark_node
)
251 qualifiers
= TYPE_QUALS (t
);
252 pp_c_cv_qualifiers (pp
, qualifiers
,
253 TREE_CODE (t
) == FUNCTION_TYPE
);
255 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
257 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
258 pp_c_identifier (pp
, as
);
263 * type-qualifier-list(opt)
264 * type-qualifier-list(opt) pointer */
267 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
269 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
271 switch (TREE_CODE (t
))
274 /* It is easier to handle C++ reference types here. */
276 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
277 pp_c_pointer (pp
, TREE_TYPE (t
));
278 if (TREE_CODE (t
) == POINTER_TYPE
)
282 pp_c_type_qualifier_list (pp
, t
);
285 /* ??? This node is now in GENERIC and so shouldn't be here. But
286 we'll fix that later. */
288 pp
->declaration (DECL_EXPR_DECL (t
));
289 pp_needs_newline (pp
) = true;
293 pp_unsupported_tree (pp
, t
);
297 /* simple-type-specifier:
313 struct-or-union-specifier
318 simple-type-specifier:
323 c_pretty_printer::simple_type_specifier (tree t
)
325 const enum tree_code code
= TREE_CODE (t
);
329 translate_string ("<type-error>");
332 case IDENTIFIER_NODE
:
333 pp_c_identifier (this, IDENTIFIER_POINTER (t
));
340 case FIXED_POINT_TYPE
:
344 simple_type_specifier (t
);
348 int prec
= TYPE_PRECISION (t
);
350 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
351 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
352 TYPE_SATURATING (t
));
354 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
356 if (common_t
&& TYPE_NAME (common_t
))
358 simple_type_specifier (common_t
);
359 if (TYPE_PRECISION (common_t
) != prec
)
362 pp_decimal_int (this, prec
);
370 translate_string (TYPE_UNSIGNED (t
)
371 ? "<unnamed-unsigned:"
372 : "<unnamed-signed:");
375 translate_string ("<unnamed-float:");
377 case FIXED_POINT_TYPE
:
378 translate_string ("<unnamed-fixed:");
383 pp_decimal_int (this, prec
);
393 translate_string ("<typedef-error>");
399 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
400 /* Don't decorate the type if this is a typedef name. */;
401 else if (code
== UNION_TYPE
)
402 pp_c_ws_string (this, "union");
403 else if (code
== RECORD_TYPE
)
404 pp_c_ws_string (this, "struct");
405 else if (code
== ENUMERAL_TYPE
)
406 pp_c_ws_string (this, "enum");
408 translate_string ("<tag-error>");
411 id_expression (TYPE_NAME (t
));
413 translate_string ("<anonymous>");
417 pp_unsupported_tree (this, t
);
422 /* specifier-qualifier-list:
423 type-specifier specifier-qualifier-list-opt
424 type-qualifier specifier-qualifier-list-opt
427 Implementation note: Because of the non-linearities in array or
428 function declarations, this routine prints not just the
429 specifier-qualifier-list of such entities or types of such entities,
430 but also the 'pointer' production part of their declarators. The
431 remaining part is done by declarator() or abstract_declarator(). */
434 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
436 const enum tree_code code
= TREE_CODE (t
);
438 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
439 pp_c_type_qualifier_list (pp
, t
);
445 /* Get the types-specifier of this type. */
446 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
447 pp_c_specifier_qualifier_list (pp
, pointee
);
448 if (TREE_CODE (pointee
) == ARRAY_TYPE
449 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
451 pp_c_whitespace (pp
);
452 pp_c_left_paren (pp
);
453 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
455 else if (!c_dialect_cxx ())
456 pp_c_whitespace (pp
);
457 pp_ptr_operator (pp
, t
);
463 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
468 if (code
== COMPLEX_TYPE
)
469 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
470 ? "_Complex" : "__complex__"));
471 else if (code
== VECTOR_TYPE
)
473 pp_c_ws_string (pp
, "__vector");
474 pp_c_left_paren (pp
);
475 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
476 pp_c_right_paren (pp
);
477 pp_c_whitespace (pp
);
479 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
483 pp
->simple_type_specifier (t
);
486 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
487 pp_c_type_qualifier_list (pp
, t
);
490 /* parameter-type-list:
495 parameter-declaration
496 parameter-list , parameter-declaration
498 parameter-declaration:
499 declaration-specifiers declarator
500 declaration-specifiers abstract-declarator(opt) */
503 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
505 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
506 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
507 pp_c_left_paren (pp
);
508 if (parms
== void_list_node
)
509 pp_c_ws_string (pp
, "void");
513 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
516 pp_separate_with (pp
, ',');
518 pp
->declaration_specifiers
519 (want_parm_decl
? parms
: TREE_VALUE (parms
));
521 pp
->declarator (parms
);
523 pp
->abstract_declarator (TREE_VALUE (parms
));
525 if (!first
&& !parms
)
527 pp_separate_with (pp
, ',');
528 pp_c_ws_string (pp
, "...");
531 pp_c_right_paren (pp
);
534 /* abstract-declarator:
536 pointer(opt) direct-abstract-declarator */
539 c_pretty_printer::abstract_declarator (tree t
)
541 if (TREE_CODE (t
) == POINTER_TYPE
)
543 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
544 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
545 pp_c_right_paren (this);
549 direct_abstract_declarator (t
);
552 /* direct-abstract-declarator:
553 ( abstract-declarator )
554 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
555 direct-abstract-declarator(opt) [ * ]
556 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
559 c_pretty_printer::direct_abstract_declarator (tree t
)
561 switch (TREE_CODE (t
))
564 abstract_declarator (t
);
568 pp_c_parameter_type_list (this, t
);
569 direct_abstract_declarator (TREE_TYPE (t
));
573 pp_c_left_bracket (this);
574 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
576 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
577 tree type
= TREE_TYPE (maxval
);
579 if (tree_fits_shwi_p (maxval
))
580 pp_wide_integer (this, tree_to_shwi (maxval
) + 1);
582 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
583 build_int_cst (type
, 1)));
585 pp_c_right_bracket (this);
586 direct_abstract_declarator (TREE_TYPE (t
));
589 case IDENTIFIER_NODE
:
594 case FIXED_POINT_TYPE
:
604 pp_unsupported_tree (this, t
);
610 specifier-qualifier-list abstract-declarator(opt) */
613 c_pretty_printer::type_id (tree t
)
615 pp_c_specifier_qualifier_list (this, t
);
616 abstract_declarator (t
);
619 /* storage-class-specifier:
627 c_pretty_printer::storage_class_specifier (tree t
)
629 if (TREE_CODE (t
) == TYPE_DECL
)
630 pp_c_ws_string (this, "typedef");
633 if (DECL_REGISTER (t
))
634 pp_c_ws_string (this, "register");
635 else if (TREE_STATIC (t
) && VAR_P (t
))
636 pp_c_ws_string (this, "static");
640 /* function-specifier:
644 c_pretty_printer::function_specifier (tree t
)
646 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
647 pp_c_ws_string (this, "inline");
650 /* declaration-specifiers:
651 storage-class-specifier declaration-specifiers(opt)
652 type-specifier declaration-specifiers(opt)
653 type-qualifier declaration-specifiers(opt)
654 function-specifier declaration-specifiers(opt) */
657 c_pretty_printer::declaration_specifiers (tree t
)
659 storage_class_specifier (t
);
660 function_specifier (t
);
661 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
667 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
668 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
669 direct-declarator [ type-qualifier-list static assignment-expression ]
670 direct-declarator [ type-qualifier-list * ]
671 direct-declarator ( parameter-type-list )
672 direct-declarator ( identifier-list(opt) ) */
675 c_pretty_printer::direct_declarator (tree t
)
677 switch (TREE_CODE (t
))
684 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
685 pp_c_tree_decl_identifier (this, t
);
690 abstract_declarator (TREE_TYPE (t
));
694 pp_parameter_list (this, t
);
695 abstract_declarator (TREE_TYPE (t
));
699 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
700 pp_c_tree_decl_identifier (this, t
);
701 if (flags
& pp_c_flag_abstract
)
702 abstract_declarator (TREE_TYPE (t
));
705 pp_parameter_list (this, t
);
706 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
712 case FIXED_POINT_TYPE
:
719 pp_unsupported_tree (this, t
);
726 pointer(opt) direct-declarator */
729 c_pretty_printer::declarator (tree t
)
731 switch (TREE_CODE (t
))
735 case FIXED_POINT_TYPE
:
748 direct_declarator (t
);
753 pp_unsupported_tree (this, t
);
759 declaration-specifiers init-declarator-list(opt) ; */
762 c_pretty_printer::declaration (tree t
)
764 declaration_specifiers (t
);
765 pp_c_init_declarator (this, t
);
768 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
771 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
773 if (attributes
== NULL_TREE
)
776 pp_c_ws_string (pp
, "__attribute__");
777 pp_c_left_paren (pp
);
778 pp_c_left_paren (pp
);
779 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
781 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
782 if (TREE_VALUE (attributes
))
783 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
785 if (TREE_CHAIN (attributes
))
786 pp_separate_with (pp
, ',');
788 pp_c_right_paren (pp
);
789 pp_c_right_paren (pp
);
792 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
793 marked to be displayed on disgnostic. */
796 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
798 bool is_first
= true;
803 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
805 const struct attribute_spec
*as
;
806 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
807 if (!as
|| as
->affects_type_identity
== false)
810 && !strcmp ("transaction_safe", as
->name
))
811 /* In C++ transaction_safe is printed at the end of the declarator. */
815 pp_c_ws_string (pp
, "__attribute__");
816 pp_c_left_paren (pp
);
817 pp_c_left_paren (pp
);
822 pp_separate_with (pp
, ',');
824 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
826 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
831 pp_c_right_paren (pp
);
832 pp_c_right_paren (pp
);
833 pp_c_whitespace (pp
);
837 /* function-definition:
838 declaration-specifiers declarator compound-statement */
841 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
843 pp
->declaration_specifiers (t
);
845 pp_needs_newline (pp
) = true;
846 pp
->statement (DECL_SAVED_TREE (t
));
847 pp_newline_and_flush (pp
);
853 /* Print out a c-char. This is called solely for characters which are
854 in the *target* execution character set. We ought to convert them
855 back to the *host* execution character set before printing, but we
856 have no way to do this at present. A decent compromise is to print
857 all characters as if they were in the host execution character set,
858 and not attempt to recover any named escape characters, but render
859 all unprintables as octal escapes. If the host and target character
860 sets are the same, this produces relatively readable output. If they
861 are not the same, strings may appear as gibberish, but that's okay
862 (in fact, it may well be what the reader wants, e.g. if they are looking
863 to see if conversion to the target character set happened correctly).
865 A special case: we need to prefix \, ", and ' with backslashes. It is
866 correct to do so for the *host*'s \, ", and ', because the rest of the
867 file appears in the host character set. */
870 pp_c_char (c_pretty_printer
*pp
, int c
)
876 case '\\': pp_string (pp
, "\\\\"); break;
877 case '\'': pp_string (pp
, "\\\'"); break;
878 case '\"': pp_string (pp
, "\\\""); break;
879 default: pp_character (pp
, c
);
883 pp_scalar (pp
, "\\%03o", (unsigned) c
);
886 /* Print out a STRING literal. */
889 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
891 const char *p
= TREE_STRING_POINTER (s
);
892 int n
= TREE_STRING_LENGTH (s
) - 1;
895 for (i
= 0; i
< n
; ++i
)
896 pp_c_char (pp
, p
[i
]);
900 /* Pretty-print a VOID_CST (void_node). */
903 pp_c_void_constant (c_pretty_printer
*pp
)
905 pp_c_type_cast (pp
, void_type_node
);
909 /* Pretty-print an INTEGER literal. */
912 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
914 if (tree_fits_shwi_p (i
))
915 pp_wide_integer (pp
, tree_to_shwi (i
));
916 else if (tree_fits_uhwi_p (i
))
917 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
920 wide_int wi
= wi::to_wide (i
);
922 if (wi::lt_p (wi::to_wide (i
), 0, TYPE_SIGN (TREE_TYPE (i
))))
927 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
928 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
932 /* Print out a CHARACTER literal. */
935 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
938 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
942 /* Print out a BOOLEAN literal. */
945 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
947 if (b
== boolean_false_node
)
949 if (c_dialect_cxx ())
950 pp_c_ws_string (pp
, "false");
951 else if (flag_isoc99
)
952 pp_c_ws_string (pp
, "_False");
954 pp_unsupported_tree (pp
, b
);
956 else if (b
== boolean_true_node
)
958 if (c_dialect_cxx ())
959 pp_c_ws_string (pp
, "true");
960 else if (flag_isoc99
)
961 pp_c_ws_string (pp
, "_True");
963 pp_unsupported_tree (pp
, b
);
965 else if (TREE_CODE (b
) == INTEGER_CST
)
966 pp_c_integer_constant (pp
, b
);
968 pp_unsupported_tree (pp
, b
);
971 /* Given a value e of ENUMERAL_TYPE:
972 Print out the first ENUMERATOR id with value e, if one is found,
973 else print out the value as a C-style cast (type-id)value. */
976 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
978 tree type
= TREE_TYPE (e
);
981 /* Find the name of this constant. */
982 for (value
= TYPE_VALUES (type
);
984 && !tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value
)), e
);
985 value
= TREE_CHAIN (value
))
988 if (value
!= NULL_TREE
)
989 pp
->id_expression (TREE_PURPOSE (value
));
992 /* Value must have been cast. */
993 pp_c_type_cast (pp
, type
);
994 pp_c_integer_constant (pp
, e
);
998 /* Print out a REAL value as a decimal-floating-constant. */
1001 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1003 const struct real_format
*fmt
1004 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1006 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1007 bool is_decimal
= floating_cst
.decimal
;
1009 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1010 log10(2) to 7 significant digits. */
1011 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1013 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1014 sizeof (pp_buffer (pp
)->digit_buffer
),
1017 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1018 if (TREE_TYPE (r
) == float_type_node
)
1019 pp_character (pp
, 'f');
1020 else if (TREE_TYPE (r
) == long_double_type_node
)
1021 pp_character (pp
, 'l');
1022 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1023 pp_string (pp
, "dl");
1024 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1025 pp_string (pp
, "dd");
1026 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1027 pp_string (pp
, "df");
1028 else if (TREE_TYPE (r
) != double_type_node
)
1029 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1030 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1032 pp_character (pp
, 'f');
1033 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1034 if (floatn_nx_types
[i
].extended
)
1035 pp_character (pp
, 'x');
1040 /* Print out a FIXED value as a decimal-floating-constant. */
1043 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1045 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1046 sizeof (pp_buffer (pp
)->digit_buffer
));
1047 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1050 /* Pretty-print a compound literal expression. GNU extensions include
1051 vector constants. */
1054 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1056 tree type
= TREE_TYPE (e
);
1057 pp_c_type_cast (pp
, type
);
1059 switch (TREE_CODE (type
))
1066 pp_c_brace_enclosed_initializer_list (pp
, e
);
1070 pp_unsupported_tree (pp
, e
);
1075 /* Pretty-print a COMPLEX_EXPR expression. */
1078 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1080 /* Handle a few common special cases, otherwise fallback
1081 to printing it as compound literal. */
1082 tree type
= TREE_TYPE (e
);
1083 tree realexpr
= TREE_OPERAND (e
, 0);
1084 tree imagexpr
= TREE_OPERAND (e
, 1);
1086 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1087 if (TREE_CODE (realexpr
) == NOP_EXPR
1088 && TREE_CODE (imagexpr
) == NOP_EXPR
1089 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1090 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1091 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1092 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1093 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1094 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1096 pp_c_type_cast (pp
, type
);
1097 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1101 /* Cast of an scalar expression to COMPLEX_TYPE. */
1102 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1103 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1105 pp_c_type_cast (pp
, type
);
1106 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1107 realexpr
= TREE_OPERAND (realexpr
, 0);
1108 pp
->expression (realexpr
);
1112 pp_c_compound_literal (pp
, e
);
1118 fixed-point-constant
1119 enumeration-constant
1120 character-constant */
1123 c_pretty_printer::constant (tree e
)
1125 const enum tree_code code
= TREE_CODE (e
);
1130 pp_c_void_constant (this);
1135 tree type
= TREE_TYPE (e
);
1136 if (type
== boolean_type_node
)
1137 pp_c_bool_constant (this, e
);
1138 else if (type
== char_type_node
)
1139 pp_c_character_constant (this, e
);
1140 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1141 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>");
1246 pp_c_ws_string (this, "__builtin_memcpy");
1247 pp_c_left_paren (this);
1248 pp_ampersand (this);
1249 primary_expression (TREE_OPERAND (e
, 0));
1250 pp_separate_with (this, ',');
1251 pp_ampersand (this);
1252 initializer (TREE_OPERAND (e
, 1));
1253 if (TREE_OPERAND (e
, 2))
1255 pp_separate_with (this, ',');
1256 expression (TREE_OPERAND (e
, 2));
1258 pp_c_right_paren (this);
1262 /* FIXME: Make sure we won't get into an infinite loop. */
1263 pp_c_left_paren (this);
1265 pp_c_right_paren (this);
1270 /* Print out a C initializer -- also support C compound-literals.
1272 assignment-expression:
1273 { initializer-list }
1274 { initializer-list , } */
1277 c_pretty_printer::initializer (tree e
)
1279 if (TREE_CODE (e
) == CONSTRUCTOR
)
1280 pp_c_brace_enclosed_initializer_list (this, e
);
1287 declarator = initializer */
1290 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1293 /* We don't want to output function definitions here. There are handled
1294 elsewhere (and the syntactic form is bogus anyway). */
1295 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1297 tree init
= DECL_INITIAL (t
);
1298 /* This C++ bit is handled here because it is easier to do so.
1299 In templates, the C++ parser builds a TREE_LIST for a
1300 direct-initialization; the TREE_PURPOSE is the variable to
1301 initialize and the TREE_VALUE is the initializer. */
1302 if (TREE_CODE (init
) == TREE_LIST
)
1304 pp_c_left_paren (pp
);
1305 pp
->expression (TREE_VALUE (init
));
1306 pp_right_paren (pp
);
1313 pp
->initializer (init
);
1318 /* initializer-list:
1319 designation(opt) initializer
1320 initializer-list , designation(opt) initializer
1327 designator-list designator
1330 [ constant-expression ]
1334 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1336 tree type
= TREE_TYPE (e
);
1337 const enum tree_code code
= TREE_CODE (type
);
1339 if (TREE_CODE (e
) == CONSTRUCTOR
)
1341 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1351 tree init
= TREE_OPERAND (e
, 0);
1352 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1354 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1357 pp
->primary_expression (TREE_PURPOSE (init
));
1361 pp_c_left_bracket (pp
);
1362 if (TREE_PURPOSE (init
))
1363 pp
->constant (TREE_PURPOSE (init
));
1364 pp_c_right_bracket (pp
);
1366 pp_c_whitespace (pp
);
1368 pp_c_whitespace (pp
);
1369 pp
->initializer (TREE_VALUE (init
));
1370 if (TREE_CHAIN (init
))
1371 pp_separate_with (pp
, ',');
1377 if (TREE_CODE (e
) == VECTOR_CST
)
1379 /* We don't create variable-length VECTOR_CSTs. */
1380 unsigned int nunits
= VECTOR_CST_NELTS (e
).to_constant ();
1381 for (unsigned int i
= 0; i
< nunits
; ++i
)
1384 pp_separate_with (pp
, ',');
1385 pp
->expression (VECTOR_CST_ELT (e
, i
));
1393 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1395 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1396 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1397 pp_separate_with (pp
, ',');
1398 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1408 pp_unsupported_tree (pp
, type
);
1411 /* Pretty-print a brace-enclosed initializer-list. */
1414 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1416 pp_c_left_brace (pp
);
1417 pp_c_initializer_list (pp
, l
);
1418 pp_c_right_brace (pp
);
1422 /* This is a convenient function, used to bridge gap between C and C++
1429 c_pretty_printer::id_expression (tree t
)
1431 switch (TREE_CODE (t
))
1440 pp_c_tree_decl_identifier (this, t
);
1443 case IDENTIFIER_NODE
:
1444 pp_c_tree_identifier (this, t
);
1448 pp_unsupported_tree (this, t
);
1453 /* postfix-expression:
1455 postfix-expression [ expression ]
1456 postfix-expression ( argument-expression-list(opt) )
1457 postfix-expression . identifier
1458 postfix-expression -> identifier
1459 postfix-expression ++
1460 postfix-expression --
1461 ( type-name ) { initializer-list }
1462 ( type-name ) { initializer-list , } */
1465 c_pretty_printer::postfix_expression (tree e
)
1467 enum tree_code code
= TREE_CODE (e
);
1470 case POSTINCREMENT_EXPR
:
1471 case POSTDECREMENT_EXPR
:
1472 postfix_expression (TREE_OPERAND (e
, 0));
1473 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1477 postfix_expression (TREE_OPERAND (e
, 0));
1478 pp_c_left_bracket (this);
1479 expression (TREE_OPERAND (e
, 1));
1480 pp_c_right_bracket (this);
1485 call_expr_arg_iterator iter
;
1487 postfix_expression (CALL_EXPR_FN (e
));
1488 pp_c_left_paren (this);
1489 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1492 if (more_call_expr_args_p (&iter
))
1493 pp_separate_with (this, ',');
1495 pp_c_right_paren (this);
1499 case UNORDERED_EXPR
:
1500 pp_c_ws_string (this, flag_isoc99
1502 : "__builtin_isunordered");
1506 pp_c_ws_string (this, flag_isoc99
1508 : "!__builtin_isunordered");
1512 pp_c_ws_string (this, flag_isoc99
1514 : "!__builtin_isgreaterequal");
1518 pp_c_ws_string (this, flag_isoc99
1520 : "!__builtin_isgreater");
1524 pp_c_ws_string (this, flag_isoc99
1526 : "!__builtin_islessequal");
1530 pp_c_ws_string (this, flag_isoc99
1532 : "!__builtin_isless");
1536 pp_c_ws_string (this, flag_isoc99
1538 : "!__builtin_islessgreater");
1542 pp_c_ws_string (this, flag_isoc99
1544 : "__builtin_islessgreater");
1548 pp_c_ws_string (this, "max");
1552 pp_c_ws_string (this, "min");
1556 pp_c_left_paren (this);
1557 expression (TREE_OPERAND (e
, 0));
1558 pp_separate_with (this, ',');
1559 expression (TREE_OPERAND (e
, 1));
1560 pp_c_right_paren (this);
1564 pp_c_ws_string (this, "__builtin_abs");
1565 pp_c_left_paren (this);
1566 expression (TREE_OPERAND (e
, 0));
1567 pp_c_right_paren (this);
1572 tree object
= TREE_OPERAND (e
, 0);
1573 if (INDIRECT_REF_P (object
))
1575 postfix_expression (TREE_OPERAND (object
, 0));
1580 postfix_expression (object
);
1583 expression (TREE_OPERAND (e
, 1));
1589 tree type
= TREE_TYPE (e
);
1591 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1593 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1595 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1596 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1597 if ((bitpos
% size
) == 0)
1599 pp_c_left_paren (this);
1600 pp_c_left_paren (this);
1603 pp_c_right_paren (this);
1604 pp_c_ampersand (this);
1605 expression (TREE_OPERAND (e
, 0));
1606 pp_c_right_paren (this);
1607 pp_c_left_bracket (this);
1608 pp_wide_integer (this, bitpos
/ size
);
1609 pp_c_right_bracket (this);
1613 pp_unsupported_tree (this, e
);
1623 pp_c_compound_literal (this, e
);
1627 pp_c_complex_expr (this, e
);
1630 case COMPOUND_LITERAL_EXPR
:
1631 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1638 pp_c_ws_string (this, "__builtin_va_arg");
1639 pp_c_left_paren (this);
1640 assignment_expression (TREE_OPERAND (e
, 0));
1641 pp_separate_with (this, ',');
1642 type_id (TREE_TYPE (e
));
1643 pp_c_right_paren (this);
1647 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1649 id_expression (TREE_OPERAND (e
, 0));
1655 primary_expression (e
);
1660 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1663 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1665 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1667 pp
->expression (TREE_VALUE (e
));
1669 pp_separate_with (pp
, ',');
1673 /* Print out V, which contains the elements of a constructor. */
1676 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1678 unsigned HOST_WIDE_INT ix
;
1681 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1683 pp
->expression (value
);
1684 if (ix
!= vec_safe_length (v
) - 1)
1685 pp_separate_with (pp
, ',');
1689 /* Print out an expression-list in parens, as if it were the argument
1690 list to a function. */
1693 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1695 pp_c_left_paren (pp
);
1696 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1697 pp_c_expression_list (pp
, t
);
1698 pp_c_right_paren (pp
);
1701 /* unary-expression:
1705 unary-operator cast-expression
1706 sizeof unary-expression
1709 unary-operator: one of
1714 __alignof__ unary-expression
1715 __alignof__ ( type-id )
1716 __real__ unary-expression
1717 __imag__ unary-expression */
1720 c_pretty_printer::unary_expression (tree e
)
1722 enum tree_code code
= TREE_CODE (e
);
1725 case PREINCREMENT_EXPR
:
1726 case PREDECREMENT_EXPR
:
1727 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1728 unary_expression (TREE_OPERAND (e
, 0));
1735 case TRUTH_NOT_EXPR
:
1737 /* String literal are used by address. */
1738 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1739 pp_ampersand (this);
1740 else if (code
== INDIRECT_REF
)
1742 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
1743 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
1744 /* Reference decay is implicit, don't print anything. */;
1748 else if (code
== NEGATE_EXPR
)
1750 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1751 pp_complement (this);
1752 else if (code
== TRUTH_NOT_EXPR
)
1753 pp_exclamation (this);
1754 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1758 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1759 && integer_zerop (TREE_OPERAND (e
, 1)))
1760 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1764 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1766 pp_c_left_paren (this);
1767 if (!integer_onep (TYPE_SIZE_UNIT
1768 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1769 pp_c_type_cast (this, ptr_type_node
);
1771 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1772 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1775 pp_c_integer_constant (this,
1776 fold_convert (ssizetype
,
1777 TREE_OPERAND (e
, 1)));
1778 pp_c_right_paren (this);
1785 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1786 pp_c_whitespace (this);
1787 unary_expression (TREE_OPERAND (e
, 0));
1791 postfix_expression (e
);
1798 ( type-name ) cast-expression */
1801 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1803 switch (TREE_CODE (e
))
1806 case FIX_TRUNC_EXPR
:
1808 case VIEW_CONVERT_EXPR
:
1809 if (!location_wrapper_p (e
))
1810 pp_c_type_cast (pp
, TREE_TYPE (e
));
1811 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1815 pp
->unary_expression (e
);
1819 /* multiplicative-expression:
1821 multiplicative-expression * cast-expression
1822 multiplicative-expression / cast-expression
1823 multiplicative-expression % cast-expression */
1826 c_pretty_printer::multiplicative_expression (tree e
)
1828 enum tree_code code
= TREE_CODE (e
);
1832 case TRUNC_DIV_EXPR
:
1833 case TRUNC_MOD_EXPR
:
1834 case EXACT_DIV_EXPR
:
1836 multiplicative_expression (TREE_OPERAND (e
, 0));
1837 pp_c_whitespace (this);
1838 if (code
== MULT_EXPR
)
1840 else if (code
!= TRUNC_MOD_EXPR
)
1844 pp_c_whitespace (this);
1845 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1849 pp_c_cast_expression (this, e
);
1854 /* additive-expression:
1855 multiplicative-expression
1856 additive-expression + multiplicative-expression
1857 additive-expression - multiplicative-expression */
1860 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1862 enum tree_code code
= TREE_CODE (e
);
1865 case POINTER_PLUS_EXPR
:
1867 case POINTER_DIFF_EXPR
:
1869 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1870 pp_c_whitespace (pp
);
1871 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1875 pp_c_whitespace (pp
);
1876 pp
->multiplicative_expression (TREE_OPERAND (e
, 1));
1880 pp
->multiplicative_expression (e
);
1885 /* additive-expression:
1887 shift-expression << additive-expression
1888 shift-expression >> additive-expression */
1891 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1893 enum tree_code code
= TREE_CODE (e
);
1900 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1901 pp_c_whitespace (pp
);
1902 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
1903 code
== RSHIFT_EXPR
? ">>" :
1904 code
== LROTATE_EXPR
? "<<<" : ">>>");
1905 pp_c_whitespace (pp
);
1906 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1910 pp_c_additive_expression (pp
, e
);
1914 /* relational-expression:
1916 relational-expression < shift-expression
1917 relational-expression > shift-expression
1918 relational-expression <= shift-expression
1919 relational-expression >= shift-expression */
1922 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1924 enum tree_code code
= TREE_CODE (e
);
1931 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1932 pp_c_whitespace (pp
);
1933 if (code
== LT_EXPR
)
1935 else if (code
== GT_EXPR
)
1937 else if (code
== LE_EXPR
)
1939 else if (code
== GE_EXPR
)
1940 pp_greater_equal (pp
);
1941 pp_c_whitespace (pp
);
1942 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1946 pp_c_shift_expression (pp
, e
);
1951 /* equality-expression:
1952 relational-expression
1953 equality-expression == relational-expression
1954 equality-equality != relational-expression */
1957 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1959 enum tree_code code
= TREE_CODE (e
);
1964 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1965 pp_c_whitespace (pp
);
1966 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1967 pp_c_whitespace (pp
);
1968 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1972 pp_c_relational_expression (pp
, e
);
1979 AND-expression & equality-equality */
1982 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1984 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1986 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1987 pp_c_whitespace (pp
);
1989 pp_c_whitespace (pp
);
1990 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1993 pp_c_equality_expression (pp
, e
);
1996 /* exclusive-OR-expression:
1998 exclusive-OR-expression ^ AND-expression */
2001 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2003 if (TREE_CODE (e
) == BIT_XOR_EXPR
2004 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2006 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2007 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2008 pp_c_maybe_whitespace (pp
);
2010 pp_c_whitespace (pp
);
2012 pp_c_whitespace (pp
);
2013 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2016 pp_c_and_expression (pp
, e
);
2019 /* inclusive-OR-expression:
2020 exclusive-OR-expression
2021 inclusive-OR-expression | exclusive-OR-expression */
2024 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2026 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2028 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2029 pp_c_whitespace (pp
);
2031 pp_c_whitespace (pp
);
2032 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2035 pp_c_exclusive_or_expression (pp
, e
);
2038 /* logical-AND-expression:
2039 inclusive-OR-expression
2040 logical-AND-expression && inclusive-OR-expression */
2043 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2045 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2046 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2048 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2049 pp_c_whitespace (pp
);
2050 pp_ampersand_ampersand (pp
);
2051 pp_c_whitespace (pp
);
2052 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2055 pp_c_inclusive_or_expression (pp
, e
);
2058 /* logical-OR-expression:
2059 logical-AND-expression
2060 logical-OR-expression || logical-AND-expression */
2063 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2065 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2066 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2068 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2069 pp_c_whitespace (pp
);
2071 pp_c_whitespace (pp
);
2072 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2075 pp_c_logical_and_expression (pp
, e
);
2078 /* conditional-expression:
2079 logical-OR-expression
2080 logical-OR-expression ? expression : conditional-expression */
2083 c_pretty_printer::conditional_expression (tree e
)
2085 if (TREE_CODE (e
) == COND_EXPR
)
2087 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2088 pp_c_whitespace (this);
2090 pp_c_whitespace (this);
2091 expression (TREE_OPERAND (e
, 1));
2092 pp_c_whitespace (this);
2094 pp_c_whitespace (this);
2095 conditional_expression (TREE_OPERAND (e
, 2));
2098 pp_c_logical_or_expression (this, e
);
2102 /* assignment-expression:
2103 conditional-expression
2104 unary-expression assignment-operator assignment-expression
2106 assignment-expression: one of
2107 = *= /= %= += -= >>= <<= &= ^= |= */
2110 c_pretty_printer::assignment_expression (tree e
)
2112 if (TREE_CODE (e
) == MODIFY_EXPR
2113 || TREE_CODE (e
) == INIT_EXPR
)
2115 unary_expression (TREE_OPERAND (e
, 0));
2116 pp_c_whitespace (this);
2119 expression (TREE_OPERAND (e
, 1));
2122 conditional_expression (e
);
2126 assignment-expression
2127 expression , assignment-expression
2129 Implementation note: instead of going through the usual recursion
2130 chain, I take the liberty of dispatching nodes to the appropriate
2131 functions. This makes some redundancy, but it worths it. That also
2132 prevents a possible infinite recursion between primary_expression ()
2133 and expression (). */
2136 c_pretty_printer::expression (tree e
)
2138 switch (TREE_CODE (e
))
2141 pp_c_void_constant (this);
2145 pp_c_integer_constant (this, e
);
2149 pp_c_floating_constant (this, e
);
2153 pp_c_fixed_constant (this, e
);
2157 pp_c_string_literal (this, e
);
2160 case IDENTIFIER_NODE
:
2169 primary_expression (e
);
2173 if (SSA_NAME_VAR (e
)
2174 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2175 expression (SSA_NAME_VAR (e
));
2177 translate_string ("<unknown>");
2180 case POSTINCREMENT_EXPR
:
2181 case POSTDECREMENT_EXPR
:
2190 case UNORDERED_EXPR
:
2201 case COMPOUND_LITERAL_EXPR
:
2203 postfix_expression (e
);
2212 case TRUTH_NOT_EXPR
:
2213 case PREINCREMENT_EXPR
:
2214 case PREDECREMENT_EXPR
:
2217 unary_expression (e
);
2221 case FIX_TRUNC_EXPR
:
2223 case VIEW_CONVERT_EXPR
:
2224 pp_c_cast_expression (this, e
);
2228 case TRUNC_MOD_EXPR
:
2229 case TRUNC_DIV_EXPR
:
2230 case EXACT_DIV_EXPR
:
2232 multiplicative_expression (e
);
2239 pp_c_shift_expression (this, e
);
2246 pp_c_relational_expression (this, e
);
2250 pp_c_and_expression (this, e
);
2254 case TRUTH_XOR_EXPR
:
2255 pp_c_exclusive_or_expression (this, e
);
2259 pp_c_inclusive_or_expression (this, e
);
2262 case TRUTH_ANDIF_EXPR
:
2263 case TRUTH_AND_EXPR
:
2264 pp_c_logical_and_expression (this, e
);
2267 case TRUTH_ORIF_EXPR
:
2269 pp_c_logical_or_expression (this, e
);
2274 pp_c_equality_expression (this, e
);
2278 conditional_expression (e
);
2281 case POINTER_PLUS_EXPR
:
2283 case POINTER_DIFF_EXPR
:
2285 pp_c_additive_expression (this, e
);
2290 assignment_expression (e
);
2294 pp_c_left_paren (this);
2295 expression (TREE_OPERAND (e
, 0));
2296 pp_separate_with (this, ',');
2297 assignment_expression (TREE_OPERAND (e
, 1));
2298 pp_c_right_paren (this);
2301 case NON_LVALUE_EXPR
:
2303 expression (TREE_OPERAND (e
, 0));
2307 postfix_expression (TREE_OPERAND (e
, 1));
2312 /* We don't yet have a way of dumping statements in a
2313 human-readable format. */
2314 pp_string (this, "({...})");
2317 case C_MAYBE_CONST_EXPR
:
2318 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2322 pp_unsupported_tree (this, e
);
2332 c_pretty_printer::statement (tree stmt
)
2337 if (pp_needs_newline (this))
2338 pp_newline_and_indent (this, 0);
2340 dump_generic_node (this, stmt
, pp_indentation (this), TDF_NONE
, true);
2344 /* Initialize the PRETTY-PRINTER for handling C codes. */
2346 c_pretty_printer::c_pretty_printer ()
2347 : pretty_printer (),
2351 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2352 ptr_operator
= pp_c_pointer
;
2353 parameter_list
= pp_c_parameter_type_list
;
2357 /* Print the tree T in full, on file FILE. */
2360 print_c_tree (FILE *file
, tree t
)
2362 c_pretty_printer pp
;
2364 pp_needs_newline (&pp
) = true;
2365 pp
.buffer
->stream
= file
;
2367 pp_newline_and_flush (&pp
);
2370 /* Print the tree T in full, on stderr. */
2373 debug_c_tree (tree t
)
2375 print_c_tree (stderr
, t
);
2376 fputc ('\n', stderr
);
2379 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2380 up of T's memory address. */
2383 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2387 gcc_assert (DECL_P (t
));
2390 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2393 static char xname
[8];
2394 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2399 pp_c_identifier (pp
, name
);
2404 namespace selftest
{
2406 /* Selftests for pretty-printing trees. */
2408 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2409 LOC as the effective location for any failures. */
2412 assert_c_pretty_printer_output (const location
&loc
, const char *expected
,
2415 c_pretty_printer pp
;
2416 pp
.expression (expr
);
2417 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2420 /* Helper function for calling assert_c_pretty_printer_output.
2421 This is to avoid having to write SELFTEST_LOCATION. */
2423 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2424 SELFTEST_BEGIN_STMT \
2425 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
2430 /* Verify that location wrappers don't show up in pretty-printed output. */
2433 test_location_wrappers ()
2436 tree id
= get_identifier ("foo");
2437 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, id
,
2439 tree wrapped_decl
= maybe_wrap_with_location (decl
, BUILTINS_LOCATION
);
2440 ASSERT_NE (wrapped_decl
, decl
);
2441 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl
);
2442 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl
);
2445 tree int_cst
= build_int_cst (integer_type_node
, 42);
2446 tree wrapped_cst
= maybe_wrap_with_location (int_cst
, BUILTINS_LOCATION
);
2447 ASSERT_NE (wrapped_cst
, int_cst
);
2448 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst
);
2449 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst
);
2452 /* Run all of the selftests within this file. */
2455 c_pretty_print_c_tests ()
2457 test_location_wrappers ();
2460 } // namespace selftest
2462 #endif /* CHECKING_P */