1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2022 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 "gimple-pretty-print.h"
26 #include "diagnostic.h"
27 #include "stor-layout.h"
28 #include "stringpool.h"
31 #include "tree-pretty-print.h"
33 #include "langhooks.h"
35 #include "internal-fn.h"
37 /* The pretty-printer code is primarily designed to closely follow
38 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
39 codes we used to have in the past. Following a structured
40 approach (preferably the official grammars) is believed to make it
41 much easier to add extensions and nifty pretty-printing effects that
42 takes expression or declaration contexts into account. */
45 #define pp_c_maybe_whitespace(PP) \
47 if ((PP)->padding == pp_before) \
48 pp_c_whitespace (PP); \
52 static void pp_c_char (c_pretty_printer
*, int);
54 /* postfix-expression */
55 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
56 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
58 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
59 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
60 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
61 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
62 static void pp_c_and_expression (c_pretty_printer
*, tree
);
63 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
64 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
65 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
70 /* Helper functions. */
73 pp_c_whitespace (c_pretty_printer
*pp
)
76 pp
->padding
= pp_none
;
80 pp_c_left_paren (c_pretty_printer
*pp
)
83 pp
->padding
= pp_none
;
87 pp_c_right_paren (c_pretty_printer
*pp
)
90 pp
->padding
= pp_none
;
94 pp_c_left_brace (c_pretty_printer
*pp
)
97 pp
->padding
= pp_none
;
101 pp_c_right_brace (c_pretty_printer
*pp
)
104 pp
->padding
= pp_none
;
108 pp_c_left_bracket (c_pretty_printer
*pp
)
110 pp_left_bracket (pp
);
111 pp
->padding
= pp_none
;
115 pp_c_right_bracket (c_pretty_printer
*pp
)
117 pp_right_bracket (pp
);
118 pp
->padding
= pp_none
;
122 pp_c_dot (c_pretty_printer
*pp
)
125 pp
->padding
= pp_none
;
129 pp_c_ampersand (c_pretty_printer
*pp
)
132 pp
->padding
= pp_none
;
136 pp_c_star (c_pretty_printer
*pp
)
139 pp
->padding
= pp_none
;
143 pp_c_arrow (c_pretty_printer
*pp
)
146 pp
->padding
= pp_none
;
150 pp_c_semicolon (c_pretty_printer
*pp
)
153 pp
->padding
= pp_none
;
157 pp_c_complement (c_pretty_printer
*pp
)
160 pp
->padding
= pp_none
;
164 pp_c_exclamation (c_pretty_printer
*pp
)
167 pp
->padding
= pp_none
;
170 /* Print out the external representation of QUALIFIERS. */
173 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
175 const char *p
= pp_last_position_in_text (pp
);
180 /* The C programming language does not have references, but it is much
181 simpler to handle those here rather than going through the same
182 logic in the C++ pretty-printer. */
183 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
184 pp_c_whitespace (pp
);
186 if (qualifiers
& TYPE_QUAL_ATOMIC
)
187 pp_c_ws_string (pp
, "_Atomic");
188 if (qualifiers
& TYPE_QUAL_CONST
)
189 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
190 if (qualifiers
& TYPE_QUAL_VOLATILE
)
191 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
192 if (qualifiers
& TYPE_QUAL_RESTRICT
)
193 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
194 ? "restrict" : "__restrict__"));
197 /* Pretty-print T using the type-cast notation '( type-name )'. */
200 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
202 pp_c_left_paren (pp
);
204 pp_c_right_paren (pp
);
207 /* We're about to pretty-print a pointer type as indicated by T.
208 Output a whitespace, if needed, preparing for subsequent output. */
211 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
213 if (POINTER_TYPE_P (t
))
215 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
216 if (TREE_CODE (pointee
) != ARRAY_TYPE
217 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
218 pp_c_whitespace (pp
);
225 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
226 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
227 of its type. Take care of possible extensions.
231 type-qualifier-list type-qualifier
236 __restrict__ -- GNU C
237 address-space-qualifier -- GNU C
241 address-space-qualifier:
242 identifier -- GNU C */
245 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
249 if (!t
|| t
== error_mark_node
)
255 if (TREE_CODE (t
) != ARRAY_TYPE
)
257 qualifiers
= TYPE_QUALS (t
);
258 pp_c_cv_qualifiers (pp
, qualifiers
,
259 TREE_CODE (t
) == FUNCTION_TYPE
);
262 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
264 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
265 pp_c_identifier (pp
, as
);
270 * type-qualifier-list(opt)
271 * type-qualifier-list(opt) pointer */
274 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
276 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
278 switch (TREE_CODE (t
))
281 /* It is easier to handle C++ reference types here. */
283 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
284 pp_c_pointer (pp
, TREE_TYPE (t
));
285 if (TREE_CODE (t
) == POINTER_TYPE
)
290 if (TYPE_REF_IS_RVALUE (t
))
293 pp_c_type_qualifier_list (pp
, t
);
296 /* ??? This node is now in GENERIC and so shouldn't be here. But
297 we'll fix that later. */
299 pp
->declaration (DECL_EXPR_DECL (t
));
300 pp_needs_newline (pp
) = true;
304 pp_unsupported_tree (pp
, t
);
308 /* simple-type-specifier:
325 struct-or-union-specifier
330 simple-type-specifier:
335 c_pretty_printer::simple_type_specifier (tree t
)
337 const enum tree_code code
= TREE_CODE (t
);
341 translate_string ("<type-error>");
344 case IDENTIFIER_NODE
:
345 pp_c_identifier (this, IDENTIFIER_POINTER (t
));
353 case FIXED_POINT_TYPE
:
357 simple_type_specifier (t
);
361 int prec
= TYPE_PRECISION (t
);
363 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
364 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
365 TYPE_SATURATING (t
));
367 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
369 if (common_t
&& TYPE_NAME (common_t
))
371 simple_type_specifier (common_t
);
372 if (TYPE_PRECISION (common_t
) != prec
)
375 pp_decimal_int (this, prec
);
383 translate_string (TYPE_UNSIGNED (t
)
384 ? "<unnamed-unsigned:"
385 : "<unnamed-signed:");
388 translate_string ("<unnamed-float:");
390 case FIXED_POINT_TYPE
:
391 translate_string ("<unnamed-fixed:");
396 pp_decimal_int (this, prec
);
406 translate_string ("<typedef-error>");
412 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
413 /* Don't decorate the type if this is a typedef name. */;
414 else if (code
== UNION_TYPE
)
415 pp_c_ws_string (this, "union");
416 else if (code
== RECORD_TYPE
)
417 pp_c_ws_string (this, "struct");
418 else if (code
== ENUMERAL_TYPE
)
419 pp_c_ws_string (this, "enum");
421 translate_string ("<tag-error>");
424 id_expression (TYPE_NAME (t
));
426 translate_string ("<anonymous>");
429 pp_c_ws_string (this, "nullptr_t");
433 pp_unsupported_tree (this, t
);
438 /* specifier-qualifier-list:
439 type-specifier specifier-qualifier-list-opt
440 type-qualifier specifier-qualifier-list-opt
443 Implementation note: Because of the non-linearities in array or
444 function declarations, this routine prints not just the
445 specifier-qualifier-list of such entities or types of such entities,
446 but also the 'pointer' production part of their declarators. The
447 remaining part is done by declarator() or abstract_declarator(). */
450 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
452 const enum tree_code code
= TREE_CODE (t
);
454 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
455 pp_c_type_qualifier_list (pp
, t
);
461 /* Get the types-specifier of this type. */
462 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
463 pp_c_specifier_qualifier_list (pp
, pointee
);
464 if (TREE_CODE (pointee
) == ARRAY_TYPE
465 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
467 pp_c_whitespace (pp
);
468 pp_c_left_paren (pp
);
469 /* If we're dealing with the GNU form of attributes, print this:
470 void (__attribute__((noreturn)) *f) ();
471 If it is the standard [[]] attribute, we'll print the attribute
472 in c_pretty_printer::direct_abstract_declarator/FUNCTION_TYPE. */
473 if (!cxx11_attribute_p (TYPE_ATTRIBUTES (pointee
)))
474 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
476 else if (!c_dialect_cxx ())
477 pp_c_whitespace (pp
);
478 pp_ptr_operator (pp
, t
);
484 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
489 if (code
== COMPLEX_TYPE
)
490 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
491 ? "_Complex" : "__complex__"));
492 else if (code
== VECTOR_TYPE
)
494 /* The syntax we print for vector types isn't real C or C++ syntax,
495 so it's better to print the type name if we have one. */
496 tree name
= TYPE_NAME (t
);
497 if (!(pp
->flags
& pp_c_flag_gnu_v3
)
499 && TREE_CODE (name
) == TYPE_DECL
)
501 pp
->id_expression (name
);
504 pp_c_ws_string (pp
, "__vector");
505 pp_c_left_paren (pp
);
506 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
507 pp_c_right_paren (pp
);
508 pp_c_whitespace (pp
);
510 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
514 pp
->simple_type_specifier (t
);
517 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
518 pp_c_type_qualifier_list (pp
, t
);
521 /* parameter-type-list:
526 parameter-declaration
527 parameter-list , parameter-declaration
529 parameter-declaration:
530 declaration-specifiers declarator
531 declaration-specifiers abstract-declarator(opt) */
534 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
536 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
537 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
538 pp_c_left_paren (pp
);
539 if (parms
== void_list_node
)
540 pp_c_ws_string (pp
, "void");
544 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
547 pp_separate_with (pp
, ',');
549 pp
->declaration_specifiers
550 (want_parm_decl
? parms
: TREE_VALUE (parms
));
552 pp
->declarator (parms
);
554 pp
->abstract_declarator (TREE_VALUE (parms
));
556 if (!first
&& !parms
)
558 pp_separate_with (pp
, ',');
559 pp_string (pp
, "...");
562 pp_c_right_paren (pp
);
565 /* abstract-declarator:
567 pointer(opt) direct-abstract-declarator */
570 c_pretty_printer::abstract_declarator (tree t
)
572 if (TREE_CODE (t
) == POINTER_TYPE
)
574 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
575 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
576 pp_c_right_paren (this);
580 direct_abstract_declarator (t
);
583 /* direct-abstract-declarator:
584 ( abstract-declarator )
585 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
586 direct-abstract-declarator(opt) [ * ]
587 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
590 c_pretty_printer::direct_abstract_declarator (tree t
)
592 bool add_space
= false;
594 switch (TREE_CODE (t
))
597 abstract_declarator (t
);
601 pp_c_parameter_type_list (this, t
);
602 direct_abstract_declarator (TREE_TYPE (t
));
603 /* If this is the standard [[]] attribute, print
604 void (*)() [[noreturn]]; */
605 if (cxx11_attribute_p (TYPE_ATTRIBUTES (t
)))
608 pp_c_attributes_display (this, TYPE_ATTRIBUTES (t
));
613 pp_c_left_bracket (this);
615 if (int quals
= TYPE_QUALS (t
))
617 /* Print the array qualifiers such as in "T[const restrict 3]". */
618 pp_c_cv_qualifiers (this, quals
, false);
622 if (tree arr
= lookup_attribute ("array", TYPE_ATTRIBUTES (t
)))
624 if (TREE_VALUE (arr
))
626 /* Print the specifier as in "T[static 3]" that's not actually
627 part of the type but may be added by the front end. */
628 pp_c_ws_string (this, "static");
631 else if (!TYPE_DOMAIN (t
))
632 /* For arrays of unspecified bound using the [*] notation. */
633 pp_character (this, '*');
636 if (tree dom
= TYPE_DOMAIN (t
))
638 if (tree maxval
= TYPE_MAX_VALUE (dom
))
643 tree type
= TREE_TYPE (maxval
);
645 if (tree_fits_shwi_p (maxval
))
646 pp_wide_integer (this, tree_to_shwi (maxval
) + 1);
647 else if (TREE_CODE (maxval
) == INTEGER_CST
)
648 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
649 build_int_cst (type
, 1)));
652 /* Strip the expressions from around a VLA bound added
653 internally to make it fit the domain mold, including
655 if (TREE_CODE (maxval
) == NOP_EXPR
)
656 maxval
= TREE_OPERAND (maxval
, 0);
657 if (TREE_CODE (maxval
) == PLUS_EXPR
658 && integer_all_onesp (TREE_OPERAND (maxval
, 1)))
660 maxval
= TREE_OPERAND (maxval
, 0);
661 if (TREE_CODE (maxval
) == NOP_EXPR
)
662 maxval
= TREE_OPERAND (maxval
, 0);
664 if (TREE_CODE (maxval
) == SAVE_EXPR
)
666 maxval
= TREE_OPERAND (maxval
, 0);
667 if (TREE_CODE (maxval
) == NOP_EXPR
)
668 maxval
= TREE_OPERAND (maxval
, 0);
674 else if (TYPE_SIZE (t
))
675 /* Print zero for zero-length arrays but not for flexible
676 array members whose TYPE_SIZE is null. */
677 pp_string (this, "0");
679 pp_c_right_bracket (this);
680 direct_abstract_declarator (TREE_TYPE (t
));
683 case IDENTIFIER_NODE
:
689 case FIXED_POINT_TYPE
:
701 pp_unsupported_tree (this, t
);
707 specifier-qualifier-list abstract-declarator(opt) */
710 c_pretty_printer::type_id (tree t
)
712 pp_c_specifier_qualifier_list (this, t
);
713 abstract_declarator (t
);
716 /* storage-class-specifier:
724 c_pretty_printer::storage_class_specifier (tree t
)
726 if (TREE_CODE (t
) == TYPE_DECL
)
727 pp_c_ws_string (this, "typedef");
730 if (DECL_REGISTER (t
))
731 pp_c_ws_string (this, "register");
732 else if (TREE_STATIC (t
) && VAR_P (t
))
733 pp_c_ws_string (this, "static");
737 /* function-specifier:
741 c_pretty_printer::function_specifier (tree t
)
743 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
744 pp_c_ws_string (this, "inline");
747 /* declaration-specifiers:
748 storage-class-specifier declaration-specifiers(opt)
749 type-specifier declaration-specifiers(opt)
750 type-qualifier declaration-specifiers(opt)
751 function-specifier declaration-specifiers(opt) */
754 c_pretty_printer::declaration_specifiers (tree t
)
756 storage_class_specifier (t
);
757 function_specifier (t
);
758 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
764 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
765 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
766 direct-declarator [ type-qualifier-list static assignment-expression ]
767 direct-declarator [ type-qualifier-list * ]
768 direct-declarator ( parameter-type-list )
769 direct-declarator ( identifier-list(opt) ) */
772 c_pretty_printer::direct_declarator (tree t
)
774 switch (TREE_CODE (t
))
781 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
782 pp_c_tree_decl_identifier (this, t
);
787 abstract_declarator (TREE_TYPE (t
));
791 pp_parameter_list (this, t
);
792 abstract_declarator (TREE_TYPE (t
));
796 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
797 pp_c_tree_decl_identifier (this, t
);
798 if (flags
& pp_c_flag_abstract
)
799 abstract_declarator (TREE_TYPE (t
));
802 pp_parameter_list (this, t
);
803 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
809 case FIXED_POINT_TYPE
:
816 pp_unsupported_tree (this, t
);
823 pointer(opt) direct-declarator */
826 c_pretty_printer::declarator (tree t
)
828 switch (TREE_CODE (t
))
832 case FIXED_POINT_TYPE
:
845 direct_declarator (t
);
850 pp_unsupported_tree (this, t
);
856 declaration-specifiers init-declarator-list(opt) ; */
859 c_pretty_printer::declaration (tree t
)
861 declaration_specifiers (t
);
862 pp_c_init_declarator (this, t
);
865 /* Pretty-print ATTRIBUTES marked to be displayed on diagnostic. */
868 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
870 bool is_first
= true;
875 const bool std_p
= cxx11_attribute_p (a
);
877 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
879 const struct attribute_spec
*as
880 = lookup_attribute_spec (get_attribute_name (a
));
881 if (!as
|| as
->affects_type_identity
== false)
884 && !strcmp ("transaction_safe", as
->name
))
885 /* In C++ transaction_safe is printed at the end of the declarator. */
891 pp_c_left_bracket (pp
);
892 pp_c_left_bracket (pp
);
896 pp_c_ws_string (pp
, "__attribute__");
897 pp_c_left_paren (pp
);
898 pp_c_left_paren (pp
);
903 pp_separate_with (pp
, ',');
905 if (std_p
&& (ns
= get_attribute_namespace (a
)))
907 pp_tree_identifier (pp
, ns
);
911 pp_tree_identifier (pp
, get_attribute_name (a
));
913 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
920 pp_c_right_bracket (pp
);
921 pp_c_right_bracket (pp
);
925 pp_c_right_paren (pp
);
926 pp_c_right_paren (pp
);
927 pp_c_whitespace (pp
);
932 /* function-definition:
933 declaration-specifiers declarator compound-statement */
936 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
938 pp
->declaration_specifiers (t
);
940 pp_needs_newline (pp
) = true;
941 pp
->statement (DECL_SAVED_TREE (t
));
942 pp_newline_and_flush (pp
);
948 /* Print out a c-char. This is called solely for characters which are
949 in the *target* execution character set. We ought to convert them
950 back to the *host* execution character set before printing, but we
951 have no way to do this at present. A decent compromise is to print
952 all characters as if they were in the host execution character set,
953 and not attempt to recover any named escape characters, but render
954 all unprintables as octal escapes. If the host and target character
955 sets are the same, this produces relatively readable output. If they
956 are not the same, strings may appear as gibberish, but that's okay
957 (in fact, it may well be what the reader wants, e.g. if they are looking
958 to see if conversion to the target character set happened correctly).
960 A special case: we need to prefix \, ", and ' with backslashes. It is
961 correct to do so for the *host*'s \, ", and ', because the rest of the
962 file appears in the host character set. */
965 pp_c_char (c_pretty_printer
*pp
, int c
)
971 case '\\': pp_string (pp
, "\\\\"); break;
972 case '\'': pp_string (pp
, "\\\'"); break;
973 case '\"': pp_string (pp
, "\\\""); break;
974 default: pp_character (pp
, c
);
978 pp_scalar (pp
, "\\%03o", (unsigned) c
);
981 /* Print out a STRING literal. */
984 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
986 const char *p
= TREE_STRING_POINTER (s
);
987 int n
= TREE_STRING_LENGTH (s
) - 1;
990 for (i
= 0; i
< n
; ++i
)
991 pp_c_char (pp
, p
[i
]);
995 /* Pretty-print a VOID_CST (void_node). */
998 pp_c_void_constant (c_pretty_printer
*pp
)
1000 pp_c_type_cast (pp
, void_type_node
);
1001 pp_string (pp
, "0");
1004 /* Pretty-print an INTEGER literal. */
1007 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
1009 if (tree_fits_shwi_p (i
))
1010 pp_wide_integer (pp
, tree_to_shwi (i
));
1011 else if (tree_fits_uhwi_p (i
))
1012 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
1015 wide_int wi
= wi::to_wide (i
);
1017 if (wi::lt_p (wi::to_wide (i
), 0, TYPE_SIGN (TREE_TYPE (i
))))
1022 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
1023 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
1027 /* Print out a CHARACTER literal. */
1030 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
1033 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
1037 /* Print out a BOOLEAN literal. */
1040 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
1042 if (b
== boolean_false_node
)
1044 if (c_dialect_cxx ())
1045 pp_c_ws_string (pp
, "false");
1046 else if (flag_isoc99
)
1047 pp_c_ws_string (pp
, "_False");
1049 pp_unsupported_tree (pp
, b
);
1051 else if (b
== boolean_true_node
)
1053 if (c_dialect_cxx ())
1054 pp_c_ws_string (pp
, "true");
1055 else if (flag_isoc99
)
1056 pp_c_ws_string (pp
, "_True");
1058 pp_unsupported_tree (pp
, b
);
1060 else if (TREE_CODE (b
) == INTEGER_CST
)
1061 pp_c_integer_constant (pp
, b
);
1063 pp_unsupported_tree (pp
, b
);
1066 /* Given a value e of ENUMERAL_TYPE:
1067 Print out the first ENUMERATOR id with value e, if one is found,
1068 else print out the value as a C-style cast (type-id)value. */
1071 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
1073 tree type
= TREE_TYPE (e
);
1074 tree value
= NULL_TREE
;
1076 /* Find the name of this constant. */
1077 if ((pp
->flags
& pp_c_flag_gnu_v3
) == 0)
1078 for (value
= TYPE_VALUES (type
); value
!= NULL_TREE
;
1079 value
= TREE_CHAIN (value
))
1080 if (tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value
)), e
))
1083 if (value
!= NULL_TREE
)
1084 pp
->id_expression (TREE_PURPOSE (value
));
1087 /* Value must have been cast. */
1088 pp_c_type_cast (pp
, type
);
1089 pp_c_integer_constant (pp
, e
);
1093 /* Print out a REAL value as a decimal-floating-constant. */
1096 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1098 const struct real_format
*fmt
1099 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1101 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1102 bool is_decimal
= floating_cst
.decimal
;
1104 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1105 log10(2) to 7 significant digits. */
1106 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1108 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1109 sizeof (pp_buffer (pp
)->digit_buffer
),
1112 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1113 if (TREE_TYPE (r
) == float_type_node
)
1114 pp_character (pp
, 'f');
1115 else if (TREE_TYPE (r
) == long_double_type_node
)
1116 pp_character (pp
, 'l');
1117 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1118 pp_string (pp
, "dl");
1119 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1120 pp_string (pp
, "dd");
1121 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1122 pp_string (pp
, "df");
1123 else if (TREE_TYPE (r
) != double_type_node
)
1124 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1125 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1127 pp_character (pp
, 'f');
1128 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1129 if (floatn_nx_types
[i
].extended
)
1130 pp_character (pp
, 'x');
1135 /* Print out a FIXED value as a decimal-floating-constant. */
1138 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1140 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1141 sizeof (pp_buffer (pp
)->digit_buffer
));
1142 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1145 /* Pretty-print a compound literal expression. GNU extensions include
1146 vector constants. */
1149 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1151 tree type
= TREE_TYPE (e
);
1152 pp_c_type_cast (pp
, type
);
1154 switch (TREE_CODE (type
))
1161 pp_c_brace_enclosed_initializer_list (pp
, e
);
1165 pp_unsupported_tree (pp
, e
);
1170 /* Pretty-print a COMPLEX_EXPR expression. */
1173 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1175 /* Handle a few common special cases, otherwise fallback
1176 to printing it as compound literal. */
1177 tree type
= TREE_TYPE (e
);
1178 tree realexpr
= TREE_OPERAND (e
, 0);
1179 tree imagexpr
= TREE_OPERAND (e
, 1);
1181 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1182 if (TREE_CODE (realexpr
) == NOP_EXPR
1183 && TREE_CODE (imagexpr
) == NOP_EXPR
1184 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1185 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1186 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1187 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1188 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1189 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1191 pp_c_type_cast (pp
, type
);
1192 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1196 /* Cast of an scalar expression to COMPLEX_TYPE. */
1197 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1198 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1200 pp_c_type_cast (pp
, type
);
1201 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1202 realexpr
= TREE_OPERAND (realexpr
, 0);
1203 pp
->expression (realexpr
);
1207 pp_c_compound_literal (pp
, e
);
1213 fixed-point-constant
1214 enumeration-constant
1215 character-constant */
1218 c_pretty_printer::constant (tree e
)
1220 const enum tree_code code
= TREE_CODE (e
);
1225 pp_c_void_constant (this);
1230 tree type
= TREE_TYPE (e
);
1231 if (type
== boolean_type_node
)
1232 pp_c_bool_constant (this, e
);
1233 else if (type
== char_type_node
)
1234 pp_c_character_constant (this, e
);
1235 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1236 pp_c_enumeration_constant (this, e
);
1237 else if (NULLPTR_TYPE_P (type
))
1238 pp_string (this, "nullptr");
1240 pp_c_integer_constant (this, e
);
1245 pp_c_floating_constant (this, e
);
1249 pp_c_fixed_constant (this, e
);
1253 pp_c_string_literal (this, e
);
1257 /* Sometimes, we are confused and we think a complex literal
1258 is a constant. Such thing is a compound literal which
1259 grammatically belongs to postfix-expr production. */
1260 pp_c_compound_literal (this, e
);
1264 pp_unsupported_tree (this, e
);
1269 /* Pretty-print a string such as an identifier, without changing its
1270 encoding, preceded by whitespace is necessary. */
1273 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1275 pp_c_maybe_whitespace (pp
);
1276 pp_string (pp
, str
);
1277 pp
->padding
= pp_before
;
1281 c_pretty_printer::translate_string (const char *gmsgid
)
1283 if (pp_translate_identifiers (this))
1284 pp_c_ws_string (this, _(gmsgid
));
1286 pp_c_ws_string (this, gmsgid
);
1289 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1290 that need converting to the locale encoding, preceded by whitespace
1294 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1296 pp_c_maybe_whitespace (pp
);
1297 pp_identifier (pp
, id
);
1298 pp
->padding
= pp_before
;
1301 /* Pretty-print a C primary-expression.
1309 c_pretty_printer::primary_expression (tree e
)
1311 switch (TREE_CODE (e
))
1319 pp_c_tree_decl_identifier (this, e
);
1322 case IDENTIFIER_NODE
:
1323 pp_c_tree_identifier (this, e
);
1327 translate_string ("<erroneous-expression>");
1331 translate_string ("<return-value>");
1343 pp_c_ws_string (this, "__builtin_memcpy");
1344 pp_c_left_paren (this);
1345 pp_ampersand (this);
1346 primary_expression (TREE_OPERAND (e
, 0));
1347 pp_separate_with (this, ',');
1348 pp_ampersand (this);
1349 initializer (TREE_OPERAND (e
, 1));
1350 if (TREE_OPERAND (e
, 2))
1352 pp_separate_with (this, ',');
1353 expression (TREE_OPERAND (e
, 2));
1355 pp_c_right_paren (this);
1359 if (SSA_NAME_VAR (e
))
1361 tree var
= SSA_NAME_VAR (e
);
1362 if (tree id
= SSA_NAME_IDENTIFIER (e
))
1364 const char *name
= IDENTIFIER_POINTER (id
);
1366 if (DECL_ARTIFICIAL (var
) && (dot
= strchr (name
, '.')))
1368 /* Print the name without the . suffix (such as in VLAs).
1369 Use pp_c_identifier so that it can be converted into
1370 the appropriate encoding. */
1371 size_t size
= dot
- name
;
1372 char *ident
= XALLOCAVEC (char, size
+ 1);
1373 memcpy (ident
, name
, size
);
1375 pp_c_identifier (this, ident
);
1378 primary_expression (var
);
1381 primary_expression (var
);
1385 /* Print only the right side of the GIMPLE assignment. */
1386 gimple
*def_stmt
= SSA_NAME_DEF_STMT (e
);
1387 pp_gimple_stmt_1 (this, def_stmt
, 0, TDF_RHS_ONLY
);
1392 /* FIXME: Make sure we won't get into an infinite loop. */
1393 if (location_wrapper_p (e
))
1397 pp_c_left_paren (this);
1399 pp_c_right_paren (this);
1405 /* Print out a C initializer -- also support C compound-literals.
1407 assignment-expression:
1408 { initializer-list }
1409 { initializer-list , } */
1412 c_pretty_printer::initializer (tree e
)
1414 if (TREE_CODE (e
) == CONSTRUCTOR
)
1415 pp_c_brace_enclosed_initializer_list (this, e
);
1422 declarator = initializer */
1425 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1428 /* We don't want to output function definitions here. There are handled
1429 elsewhere (and the syntactic form is bogus anyway). */
1430 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1432 tree init
= DECL_INITIAL (t
);
1433 /* This C++ bit is handled here because it is easier to do so.
1434 In templates, the C++ parser builds a TREE_LIST for a
1435 direct-initialization; the TREE_PURPOSE is the variable to
1436 initialize and the TREE_VALUE is the initializer. */
1437 if (TREE_CODE (init
) == TREE_LIST
)
1439 pp_c_left_paren (pp
);
1440 pp
->expression (TREE_VALUE (init
));
1441 pp_right_paren (pp
);
1448 pp
->initializer (init
);
1453 /* initializer-list:
1454 designation(opt) initializer
1455 initializer-list , designation(opt) initializer
1462 designator-list designator
1465 [ constant-expression ]
1469 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1471 tree type
= TREE_TYPE (e
);
1472 const enum tree_code code
= TREE_CODE (type
);
1474 if (TREE_CODE (e
) == CONSTRUCTOR
)
1476 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1486 tree init
= TREE_OPERAND (e
, 0);
1487 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1489 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1492 pp
->primary_expression (TREE_PURPOSE (init
));
1496 pp_c_left_bracket (pp
);
1497 if (TREE_PURPOSE (init
))
1498 pp
->constant (TREE_PURPOSE (init
));
1499 pp_c_right_bracket (pp
);
1501 pp_c_whitespace (pp
);
1503 pp_c_whitespace (pp
);
1504 pp
->initializer (TREE_VALUE (init
));
1505 if (TREE_CHAIN (init
))
1506 pp_separate_with (pp
, ',');
1512 if (TREE_CODE (e
) == VECTOR_CST
)
1514 /* We don't create variable-length VECTOR_CSTs. */
1515 unsigned int nunits
= VECTOR_CST_NELTS (e
).to_constant ();
1516 for (unsigned int i
= 0; i
< nunits
; ++i
)
1519 pp_separate_with (pp
, ',');
1520 pp
->expression (VECTOR_CST_ELT (e
, i
));
1528 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1530 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1531 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1532 pp_separate_with (pp
, ',');
1533 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1543 pp_unsupported_tree (pp
, type
);
1546 /* Pretty-print a brace-enclosed initializer-list. */
1549 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1551 pp_c_left_brace (pp
);
1552 pp_c_initializer_list (pp
, l
);
1553 pp_c_right_brace (pp
);
1557 /* This is a convenient function, used to bridge gap between C and C++
1564 c_pretty_printer::id_expression (tree t
)
1566 switch (TREE_CODE (t
))
1575 pp_c_tree_decl_identifier (this, t
);
1578 case IDENTIFIER_NODE
:
1579 pp_c_tree_identifier (this, t
);
1583 pp_unsupported_tree (this, t
);
1588 /* postfix-expression:
1590 postfix-expression [ expression ]
1591 postfix-expression ( argument-expression-list(opt) )
1592 postfix-expression . identifier
1593 postfix-expression -> identifier
1594 postfix-expression ++
1595 postfix-expression --
1596 ( type-name ) { initializer-list }
1597 ( type-name ) { initializer-list , } */
1600 c_pretty_printer::postfix_expression (tree e
)
1602 enum tree_code code
= TREE_CODE (e
);
1605 case POSTINCREMENT_EXPR
:
1606 case POSTDECREMENT_EXPR
:
1607 postfix_expression (TREE_OPERAND (e
, 0));
1608 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1612 postfix_expression (TREE_OPERAND (e
, 0));
1613 pp_c_left_bracket (this);
1614 expression (TREE_OPERAND (e
, 1));
1615 pp_c_right_bracket (this);
1620 call_expr_arg_iterator iter
;
1622 if (CALL_EXPR_FN (e
) != NULL_TREE
)
1623 postfix_expression (CALL_EXPR_FN (e
));
1625 pp_string (this, internal_fn_name (CALL_EXPR_IFN (e
)));
1626 pp_c_left_paren (this);
1627 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1630 if (more_call_expr_args_p (&iter
))
1631 pp_separate_with (this, ',');
1633 pp_c_right_paren (this);
1637 case UNORDERED_EXPR
:
1638 pp_c_ws_string (this, flag_isoc99
1640 : "__builtin_isunordered");
1644 pp_c_ws_string (this, flag_isoc99
1646 : "!__builtin_isunordered");
1650 pp_c_ws_string (this, flag_isoc99
1652 : "!__builtin_isgreaterequal");
1656 pp_c_ws_string (this, flag_isoc99
1658 : "!__builtin_isgreater");
1662 pp_c_ws_string (this, flag_isoc99
1664 : "!__builtin_islessequal");
1668 pp_c_ws_string (this, flag_isoc99
1670 : "!__builtin_isless");
1674 pp_c_ws_string (this, flag_isoc99
1676 : "!__builtin_islessgreater");
1680 pp_c_ws_string (this, flag_isoc99
1682 : "__builtin_islessgreater");
1686 pp_c_ws_string (this, "max");
1690 pp_c_ws_string (this, "min");
1694 pp_c_left_paren (this);
1695 expression (TREE_OPERAND (e
, 0));
1696 pp_separate_with (this, ',');
1697 expression (TREE_OPERAND (e
, 1));
1698 pp_c_right_paren (this);
1702 pp_c_ws_string (this, "__builtin_abs");
1703 pp_c_left_paren (this);
1704 expression (TREE_OPERAND (e
, 0));
1705 pp_c_right_paren (this);
1710 tree object
= TREE_OPERAND (e
, 0);
1711 if (INDIRECT_REF_P (object
))
1713 postfix_expression (TREE_OPERAND (object
, 0));
1718 postfix_expression (object
);
1721 expression (TREE_OPERAND (e
, 1));
1727 tree type
= TREE_TYPE (e
);
1729 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1731 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1733 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1734 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1735 if ((bitpos
% size
) == 0)
1737 pp_c_left_paren (this);
1738 pp_c_left_paren (this);
1741 pp_c_right_paren (this);
1742 pp_c_ampersand (this);
1743 expression (TREE_OPERAND (e
, 0));
1744 pp_c_right_paren (this);
1745 pp_c_left_bracket (this);
1746 pp_wide_integer (this, bitpos
/ size
);
1747 pp_c_right_bracket (this);
1751 pp_unsupported_tree (this, e
);
1756 case TARGET_MEM_REF
:
1762 pp_c_compound_literal (this, e
);
1766 pp_c_complex_expr (this, e
);
1769 case COMPOUND_LITERAL_EXPR
:
1770 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1777 pp_c_ws_string (this, "__builtin_va_arg");
1778 pp_c_left_paren (this);
1779 assignment_expression (TREE_OPERAND (e
, 0));
1780 pp_separate_with (this, ',');
1781 type_id (TREE_TYPE (e
));
1782 pp_c_right_paren (this);
1786 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1788 id_expression (TREE_OPERAND (e
, 0));
1794 primary_expression (e
);
1799 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1802 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1804 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1806 pp
->expression (TREE_VALUE (e
));
1808 pp_separate_with (pp
, ',');
1812 /* Print out V, which contains the elements of a constructor. */
1815 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1817 unsigned HOST_WIDE_INT ix
;
1820 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1822 pp
->expression (value
);
1823 if (ix
!= vec_safe_length (v
) - 1)
1824 pp_separate_with (pp
, ',');
1828 /* Print out an expression-list in parens, as if it were the argument
1829 list to a function. */
1832 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1834 pp_c_left_paren (pp
);
1835 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1836 pp_c_expression_list (pp
, t
);
1837 pp_c_right_paren (pp
);
1840 /* Try to fold *(type *)&op into op.fld.fld2[1] if possible.
1841 Only used for printing expressions. Should punt if ambiguous
1842 (e.g. in unions). */
1845 c_fold_indirect_ref_for_warn (location_t loc
, tree type
, tree op
,
1848 tree optype
= TREE_TYPE (op
);
1851 if (lang_hooks
.types_compatible_p (optype
, type
))
1853 /* *(foo *)&complexfoo => __real__ complexfoo */
1854 else if (TREE_CODE (optype
) == COMPLEX_TYPE
1855 && lang_hooks
.types_compatible_p (type
, TREE_TYPE (optype
)))
1856 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
1858 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
1859 else if (TREE_CODE (optype
) == COMPLEX_TYPE
1860 && lang_hooks
.types_compatible_p (type
, TREE_TYPE (optype
))
1861 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
1864 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
1866 /* ((foo *)&fooarray)[x] => fooarray[x] */
1867 if (TREE_CODE (optype
) == ARRAY_TYPE
1868 && TYPE_SIZE_UNIT (TREE_TYPE (optype
))
1869 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (optype
))) == INTEGER_CST
1870 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
1872 tree type_domain
= TYPE_DOMAIN (optype
);
1873 tree min_val
= size_zero_node
;
1874 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
1875 min_val
= TYPE_MIN_VALUE (type_domain
);
1876 offset_int el_sz
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
1877 offset_int idx
= off
/ el_sz
;
1878 offset_int rem
= off
% el_sz
;
1879 if (TREE_CODE (min_val
) == INTEGER_CST
)
1882 = wide_int_to_tree (sizetype
, idx
+ wi::to_offset (min_val
));
1883 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
1884 NULL_TREE
, NULL_TREE
);
1886 if (tree ret
= c_fold_indirect_ref_for_warn (loc
, type
, op
, off
))
1891 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
1892 else if (TREE_CODE (optype
) == RECORD_TYPE
)
1894 for (tree field
= TYPE_FIELDS (optype
);
1895 field
; field
= DECL_CHAIN (field
))
1896 if (TREE_CODE (field
) == FIELD_DECL
1897 && TREE_TYPE (field
) != error_mark_node
1898 && TYPE_SIZE_UNIT (TREE_TYPE (field
))
1899 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (field
))) == INTEGER_CST
)
1901 tree pos
= byte_position (field
);
1902 if (TREE_CODE (pos
) != INTEGER_CST
)
1904 offset_int upos
= wi::to_offset (pos
);
1906 = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
1907 if (upos
<= off
&& off
< upos
+ el_sz
)
1909 /* The C++ pretty printers print scope of the FIELD_DECLs,
1910 so punt if it is something that can't be printed. */
1911 if (c_dialect_cxx ())
1912 if (tree scope
= get_containing_scope (field
))
1913 if (TYPE_P (scope
) && TYPE_NAME (scope
) == NULL_TREE
)
1915 tree cop
= build3_loc (loc
, COMPONENT_REF
, TREE_TYPE (field
),
1916 op
, field
, NULL_TREE
);
1918 if (tree ret
= c_fold_indirect_ref_for_warn (loc
, type
, cop
,
1925 /* Similarly for unions, but in this case try to be very conservative,
1926 only match if some field has type compatible with type and it is the
1928 else if (TREE_CODE (optype
) == UNION_TYPE
)
1930 tree fld
= NULL_TREE
;
1931 for (tree field
= TYPE_FIELDS (optype
);
1932 field
; field
= DECL_CHAIN (field
))
1933 if (TREE_CODE (field
) == FIELD_DECL
1934 && TREE_TYPE (field
) != error_mark_node
1935 && lang_hooks
.types_compatible_p (TREE_TYPE (field
), type
))
1945 return build3_loc (loc
, COMPONENT_REF
, TREE_TYPE (fld
), op
, fld
,
1953 /* Print the MEM_REF expression REF, including its type and offset.
1954 Apply casts as necessary if the type of the access is different
1955 from the type of the accessed object. Produce compact output
1956 designed to include both the element index as well as any
1957 misalignment by preferring
1958 ((int*)((char*)p + 1))[2]
1960 *(int*)((char*)p + 9)
1961 The former is more verbose but makes it clearer that the access
1962 to the third element of the array is misaligned by one byte. */
1965 print_mem_ref (c_pretty_printer
*pp
, tree e
)
1967 tree arg
= TREE_OPERAND (e
, 0);
1969 /* The byte offset. Initially equal to the MEM_REF offset, then
1970 adjusted to the remainder of the division by the byte size of
1972 offset_int byte_off
= wi::to_offset (TREE_OPERAND (e
, 1));
1973 /* The result of dividing BYTE_OFF by the size of the access. */
1974 offset_int elt_idx
= 0;
1975 /* True to include a cast to char* (for a nonzero final BYTE_OFF). */
1976 bool char_cast
= false;
1977 tree op
= NULL_TREE
;
1978 bool array_ref_only
= false;
1979 if (TREE_CODE (arg
) == ADDR_EXPR
)
1981 op
= c_fold_indirect_ref_for_warn (EXPR_LOCATION (e
), TREE_TYPE (e
),
1982 TREE_OPERAND (arg
, 0), byte_off
);
1983 /* Try to fold it back to component, array ref or their combination,
1984 but print it only if the types and TBAA types are compatible. */
1987 && lang_hooks
.types_compatible_p (TREE_TYPE (e
), TREE_TYPE (op
))
1988 && (!flag_strict_aliasing
1989 || (get_deref_alias_set (TREE_OPERAND (e
, 1))
1990 == get_alias_set (op
))))
1992 pp
->expression (op
);
1995 if (op
== NULL_TREE
)
1996 op
= TREE_OPERAND (arg
, 0);
1997 /* If the types or TBAA types are incompatible, undo the
1998 UNION_TYPE handling from c_fold_indirect_ref_for_warn, and similarly
1999 undo __real__/__imag__ the code below doesn't try to handle. */
2000 if (op
!= TREE_OPERAND (arg
, 0)
2001 && ((TREE_CODE (op
) == COMPONENT_REF
2002 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == UNION_TYPE
)
2003 || TREE_CODE (op
) == REALPART_EXPR
2004 || TREE_CODE (op
) == IMAGPART_EXPR
))
2005 op
= TREE_OPERAND (op
, 0);
2006 if (op
!= TREE_OPERAND (arg
, 0))
2008 array_ref_only
= true;
2009 for (tree ref
= op
; ref
!= TREE_OPERAND (arg
, 0);
2010 ref
= TREE_OPERAND (ref
, 0))
2011 if (TREE_CODE (ref
) != ARRAY_REF
)
2013 array_ref_only
= false;
2019 tree access_type
= TREE_TYPE (e
);
2020 tree arg_type
= TREE_TYPE (TREE_TYPE (arg
));
2021 if (tree access_size
= TYPE_SIZE_UNIT (access_type
))
2023 && TREE_CODE (access_size
) == INTEGER_CST
2024 && !integer_zerop (access_size
))
2026 offset_int asize
= wi::to_offset (access_size
);
2027 elt_idx
= byte_off
/ asize
;
2028 byte_off
= byte_off
% asize
;
2031 /* True to include a cast to the accessed type. */
2032 const bool access_cast
2033 = ((op
&& op
!= TREE_OPERAND (arg
, 0))
2034 || VOID_TYPE_P (arg_type
)
2035 || !lang_hooks
.types_compatible_p (access_type
, arg_type
));
2036 const bool has_off
= byte_off
!= 0 || (op
&& op
!= TREE_OPERAND (arg
, 0));
2038 if (has_off
&& (byte_off
!= 0 || !array_ref_only
))
2040 /* When printing the byte offset for a pointer to a type of
2041 a different size than char, include a cast to char* first,
2042 before printing the cast to a pointer to the accessed type. */
2043 tree size
= TYPE_SIZE (arg_type
);
2044 if (size
== NULL_TREE
2045 || TREE_CODE (size
) != INTEGER_CST
2046 || wi::to_wide (size
) != BITS_PER_UNIT
)
2052 else if (access_cast
|| char_cast
)
2053 pp_c_left_paren (pp
);
2057 /* Include a cast to the accessed type if it isn't compatible
2058 with the type of the referenced object (or if the object
2060 pp_c_left_paren (pp
);
2061 pp
->type_id (build_pointer_type (access_type
));
2062 pp_c_right_paren (pp
);
2066 pp_c_left_paren (pp
);
2070 /* Include a cast to char *. */
2071 pp_c_left_paren (pp
);
2072 pp
->type_id (string_type_node
);
2073 pp_c_right_paren (pp
);
2076 pp
->unary_expression (arg
);
2078 if (op
&& op
!= TREE_OPERAND (arg
, 0))
2080 auto_vec
<tree
, 16> refs
;
2083 bool array_refs
= true;
2084 for (ref
= op
; ref
!= TREE_OPERAND (arg
, 0); ref
= TREE_OPERAND (ref
, 0))
2085 refs
.safe_push (ref
);
2086 FOR_EACH_VEC_ELT_REVERSE (refs
, i
, ref
)
2087 if (array_refs
&& TREE_CODE (ref
) == ARRAY_REF
)
2089 pp_c_left_bracket (pp
);
2090 pp
->expression (TREE_OPERAND (ref
, 1));
2091 pp_c_right_bracket (pp
);
2098 pp_string (pp
, " + offsetof");
2099 pp_c_left_paren (pp
);
2100 pp
->type_id (TREE_TYPE (TREE_OPERAND (ref
, 0)));
2103 else if (TREE_CODE (ref
) == COMPONENT_REF
)
2105 if (TREE_CODE (ref
) == COMPONENT_REF
)
2106 pp
->expression (TREE_OPERAND (ref
, 1));
2109 pp_c_left_bracket (pp
);
2110 pp
->expression (TREE_OPERAND (ref
, 1));
2111 pp_c_right_bracket (pp
);
2115 pp_c_right_paren (pp
);
2123 tree off
= wide_int_to_tree (ssizetype
, byte_off
);
2128 pp_c_right_paren (pp
);
2132 if (access_cast
|| char_cast
)
2133 pp_c_right_paren (pp
);
2135 pp_c_left_bracket (pp
);
2136 tree idx
= wide_int_to_tree (ssizetype
, elt_idx
);
2138 pp_c_right_bracket (pp
);
2142 /* unary-expression:
2146 unary-operator cast-expression
2147 sizeof unary-expression
2150 unary-operator: one of
2155 __alignof__ unary-expression
2156 __alignof__ ( type-id )
2157 __real__ unary-expression
2158 __imag__ unary-expression */
2161 c_pretty_printer::unary_expression (tree e
)
2163 enum tree_code code
= TREE_CODE (e
);
2166 case PREINCREMENT_EXPR
:
2167 case PREDECREMENT_EXPR
:
2168 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
2169 unary_expression (TREE_OPERAND (e
, 0));
2176 case TRUTH_NOT_EXPR
:
2178 /* String literal are used by address. */
2179 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
2180 pp_ampersand (this);
2181 else if (code
== INDIRECT_REF
)
2183 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
2184 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2185 /* Reference decay is implicit, don't print anything. */;
2189 else if (code
== NEGATE_EXPR
)
2191 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
2192 pp_complement (this);
2193 else if (code
== TRUTH_NOT_EXPR
)
2194 pp_exclamation (this);
2195 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
2199 print_mem_ref (this, e
);
2202 case TARGET_MEM_REF
:
2203 /* TARGET_MEM_REF can't appear directly from source, but can appear
2204 during late GIMPLE optimizations and through late diagnostic we might
2205 need to support it. Print it as dereferencing of a pointer after
2206 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2207 pointer to single byte types, so
2208 *(type *)((char *) ptr + step * index + index2) if all the operands
2209 are present and the casts are needed. */
2211 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))) == NULL_TREE
2212 || !integer_onep (TYPE_SIZE_UNIT
2213 (TREE_TYPE (TREE_TYPE (TMR_BASE (e
))))))
2215 if (TYPE_SIZE_UNIT (TREE_TYPE (e
))
2216 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (e
))))
2218 pp_c_left_paren (this);
2219 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2223 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2224 pp_c_left_paren (this);
2225 pp_c_type_cast (this, build_pointer_type (char_type_node
));
2228 else if (!lang_hooks
.types_compatible_p
2229 (TREE_TYPE (e
), TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))))
2231 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2232 pp_c_left_paren (this);
2235 pp_c_left_paren (this);
2236 pp_c_cast_expression (this, TMR_BASE (e
));
2237 if (TMR_STEP (e
) && TMR_INDEX (e
))
2240 pp_c_cast_expression (this, TMR_INDEX (e
));
2242 pp_c_cast_expression (this, TMR_STEP (e
));
2247 pp_c_cast_expression (this, TMR_INDEX2 (e
));
2249 if (!integer_zerop (TMR_OFFSET (e
)))
2252 pp_c_integer_constant (this,
2253 fold_convert (ssizetype
, TMR_OFFSET (e
)));
2255 pp_c_right_paren (this);
2260 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
2261 pp_c_whitespace (this);
2262 unary_expression (TREE_OPERAND (e
, 0));
2266 postfix_expression (e
);
2273 ( type-name ) cast-expression */
2276 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
2278 switch (TREE_CODE (e
))
2281 case FIX_TRUNC_EXPR
:
2283 case VIEW_CONVERT_EXPR
:
2284 if (!location_wrapper_p (e
))
2285 pp_c_type_cast (pp
, TREE_TYPE (e
));
2286 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
2290 pp
->unary_expression (e
);
2294 /* multiplicative-expression:
2296 multiplicative-expression * cast-expression
2297 multiplicative-expression / cast-expression
2298 multiplicative-expression % cast-expression */
2301 c_pretty_printer::multiplicative_expression (tree e
)
2303 enum tree_code code
= TREE_CODE (e
);
2307 case TRUNC_DIV_EXPR
:
2308 case TRUNC_MOD_EXPR
:
2309 case EXACT_DIV_EXPR
:
2311 multiplicative_expression (TREE_OPERAND (e
, 0));
2312 pp_c_whitespace (this);
2313 if (code
== MULT_EXPR
)
2315 else if (code
!= TRUNC_MOD_EXPR
)
2319 pp_c_whitespace (this);
2320 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
2324 pp_c_cast_expression (this, e
);
2329 /* additive-expression:
2330 multiplicative-expression
2331 additive-expression + multiplicative-expression
2332 additive-expression - multiplicative-expression */
2335 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
2337 enum tree_code code
= TREE_CODE (e
);
2340 case POINTER_PLUS_EXPR
:
2342 case POINTER_DIFF_EXPR
:
2344 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
2345 pp_c_whitespace (pp
);
2346 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
2350 pp_c_whitespace (pp
);
2352 tree op1
= TREE_OPERAND (e
, 1);
2353 if (code
== POINTER_PLUS_EXPR
2354 && TREE_CODE (op1
) == INTEGER_CST
2355 && tree_int_cst_sign_bit (op1
))
2356 /* A pointer minus an integer is represented internally as plus a very
2357 large number, don't expose that to users. */
2358 op1
= convert (ssizetype
, op1
);
2359 pp
->multiplicative_expression (op1
);
2364 pp
->multiplicative_expression (e
);
2369 /* additive-expression:
2371 shift-expression << additive-expression
2372 shift-expression >> additive-expression */
2375 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
2377 enum tree_code code
= TREE_CODE (e
);
2384 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
2385 pp_c_whitespace (pp
);
2386 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
2387 code
== RSHIFT_EXPR
? ">>" :
2388 code
== LROTATE_EXPR
? "<<<" : ">>>");
2389 pp_c_whitespace (pp
);
2390 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
2394 pp_c_additive_expression (pp
, e
);
2398 /* relational-expression:
2400 relational-expression < shift-expression
2401 relational-expression > shift-expression
2402 relational-expression <= shift-expression
2403 relational-expression >= shift-expression */
2406 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
2408 enum tree_code code
= TREE_CODE (e
);
2415 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
2416 pp_c_whitespace (pp
);
2417 if (code
== LT_EXPR
)
2419 else if (code
== GT_EXPR
)
2421 else if (code
== LE_EXPR
)
2423 else if (code
== GE_EXPR
)
2424 pp_greater_equal (pp
);
2425 pp_c_whitespace (pp
);
2426 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
2430 pp_c_shift_expression (pp
, e
);
2435 /* equality-expression:
2436 relational-expression
2437 equality-expression == relational-expression
2438 equality-equality != relational-expression */
2441 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
2443 enum tree_code code
= TREE_CODE (e
);
2448 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
2449 pp_c_whitespace (pp
);
2450 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
2451 pp_c_whitespace (pp
);
2452 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
2456 pp_c_relational_expression (pp
, e
);
2463 AND-expression & equality-equality */
2466 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
2468 if (TREE_CODE (e
) == BIT_AND_EXPR
)
2470 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
2471 pp_c_whitespace (pp
);
2473 pp_c_whitespace (pp
);
2474 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
2477 pp_c_equality_expression (pp
, e
);
2480 /* exclusive-OR-expression:
2482 exclusive-OR-expression ^ AND-expression */
2485 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2487 if (TREE_CODE (e
) == BIT_XOR_EXPR
2488 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2490 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2491 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2492 pp_c_maybe_whitespace (pp
);
2494 pp_c_whitespace (pp
);
2496 pp_c_whitespace (pp
);
2497 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2500 pp_c_and_expression (pp
, e
);
2503 /* inclusive-OR-expression:
2504 exclusive-OR-expression
2505 inclusive-OR-expression | exclusive-OR-expression */
2508 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2510 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2512 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2513 pp_c_whitespace (pp
);
2515 pp_c_whitespace (pp
);
2516 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2519 pp_c_exclusive_or_expression (pp
, e
);
2522 /* logical-AND-expression:
2523 inclusive-OR-expression
2524 logical-AND-expression && inclusive-OR-expression */
2527 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2529 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2530 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2532 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2533 pp_c_whitespace (pp
);
2534 pp_ampersand_ampersand (pp
);
2535 pp_c_whitespace (pp
);
2536 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2539 pp_c_inclusive_or_expression (pp
, e
);
2542 /* logical-OR-expression:
2543 logical-AND-expression
2544 logical-OR-expression || logical-AND-expression */
2547 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2549 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2550 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2552 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2553 pp_c_whitespace (pp
);
2555 pp_c_whitespace (pp
);
2556 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2559 pp_c_logical_and_expression (pp
, e
);
2562 /* conditional-expression:
2563 logical-OR-expression
2564 logical-OR-expression ? expression : conditional-expression */
2567 c_pretty_printer::conditional_expression (tree e
)
2569 if (TREE_CODE (e
) == COND_EXPR
)
2571 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2572 pp_c_whitespace (this);
2574 pp_c_whitespace (this);
2575 expression (TREE_OPERAND (e
, 1));
2576 pp_c_whitespace (this);
2578 pp_c_whitespace (this);
2579 conditional_expression (TREE_OPERAND (e
, 2));
2582 pp_c_logical_or_expression (this, e
);
2586 /* assignment-expression:
2587 conditional-expression
2588 unary-expression assignment-operator assignment-expression
2590 assignment-expression: one of
2591 = *= /= %= += -= >>= <<= &= ^= |= */
2594 c_pretty_printer::assignment_expression (tree e
)
2596 if (TREE_CODE (e
) == MODIFY_EXPR
2597 || TREE_CODE (e
) == INIT_EXPR
)
2599 unary_expression (TREE_OPERAND (e
, 0));
2600 pp_c_whitespace (this);
2603 expression (TREE_OPERAND (e
, 1));
2606 conditional_expression (e
);
2610 assignment-expression
2611 expression , assignment-expression
2613 Implementation note: instead of going through the usual recursion
2614 chain, I take the liberty of dispatching nodes to the appropriate
2615 functions. This makes some redundancy, but it worths it. That also
2616 prevents a possible infinite recursion between primary_expression ()
2617 and expression (). */
2620 c_pretty_printer::expression (tree e
)
2622 switch (TREE_CODE (e
))
2625 pp_c_void_constant (this);
2629 pp_c_integer_constant (this, e
);
2633 pp_c_floating_constant (this, e
);
2637 pp_c_fixed_constant (this, e
);
2641 pp_c_string_literal (this, e
);
2644 case IDENTIFIER_NODE
:
2653 primary_expression (e
);
2657 if (SSA_NAME_VAR (e
)
2658 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2659 expression (SSA_NAME_VAR (e
));
2661 translate_string ("<unknown>");
2664 case POSTINCREMENT_EXPR
:
2665 case POSTDECREMENT_EXPR
:
2674 case UNORDERED_EXPR
:
2685 case COMPOUND_LITERAL_EXPR
:
2687 postfix_expression (e
);
2694 case TARGET_MEM_REF
:
2697 case TRUTH_NOT_EXPR
:
2698 case PREINCREMENT_EXPR
:
2699 case PREDECREMENT_EXPR
:
2702 unary_expression (e
);
2706 case FIX_TRUNC_EXPR
:
2708 case VIEW_CONVERT_EXPR
:
2709 pp_c_cast_expression (this, e
);
2713 case TRUNC_MOD_EXPR
:
2714 case TRUNC_DIV_EXPR
:
2715 case EXACT_DIV_EXPR
:
2717 multiplicative_expression (e
);
2724 pp_c_shift_expression (this, e
);
2731 pp_c_relational_expression (this, e
);
2735 pp_c_and_expression (this, e
);
2739 case TRUTH_XOR_EXPR
:
2740 pp_c_exclusive_or_expression (this, e
);
2744 pp_c_inclusive_or_expression (this, e
);
2747 case TRUTH_ANDIF_EXPR
:
2748 case TRUTH_AND_EXPR
:
2749 pp_c_logical_and_expression (this, e
);
2752 case TRUTH_ORIF_EXPR
:
2754 pp_c_logical_or_expression (this, e
);
2759 pp_c_equality_expression (this, e
);
2763 conditional_expression (e
);
2766 case POINTER_PLUS_EXPR
:
2768 case POINTER_DIFF_EXPR
:
2770 pp_c_additive_expression (this, e
);
2775 assignment_expression (e
);
2779 pp_c_left_paren (this);
2780 expression (TREE_OPERAND (e
, 0));
2781 pp_separate_with (this, ',');
2782 assignment_expression (TREE_OPERAND (e
, 1));
2783 pp_c_right_paren (this);
2786 case NON_LVALUE_EXPR
:
2788 expression (TREE_OPERAND (e
, 0));
2792 postfix_expression (TREE_OPERAND (e
, 1));
2797 /* We don't yet have a way of dumping statements in a
2798 human-readable format. */
2799 pp_string (this, "({...})");
2802 case C_MAYBE_CONST_EXPR
:
2803 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2807 pp_unsupported_tree (this, e
);
2817 c_pretty_printer::statement (tree t
)
2822 switch (TREE_CODE (t
))
2826 pp_c_ws_string (this, "switch");
2828 pp_c_left_paren (this);
2829 expression (SWITCH_STMT_COND (t
));
2830 pp_c_right_paren (this);
2831 pp_indentation (this) += 3;
2832 pp_needs_newline (this) = true;
2833 statement (SWITCH_STMT_BODY (t
));
2834 pp_newline_and_indent (this, -3);
2837 /* iteration-statement:
2838 while ( expression ) statement
2839 do statement while ( expression ) ;
2840 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2841 for ( declaration expression(opt) ; expression(opt) ) statement */
2843 pp_c_ws_string (this, "while");
2845 pp_c_left_paren (this);
2846 expression (WHILE_COND (t
));
2847 pp_c_right_paren (this);
2848 pp_newline_and_indent (this, 3);
2849 statement (WHILE_BODY (t
));
2850 pp_indentation (this) -= 3;
2851 pp_needs_newline (this) = true;
2855 pp_c_ws_string (this, "do");
2856 pp_newline_and_indent (this, 3);
2857 statement (DO_BODY (t
));
2858 pp_newline_and_indent (this, -3);
2859 pp_c_ws_string (this, "while");
2861 pp_c_left_paren (this);
2862 expression (DO_COND (t
));
2863 pp_c_right_paren (this);
2864 pp_c_semicolon (this);
2865 pp_needs_newline (this) = true;
2869 pp_c_ws_string (this, "for");
2871 pp_c_left_paren (this);
2872 if (FOR_INIT_STMT (t
))
2873 statement (FOR_INIT_STMT (t
));
2875 pp_c_semicolon (this);
2876 pp_needs_newline (this) = false;
2877 pp_c_whitespace (this);
2879 expression (FOR_COND (t
));
2880 pp_c_semicolon (this);
2881 pp_needs_newline (this) = false;
2882 pp_c_whitespace (this);
2884 expression (FOR_EXPR (t
));
2885 pp_c_right_paren (this);
2886 pp_newline_and_indent (this, 3);
2887 statement (FOR_BODY (t
));
2888 pp_indentation (this) -= 3;
2889 pp_needs_newline (this) = true;
2895 return expression(opt) ; */
2898 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
2899 pp_c_semicolon (this);
2900 pp_needs_newline (this) = true;
2904 if (pp_needs_newline (this))
2905 pp_newline_and_indent (this, 0);
2906 dump_generic_node (this, t
, pp_indentation (this), TDF_NONE
, true);
2911 /* Initialize the PRETTY-PRINTER for handling C codes. */
2913 c_pretty_printer::c_pretty_printer ()
2914 : pretty_printer (),
2918 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2919 ptr_operator
= pp_c_pointer
;
2920 parameter_list
= pp_c_parameter_type_list
;
2923 /* c_pretty_printer's implementation of pretty_printer::clone vfunc. */
2926 c_pretty_printer::clone () const
2928 return new c_pretty_printer (*this);
2931 /* Print the tree T in full, on file FILE. */
2934 print_c_tree (FILE *file
, tree t
)
2936 c_pretty_printer pp
;
2938 pp_needs_newline (&pp
) = true;
2939 pp
.buffer
->stream
= file
;
2941 pp_newline_and_flush (&pp
);
2944 /* Print the tree T in full, on stderr. */
2947 debug_c_tree (tree t
)
2949 print_c_tree (stderr
, t
);
2950 fputc ('\n', stderr
);
2953 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2954 up of T's memory address. */
2957 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2961 gcc_assert (DECL_P (t
));
2964 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2967 static char xname
[8];
2968 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2973 pp_c_identifier (pp
, name
);
2978 namespace selftest
{
2980 /* Selftests for pretty-printing trees. */
2982 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2983 LOC as the effective location for any failures. */
2986 assert_c_pretty_printer_output (const location
&loc
, const char *expected
,
2989 c_pretty_printer pp
;
2990 pp
.expression (expr
);
2991 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2994 /* Helper function for calling assert_c_pretty_printer_output.
2995 This is to avoid having to write SELFTEST_LOCATION. */
2997 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2998 SELFTEST_BEGIN_STMT \
2999 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
3004 /* Verify that location wrappers don't show up in pretty-printed output. */
3007 test_location_wrappers ()
3010 tree id
= get_identifier ("foo");
3011 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, id
,
3013 tree wrapped_decl
= maybe_wrap_with_location (decl
, BUILTINS_LOCATION
);
3014 ASSERT_NE (wrapped_decl
, decl
);
3015 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl
);
3016 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl
);
3019 tree int_cst
= build_int_cst (integer_type_node
, 42);
3020 tree wrapped_cst
= maybe_wrap_with_location (int_cst
, BUILTINS_LOCATION
);
3021 ASSERT_NE (wrapped_cst
, int_cst
);
3022 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst
);
3023 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst
);
3026 /* Run all of the selftests within this file. */
3029 c_pretty_print_cc_tests ()
3031 test_location_wrappers ();
3034 } // namespace selftest
3036 #endif /* CHECKING_P */