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 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
972 false; that means the value was obtained by a cast, in which case
973 print out the type-id part of the cast-expression -- the casted value
974 is then printed by pp_c_integer_literal. */
977 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
979 bool value_is_named
= true;
980 tree type
= TREE_TYPE (e
);
983 /* Find the name of this constant. */
984 for (value
= TYPE_VALUES (type
);
985 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
986 value
= TREE_CHAIN (value
))
989 if (value
!= NULL_TREE
)
990 pp
->id_expression (TREE_PURPOSE (value
));
993 /* Value must have been cast. */
994 pp_c_type_cast (pp
, type
);
995 value_is_named
= false;
998 return value_is_named
;
1001 /* Print out a REAL value as a decimal-floating-constant. */
1004 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1006 const struct real_format
*fmt
1007 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1009 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1010 bool is_decimal
= floating_cst
.decimal
;
1012 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1013 log10(2) to 7 significant digits. */
1014 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1016 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1017 sizeof (pp_buffer (pp
)->digit_buffer
),
1020 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1021 if (TREE_TYPE (r
) == float_type_node
)
1022 pp_character (pp
, 'f');
1023 else if (TREE_TYPE (r
) == long_double_type_node
)
1024 pp_character (pp
, 'l');
1025 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1026 pp_string (pp
, "dl");
1027 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1028 pp_string (pp
, "dd");
1029 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1030 pp_string (pp
, "df");
1031 else if (TREE_TYPE (r
) != double_type_node
)
1032 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1033 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1035 pp_character (pp
, 'f');
1036 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1037 if (floatn_nx_types
[i
].extended
)
1038 pp_character (pp
, 'x');
1043 /* Print out a FIXED value as a decimal-floating-constant. */
1046 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1048 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1049 sizeof (pp_buffer (pp
)->digit_buffer
));
1050 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1053 /* Pretty-print a compound literal expression. GNU extensions include
1054 vector constants. */
1057 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1059 tree type
= TREE_TYPE (e
);
1060 pp_c_type_cast (pp
, type
);
1062 switch (TREE_CODE (type
))
1069 pp_c_brace_enclosed_initializer_list (pp
, e
);
1073 pp_unsupported_tree (pp
, e
);
1078 /* Pretty-print a COMPLEX_EXPR expression. */
1081 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1083 /* Handle a few common special cases, otherwise fallback
1084 to printing it as compound literal. */
1085 tree type
= TREE_TYPE (e
);
1086 tree realexpr
= TREE_OPERAND (e
, 0);
1087 tree imagexpr
= TREE_OPERAND (e
, 1);
1089 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1090 if (TREE_CODE (realexpr
) == NOP_EXPR
1091 && TREE_CODE (imagexpr
) == NOP_EXPR
1092 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1093 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1094 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1095 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1096 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1097 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1099 pp_c_type_cast (pp
, type
);
1100 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1104 /* Cast of an scalar expression to COMPLEX_TYPE. */
1105 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1106 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1108 pp_c_type_cast (pp
, type
);
1109 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1110 realexpr
= TREE_OPERAND (realexpr
, 0);
1111 pp
->expression (realexpr
);
1115 pp_c_compound_literal (pp
, e
);
1121 fixed-point-constant
1122 enumeration-constant
1123 character-constant */
1126 c_pretty_printer::constant (tree e
)
1128 const enum tree_code code
= TREE_CODE (e
);
1133 pp_c_void_constant (this);
1138 tree type
= TREE_TYPE (e
);
1139 if (type
== boolean_type_node
)
1140 pp_c_bool_constant (this, e
);
1141 else if (type
== char_type_node
)
1142 pp_c_character_constant (this, e
);
1143 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1144 && pp_c_enumeration_constant (this, e
))
1147 pp_c_integer_constant (this, e
);
1152 pp_c_floating_constant (this, e
);
1156 pp_c_fixed_constant (this, e
);
1160 pp_c_string_literal (this, e
);
1164 /* Sometimes, we are confused and we think a complex literal
1165 is a constant. Such thing is a compound literal which
1166 grammatically belongs to postfix-expr production. */
1167 pp_c_compound_literal (this, e
);
1171 pp_unsupported_tree (this, e
);
1176 /* Pretty-print a string such as an identifier, without changing its
1177 encoding, preceded by whitespace is necessary. */
1180 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1182 pp_c_maybe_whitespace (pp
);
1183 pp_string (pp
, str
);
1184 pp
->padding
= pp_before
;
1188 c_pretty_printer::translate_string (const char *gmsgid
)
1190 if (pp_translate_identifiers (this))
1191 pp_c_ws_string (this, _(gmsgid
));
1193 pp_c_ws_string (this, gmsgid
);
1196 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1197 that need converting to the locale encoding, preceded by whitespace
1201 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1203 pp_c_maybe_whitespace (pp
);
1204 pp_identifier (pp
, id
);
1205 pp
->padding
= pp_before
;
1208 /* Pretty-print a C primary-expression.
1216 c_pretty_printer::primary_expression (tree e
)
1218 switch (TREE_CODE (e
))
1226 pp_c_tree_decl_identifier (this, e
);
1229 case IDENTIFIER_NODE
:
1230 pp_c_tree_identifier (this, e
);
1234 translate_string ("<erroneous-expression>");
1238 translate_string ("<return-value>");
1250 pp_c_ws_string (this, "__builtin_memcpy");
1251 pp_c_left_paren (this);
1252 pp_ampersand (this);
1253 primary_expression (TREE_OPERAND (e
, 0));
1254 pp_separate_with (this, ',');
1255 pp_ampersand (this);
1256 initializer (TREE_OPERAND (e
, 1));
1257 if (TREE_OPERAND (e
, 2))
1259 pp_separate_with (this, ',');
1260 expression (TREE_OPERAND (e
, 2));
1262 pp_c_right_paren (this);
1266 /* FIXME: Make sure we won't get into an infinite loop. */
1267 pp_c_left_paren (this);
1269 pp_c_right_paren (this);
1274 /* Print out a C initializer -- also support C compound-literals.
1276 assignment-expression:
1277 { initializer-list }
1278 { initializer-list , } */
1281 c_pretty_printer::initializer (tree e
)
1283 if (TREE_CODE (e
) == CONSTRUCTOR
)
1284 pp_c_brace_enclosed_initializer_list (this, e
);
1291 declarator = initializer */
1294 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1297 /* We don't want to output function definitions here. There are handled
1298 elsewhere (and the syntactic form is bogus anyway). */
1299 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1301 tree init
= DECL_INITIAL (t
);
1302 /* This C++ bit is handled here because it is easier to do so.
1303 In templates, the C++ parser builds a TREE_LIST for a
1304 direct-initialization; the TREE_PURPOSE is the variable to
1305 initialize and the TREE_VALUE is the initializer. */
1306 if (TREE_CODE (init
) == TREE_LIST
)
1308 pp_c_left_paren (pp
);
1309 pp
->expression (TREE_VALUE (init
));
1310 pp_right_paren (pp
);
1317 pp
->initializer (init
);
1322 /* initializer-list:
1323 designation(opt) initializer
1324 initializer-list , designation(opt) initializer
1331 designator-list designator
1334 [ constant-expression ]
1338 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1340 tree type
= TREE_TYPE (e
);
1341 const enum tree_code code
= TREE_CODE (type
);
1343 if (TREE_CODE (e
) == CONSTRUCTOR
)
1345 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1355 tree init
= TREE_OPERAND (e
, 0);
1356 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1358 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1361 pp
->primary_expression (TREE_PURPOSE (init
));
1365 pp_c_left_bracket (pp
);
1366 if (TREE_PURPOSE (init
))
1367 pp
->constant (TREE_PURPOSE (init
));
1368 pp_c_right_bracket (pp
);
1370 pp_c_whitespace (pp
);
1372 pp_c_whitespace (pp
);
1373 pp
->initializer (TREE_VALUE (init
));
1374 if (TREE_CHAIN (init
))
1375 pp_separate_with (pp
, ',');
1381 if (TREE_CODE (e
) == VECTOR_CST
)
1383 /* We don't create variable-length VECTOR_CSTs. */
1384 unsigned int nunits
= VECTOR_CST_NELTS (e
).to_constant ();
1385 for (unsigned int i
= 0; i
< nunits
; ++i
)
1388 pp_separate_with (pp
, ',');
1389 pp
->expression (VECTOR_CST_ELT (e
, i
));
1397 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1399 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1400 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1401 pp_separate_with (pp
, ',');
1402 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1412 pp_unsupported_tree (pp
, type
);
1415 /* Pretty-print a brace-enclosed initializer-list. */
1418 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1420 pp_c_left_brace (pp
);
1421 pp_c_initializer_list (pp
, l
);
1422 pp_c_right_brace (pp
);
1426 /* This is a convenient function, used to bridge gap between C and C++
1433 c_pretty_printer::id_expression (tree t
)
1435 switch (TREE_CODE (t
))
1444 pp_c_tree_decl_identifier (this, t
);
1447 case IDENTIFIER_NODE
:
1448 pp_c_tree_identifier (this, t
);
1452 pp_unsupported_tree (this, t
);
1457 /* postfix-expression:
1459 postfix-expression [ expression ]
1460 postfix-expression ( argument-expression-list(opt) )
1461 postfix-expression . identifier
1462 postfix-expression -> identifier
1463 postfix-expression ++
1464 postfix-expression --
1465 ( type-name ) { initializer-list }
1466 ( type-name ) { initializer-list , } */
1469 c_pretty_printer::postfix_expression (tree e
)
1471 enum tree_code code
= TREE_CODE (e
);
1474 case POSTINCREMENT_EXPR
:
1475 case POSTDECREMENT_EXPR
:
1476 postfix_expression (TREE_OPERAND (e
, 0));
1477 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1481 postfix_expression (TREE_OPERAND (e
, 0));
1482 pp_c_left_bracket (this);
1483 expression (TREE_OPERAND (e
, 1));
1484 pp_c_right_bracket (this);
1489 call_expr_arg_iterator iter
;
1491 postfix_expression (CALL_EXPR_FN (e
));
1492 pp_c_left_paren (this);
1493 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1496 if (more_call_expr_args_p (&iter
))
1497 pp_separate_with (this, ',');
1499 pp_c_right_paren (this);
1503 case UNORDERED_EXPR
:
1504 pp_c_ws_string (this, flag_isoc99
1506 : "__builtin_isunordered");
1510 pp_c_ws_string (this, flag_isoc99
1512 : "!__builtin_isunordered");
1516 pp_c_ws_string (this, flag_isoc99
1518 : "!__builtin_isgreaterequal");
1522 pp_c_ws_string (this, flag_isoc99
1524 : "!__builtin_isgreater");
1528 pp_c_ws_string (this, flag_isoc99
1530 : "!__builtin_islessequal");
1534 pp_c_ws_string (this, flag_isoc99
1536 : "!__builtin_isless");
1540 pp_c_ws_string (this, flag_isoc99
1542 : "!__builtin_islessgreater");
1546 pp_c_ws_string (this, flag_isoc99
1548 : "__builtin_islessgreater");
1552 pp_c_ws_string (this, "max");
1556 pp_c_ws_string (this, "min");
1560 pp_c_left_paren (this);
1561 expression (TREE_OPERAND (e
, 0));
1562 pp_separate_with (this, ',');
1563 expression (TREE_OPERAND (e
, 1));
1564 pp_c_right_paren (this);
1568 pp_c_ws_string (this, "__builtin_abs");
1569 pp_c_left_paren (this);
1570 expression (TREE_OPERAND (e
, 0));
1571 pp_c_right_paren (this);
1576 tree object
= TREE_OPERAND (e
, 0);
1577 if (INDIRECT_REF_P (object
))
1579 postfix_expression (TREE_OPERAND (object
, 0));
1584 postfix_expression (object
);
1587 expression (TREE_OPERAND (e
, 1));
1593 tree type
= TREE_TYPE (e
);
1595 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1597 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1599 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1600 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1601 if ((bitpos
% size
) == 0)
1603 pp_c_left_paren (this);
1604 pp_c_left_paren (this);
1607 pp_c_right_paren (this);
1608 pp_c_ampersand (this);
1609 expression (TREE_OPERAND (e
, 0));
1610 pp_c_right_paren (this);
1611 pp_c_left_bracket (this);
1612 pp_wide_integer (this, bitpos
/ size
);
1613 pp_c_right_bracket (this);
1617 pp_unsupported_tree (this, e
);
1627 pp_c_compound_literal (this, e
);
1631 pp_c_complex_expr (this, e
);
1634 case COMPOUND_LITERAL_EXPR
:
1635 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1642 pp_c_ws_string (this, "__builtin_va_arg");
1643 pp_c_left_paren (this);
1644 assignment_expression (TREE_OPERAND (e
, 0));
1645 pp_separate_with (this, ',');
1646 type_id (TREE_TYPE (e
));
1647 pp_c_right_paren (this);
1651 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1653 id_expression (TREE_OPERAND (e
, 0));
1659 primary_expression (e
);
1664 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1667 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1669 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1671 pp
->expression (TREE_VALUE (e
));
1673 pp_separate_with (pp
, ',');
1677 /* Print out V, which contains the elements of a constructor. */
1680 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1682 unsigned HOST_WIDE_INT ix
;
1685 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1687 pp
->expression (value
);
1688 if (ix
!= vec_safe_length (v
) - 1)
1689 pp_separate_with (pp
, ',');
1693 /* Print out an expression-list in parens, as if it were the argument
1694 list to a function. */
1697 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1699 pp_c_left_paren (pp
);
1700 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1701 pp_c_expression_list (pp
, t
);
1702 pp_c_right_paren (pp
);
1705 /* unary-expression:
1709 unary-operator cast-expression
1710 sizeof unary-expression
1713 unary-operator: one of
1718 __alignof__ unary-expression
1719 __alignof__ ( type-id )
1720 __real__ unary-expression
1721 __imag__ unary-expression */
1724 c_pretty_printer::unary_expression (tree e
)
1726 enum tree_code code
= TREE_CODE (e
);
1729 case PREINCREMENT_EXPR
:
1730 case PREDECREMENT_EXPR
:
1731 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1732 unary_expression (TREE_OPERAND (e
, 0));
1739 case TRUTH_NOT_EXPR
:
1741 /* String literal are used by address. */
1742 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1743 pp_ampersand (this);
1744 else if (code
== INDIRECT_REF
)
1746 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
1747 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
1748 /* Reference decay is implicit, don't print anything. */;
1752 else if (code
== NEGATE_EXPR
)
1754 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1755 pp_complement (this);
1756 else if (code
== TRUTH_NOT_EXPR
)
1757 pp_exclamation (this);
1758 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1762 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1763 && integer_zerop (TREE_OPERAND (e
, 1)))
1764 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1768 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1770 pp_c_left_paren (this);
1771 if (!integer_onep (TYPE_SIZE_UNIT
1772 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1773 pp_c_type_cast (this, ptr_type_node
);
1775 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1776 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1779 pp_c_integer_constant (this,
1780 fold_convert (ssizetype
,
1781 TREE_OPERAND (e
, 1)));
1782 pp_c_right_paren (this);
1789 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1790 pp_c_whitespace (this);
1791 unary_expression (TREE_OPERAND (e
, 0));
1795 postfix_expression (e
);
1802 ( type-name ) cast-expression */
1805 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1807 switch (TREE_CODE (e
))
1810 case FIX_TRUNC_EXPR
:
1812 case VIEW_CONVERT_EXPR
:
1813 if (!location_wrapper_p (e
))
1814 pp_c_type_cast (pp
, TREE_TYPE (e
));
1815 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1819 pp
->unary_expression (e
);
1823 /* multiplicative-expression:
1825 multiplicative-expression * cast-expression
1826 multiplicative-expression / cast-expression
1827 multiplicative-expression % cast-expression */
1830 c_pretty_printer::multiplicative_expression (tree e
)
1832 enum tree_code code
= TREE_CODE (e
);
1836 case TRUNC_DIV_EXPR
:
1837 case TRUNC_MOD_EXPR
:
1838 case EXACT_DIV_EXPR
:
1840 multiplicative_expression (TREE_OPERAND (e
, 0));
1841 pp_c_whitespace (this);
1842 if (code
== MULT_EXPR
)
1844 else if (code
== TRUNC_DIV_EXPR
)
1848 pp_c_whitespace (this);
1849 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1853 pp_c_cast_expression (this, e
);
1858 /* additive-expression:
1859 multiplicative-expression
1860 additive-expression + multiplicative-expression
1861 additive-expression - multiplicative-expression */
1864 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1866 enum tree_code code
= TREE_CODE (e
);
1869 case POINTER_PLUS_EXPR
:
1871 case POINTER_DIFF_EXPR
:
1873 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1874 pp_c_whitespace (pp
);
1875 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1879 pp_c_whitespace (pp
);
1880 pp
->multiplicative_expression (TREE_OPERAND (e
, 1));
1884 pp
->multiplicative_expression (e
);
1889 /* additive-expression:
1891 shift-expression << additive-expression
1892 shift-expression >> additive-expression */
1895 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1897 enum tree_code code
= TREE_CODE (e
);
1904 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1905 pp_c_whitespace (pp
);
1906 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
1907 code
== RSHIFT_EXPR
? ">>" :
1908 code
== LROTATE_EXPR
? "<<<" : ">>>");
1909 pp_c_whitespace (pp
);
1910 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1914 pp_c_additive_expression (pp
, e
);
1918 /* relational-expression:
1920 relational-expression < shift-expression
1921 relational-expression > shift-expression
1922 relational-expression <= shift-expression
1923 relational-expression >= shift-expression */
1926 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1928 enum tree_code code
= TREE_CODE (e
);
1935 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1936 pp_c_whitespace (pp
);
1937 if (code
== LT_EXPR
)
1939 else if (code
== GT_EXPR
)
1941 else if (code
== LE_EXPR
)
1943 else if (code
== GE_EXPR
)
1944 pp_greater_equal (pp
);
1945 pp_c_whitespace (pp
);
1946 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1950 pp_c_shift_expression (pp
, e
);
1955 /* equality-expression:
1956 relational-expression
1957 equality-expression == relational-expression
1958 equality-equality != relational-expression */
1961 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1963 enum tree_code code
= TREE_CODE (e
);
1968 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1969 pp_c_whitespace (pp
);
1970 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1971 pp_c_whitespace (pp
);
1972 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1976 pp_c_relational_expression (pp
, e
);
1983 AND-expression & equality-equality */
1986 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1988 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1990 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1991 pp_c_whitespace (pp
);
1993 pp_c_whitespace (pp
);
1994 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1997 pp_c_equality_expression (pp
, e
);
2000 /* exclusive-OR-expression:
2002 exclusive-OR-expression ^ AND-expression */
2005 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2007 if (TREE_CODE (e
) == BIT_XOR_EXPR
2008 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2010 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2011 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2012 pp_c_maybe_whitespace (pp
);
2014 pp_c_whitespace (pp
);
2016 pp_c_whitespace (pp
);
2017 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2020 pp_c_and_expression (pp
, e
);
2023 /* inclusive-OR-expression:
2024 exclusive-OR-expression
2025 inclusive-OR-expression | exclusive-OR-expression */
2028 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2030 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2032 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2033 pp_c_whitespace (pp
);
2035 pp_c_whitespace (pp
);
2036 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2039 pp_c_exclusive_or_expression (pp
, e
);
2042 /* logical-AND-expression:
2043 inclusive-OR-expression
2044 logical-AND-expression && inclusive-OR-expression */
2047 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2049 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2050 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2052 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2053 pp_c_whitespace (pp
);
2054 pp_ampersand_ampersand (pp
);
2055 pp_c_whitespace (pp
);
2056 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2059 pp_c_inclusive_or_expression (pp
, e
);
2062 /* logical-OR-expression:
2063 logical-AND-expression
2064 logical-OR-expression || logical-AND-expression */
2067 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2069 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2070 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2072 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2073 pp_c_whitespace (pp
);
2075 pp_c_whitespace (pp
);
2076 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2079 pp_c_logical_and_expression (pp
, e
);
2082 /* conditional-expression:
2083 logical-OR-expression
2084 logical-OR-expression ? expression : conditional-expression */
2087 c_pretty_printer::conditional_expression (tree e
)
2089 if (TREE_CODE (e
) == COND_EXPR
)
2091 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2092 pp_c_whitespace (this);
2094 pp_c_whitespace (this);
2095 expression (TREE_OPERAND (e
, 1));
2096 pp_c_whitespace (this);
2098 pp_c_whitespace (this);
2099 conditional_expression (TREE_OPERAND (e
, 2));
2102 pp_c_logical_or_expression (this, e
);
2106 /* assignment-expression:
2107 conditional-expression
2108 unary-expression assignment-operator assignment-expression
2110 assignment-expression: one of
2111 = *= /= %= += -= >>= <<= &= ^= |= */
2114 c_pretty_printer::assignment_expression (tree e
)
2116 if (TREE_CODE (e
) == MODIFY_EXPR
2117 || TREE_CODE (e
) == INIT_EXPR
)
2119 unary_expression (TREE_OPERAND (e
, 0));
2120 pp_c_whitespace (this);
2123 expression (TREE_OPERAND (e
, 1));
2126 conditional_expression (e
);
2130 assignment-expression
2131 expression , assignment-expression
2133 Implementation note: instead of going through the usual recursion
2134 chain, I take the liberty of dispatching nodes to the appropriate
2135 functions. This makes some redundancy, but it worths it. That also
2136 prevents a possible infinite recursion between primary_expression ()
2137 and expression (). */
2140 c_pretty_printer::expression (tree e
)
2142 switch (TREE_CODE (e
))
2145 pp_c_void_constant (this);
2149 pp_c_integer_constant (this, e
);
2153 pp_c_floating_constant (this, e
);
2157 pp_c_fixed_constant (this, e
);
2161 pp_c_string_literal (this, e
);
2164 case IDENTIFIER_NODE
:
2173 primary_expression (e
);
2177 if (SSA_NAME_VAR (e
)
2178 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2179 expression (SSA_NAME_VAR (e
));
2181 translate_string ("<unknown>");
2184 case POSTINCREMENT_EXPR
:
2185 case POSTDECREMENT_EXPR
:
2194 case UNORDERED_EXPR
:
2205 case COMPOUND_LITERAL_EXPR
:
2207 postfix_expression (e
);
2216 case TRUTH_NOT_EXPR
:
2217 case PREINCREMENT_EXPR
:
2218 case PREDECREMENT_EXPR
:
2221 unary_expression (e
);
2225 case FIX_TRUNC_EXPR
:
2227 case VIEW_CONVERT_EXPR
:
2228 pp_c_cast_expression (this, e
);
2232 case TRUNC_MOD_EXPR
:
2233 case TRUNC_DIV_EXPR
:
2234 case EXACT_DIV_EXPR
:
2236 multiplicative_expression (e
);
2243 pp_c_shift_expression (this, e
);
2250 pp_c_relational_expression (this, e
);
2254 pp_c_and_expression (this, e
);
2258 case TRUTH_XOR_EXPR
:
2259 pp_c_exclusive_or_expression (this, e
);
2263 pp_c_inclusive_or_expression (this, e
);
2266 case TRUTH_ANDIF_EXPR
:
2267 case TRUTH_AND_EXPR
:
2268 pp_c_logical_and_expression (this, e
);
2271 case TRUTH_ORIF_EXPR
:
2273 pp_c_logical_or_expression (this, e
);
2278 pp_c_equality_expression (this, e
);
2282 conditional_expression (e
);
2285 case POINTER_PLUS_EXPR
:
2287 case POINTER_DIFF_EXPR
:
2289 pp_c_additive_expression (this, e
);
2294 assignment_expression (e
);
2298 pp_c_left_paren (this);
2299 expression (TREE_OPERAND (e
, 0));
2300 pp_separate_with (this, ',');
2301 assignment_expression (TREE_OPERAND (e
, 1));
2302 pp_c_right_paren (this);
2305 case NON_LVALUE_EXPR
:
2307 expression (TREE_OPERAND (e
, 0));
2311 postfix_expression (TREE_OPERAND (e
, 1));
2316 /* We don't yet have a way of dumping statements in a
2317 human-readable format. */
2318 pp_string (this, "({...})");
2321 case C_MAYBE_CONST_EXPR
:
2322 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2326 pp_unsupported_tree (this, e
);
2336 c_pretty_printer::statement (tree stmt
)
2341 if (pp_needs_newline (this))
2342 pp_newline_and_indent (this, 0);
2344 dump_generic_node (this, stmt
, pp_indentation (this), 0, true);
2348 /* Initialize the PRETTY-PRINTER for handling C codes. */
2350 c_pretty_printer::c_pretty_printer ()
2351 : pretty_printer (),
2355 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2356 ptr_operator
= pp_c_pointer
;
2357 parameter_list
= pp_c_parameter_type_list
;
2361 /* Print the tree T in full, on file FILE. */
2364 print_c_tree (FILE *file
, tree t
)
2366 c_pretty_printer pp
;
2368 pp_needs_newline (&pp
) = true;
2369 pp
.buffer
->stream
= file
;
2371 pp_newline_and_flush (&pp
);
2374 /* Print the tree T in full, on stderr. */
2377 debug_c_tree (tree t
)
2379 print_c_tree (stderr
, t
);
2380 fputc ('\n', stderr
);
2383 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2384 up of T's memory address. */
2387 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2391 gcc_assert (DECL_P (t
));
2394 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2397 static char xname
[8];
2398 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2403 pp_c_identifier (pp
, name
);
2408 namespace selftest
{
2410 /* Selftests for pretty-printing trees. */
2412 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2413 LOC as the effective location for any failures. */
2416 assert_c_pretty_printer_output (const location
&loc
, const char *expected
,
2419 c_pretty_printer pp
;
2420 pp
.expression (expr
);
2421 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2424 /* Helper function for calling assert_c_pretty_printer_output.
2425 This is to avoid having to write SELFTEST_LOCATION. */
2427 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2428 SELFTEST_BEGIN_STMT \
2429 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
2434 /* Verify that location wrappers don't show up in pretty-printed output. */
2437 test_location_wrappers ()
2440 tree id
= get_identifier ("foo");
2441 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, id
,
2443 tree wrapped_decl
= maybe_wrap_with_location (decl
, BUILTINS_LOCATION
);
2444 ASSERT_NE (wrapped_decl
, decl
);
2445 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl
);
2446 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl
);
2449 tree int_cst
= build_int_cst (integer_type_node
, 42);
2450 tree wrapped_cst
= maybe_wrap_with_location (int_cst
, BUILTINS_LOCATION
);
2451 ASSERT_NE (wrapped_cst
, int_cst
);
2452 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst
);
2453 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst
);
2456 /* Run all of the selftests within this file. */
2459 c_pretty_print_c_tests ()
2461 test_location_wrappers ();
2464 } // namespace selftest
2466 #endif /* CHECKING_P */