1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2021 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"
36 /* The pretty-printer code is primarily designed to closely follow
37 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
38 codes we used to have in the past. Following a structured
39 approach (preferably the official grammars) is believed to make it
40 much easier to add extensions and nifty pretty-printing effects that
41 takes expression or declaration contexts into account. */
44 #define pp_c_maybe_whitespace(PP) \
46 if ((PP)->padding == pp_before) \
47 pp_c_whitespace (PP); \
51 static void pp_c_char (c_pretty_printer
*, int);
53 /* postfix-expression */
54 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
55 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
57 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
58 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
59 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
60 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
61 static void pp_c_and_expression (c_pretty_printer
*, tree
);
62 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
63 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
64 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
69 /* Helper functions. */
72 pp_c_whitespace (c_pretty_printer
*pp
)
75 pp
->padding
= pp_none
;
79 pp_c_left_paren (c_pretty_printer
*pp
)
82 pp
->padding
= pp_none
;
86 pp_c_right_paren (c_pretty_printer
*pp
)
89 pp
->padding
= pp_none
;
93 pp_c_left_brace (c_pretty_printer
*pp
)
96 pp
->padding
= pp_none
;
100 pp_c_right_brace (c_pretty_printer
*pp
)
103 pp
->padding
= pp_none
;
107 pp_c_left_bracket (c_pretty_printer
*pp
)
109 pp_left_bracket (pp
);
110 pp
->padding
= pp_none
;
114 pp_c_right_bracket (c_pretty_printer
*pp
)
116 pp_right_bracket (pp
);
117 pp
->padding
= pp_none
;
121 pp_c_dot (c_pretty_printer
*pp
)
124 pp
->padding
= pp_none
;
128 pp_c_ampersand (c_pretty_printer
*pp
)
131 pp
->padding
= pp_none
;
135 pp_c_star (c_pretty_printer
*pp
)
138 pp
->padding
= pp_none
;
142 pp_c_arrow (c_pretty_printer
*pp
)
145 pp
->padding
= pp_none
;
149 pp_c_semicolon (c_pretty_printer
*pp
)
152 pp
->padding
= pp_none
;
156 pp_c_complement (c_pretty_printer
*pp
)
159 pp
->padding
= pp_none
;
163 pp_c_exclamation (c_pretty_printer
*pp
)
166 pp
->padding
= pp_none
;
169 /* Print out the external representation of QUALIFIERS. */
172 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
174 const char *p
= pp_last_position_in_text (pp
);
179 /* The C programming language does not have references, but it is much
180 simpler to handle those here rather than going through the same
181 logic in the C++ pretty-printer. */
182 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
183 pp_c_whitespace (pp
);
185 if (qualifiers
& TYPE_QUAL_ATOMIC
)
186 pp_c_ws_string (pp
, "_Atomic");
187 if (qualifiers
& TYPE_QUAL_CONST
)
188 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
189 if (qualifiers
& TYPE_QUAL_VOLATILE
)
190 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
191 if (qualifiers
& TYPE_QUAL_RESTRICT
)
192 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
193 ? "restrict" : "__restrict__"));
196 /* Pretty-print T using the type-cast notation '( type-name )'. */
199 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
201 pp_c_left_paren (pp
);
203 pp_c_right_paren (pp
);
206 /* We're about to pretty-print a pointer type as indicated by T.
207 Output a whitespace, if needed, preparing for subsequent output. */
210 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
212 if (POINTER_TYPE_P (t
))
214 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
215 if (TREE_CODE (pointee
) != ARRAY_TYPE
216 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
217 pp_c_whitespace (pp
);
224 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
225 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
226 of its type. Take care of possible extensions.
230 type-qualifier-list type-qualifier
235 __restrict__ -- GNU C
236 address-space-qualifier -- GNU C
240 address-space-qualifier:
241 identifier -- GNU C */
244 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
248 if (!t
|| t
== error_mark_node
)
254 if (TREE_CODE (t
) != ARRAY_TYPE
)
256 qualifiers
= TYPE_QUALS (t
);
257 pp_c_cv_qualifiers (pp
, qualifiers
,
258 TREE_CODE (t
) == FUNCTION_TYPE
);
261 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
263 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
264 pp_c_identifier (pp
, as
);
269 * type-qualifier-list(opt)
270 * type-qualifier-list(opt) pointer */
273 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
275 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
277 switch (TREE_CODE (t
))
280 /* It is easier to handle C++ reference types here. */
282 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
283 pp_c_pointer (pp
, TREE_TYPE (t
));
284 if (TREE_CODE (t
) == POINTER_TYPE
)
289 if (TYPE_REF_IS_RVALUE (t
))
292 pp_c_type_qualifier_list (pp
, t
);
295 /* ??? This node is now in GENERIC and so shouldn't be here. But
296 we'll fix that later. */
298 pp
->declaration (DECL_EXPR_DECL (t
));
299 pp_needs_newline (pp
) = true;
303 pp_unsupported_tree (pp
, t
);
307 /* simple-type-specifier:
323 struct-or-union-specifier
328 simple-type-specifier:
333 c_pretty_printer::simple_type_specifier (tree t
)
335 const enum tree_code code
= TREE_CODE (t
);
339 translate_string ("<type-error>");
342 case IDENTIFIER_NODE
:
343 pp_c_identifier (this, IDENTIFIER_POINTER (t
));
351 case FIXED_POINT_TYPE
:
355 simple_type_specifier (t
);
359 int prec
= TYPE_PRECISION (t
);
361 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
362 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
363 TYPE_SATURATING (t
));
365 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
367 if (common_t
&& TYPE_NAME (common_t
))
369 simple_type_specifier (common_t
);
370 if (TYPE_PRECISION (common_t
) != prec
)
373 pp_decimal_int (this, prec
);
381 translate_string (TYPE_UNSIGNED (t
)
382 ? "<unnamed-unsigned:"
383 : "<unnamed-signed:");
386 translate_string ("<unnamed-float:");
388 case FIXED_POINT_TYPE
:
389 translate_string ("<unnamed-fixed:");
394 pp_decimal_int (this, prec
);
404 translate_string ("<typedef-error>");
410 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
411 /* Don't decorate the type if this is a typedef name. */;
412 else if (code
== UNION_TYPE
)
413 pp_c_ws_string (this, "union");
414 else if (code
== RECORD_TYPE
)
415 pp_c_ws_string (this, "struct");
416 else if (code
== ENUMERAL_TYPE
)
417 pp_c_ws_string (this, "enum");
419 translate_string ("<tag-error>");
422 id_expression (TYPE_NAME (t
));
424 translate_string ("<anonymous>");
428 pp_unsupported_tree (this, t
);
433 /* specifier-qualifier-list:
434 type-specifier specifier-qualifier-list-opt
435 type-qualifier specifier-qualifier-list-opt
438 Implementation note: Because of the non-linearities in array or
439 function declarations, this routine prints not just the
440 specifier-qualifier-list of such entities or types of such entities,
441 but also the 'pointer' production part of their declarators. The
442 remaining part is done by declarator() or abstract_declarator(). */
445 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
447 const enum tree_code code
= TREE_CODE (t
);
449 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
450 pp_c_type_qualifier_list (pp
, t
);
456 /* Get the types-specifier of this type. */
457 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
458 pp_c_specifier_qualifier_list (pp
, pointee
);
459 if (TREE_CODE (pointee
) == ARRAY_TYPE
460 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
462 pp_c_whitespace (pp
);
463 pp_c_left_paren (pp
);
464 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
466 else if (!c_dialect_cxx ())
467 pp_c_whitespace (pp
);
468 pp_ptr_operator (pp
, t
);
474 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
479 if (code
== COMPLEX_TYPE
)
480 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
481 ? "_Complex" : "__complex__"));
482 else if (code
== VECTOR_TYPE
)
484 /* The syntax we print for vector types isn't real C or C++ syntax,
485 so it's better to print the type name if we have one. */
486 tree name
= TYPE_NAME (t
);
487 if (!(pp
->flags
& pp_c_flag_gnu_v3
)
489 && TREE_CODE (name
) == TYPE_DECL
)
491 pp
->id_expression (name
);
494 pp_c_ws_string (pp
, "__vector");
495 pp_c_left_paren (pp
);
496 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
497 pp_c_right_paren (pp
);
498 pp_c_whitespace (pp
);
500 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
504 pp
->simple_type_specifier (t
);
507 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
508 pp_c_type_qualifier_list (pp
, t
);
511 /* parameter-type-list:
516 parameter-declaration
517 parameter-list , parameter-declaration
519 parameter-declaration:
520 declaration-specifiers declarator
521 declaration-specifiers abstract-declarator(opt) */
524 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
526 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
527 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
528 pp_c_left_paren (pp
);
529 if (parms
== void_list_node
)
530 pp_c_ws_string (pp
, "void");
534 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
537 pp_separate_with (pp
, ',');
539 pp
->declaration_specifiers
540 (want_parm_decl
? parms
: TREE_VALUE (parms
));
542 pp
->declarator (parms
);
544 pp
->abstract_declarator (TREE_VALUE (parms
));
546 if (!first
&& !parms
)
548 pp_separate_with (pp
, ',');
549 pp_string (pp
, "...");
552 pp_c_right_paren (pp
);
555 /* abstract-declarator:
557 pointer(opt) direct-abstract-declarator */
560 c_pretty_printer::abstract_declarator (tree t
)
562 if (TREE_CODE (t
) == POINTER_TYPE
)
564 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
565 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
566 pp_c_right_paren (this);
570 direct_abstract_declarator (t
);
573 /* direct-abstract-declarator:
574 ( abstract-declarator )
575 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
576 direct-abstract-declarator(opt) [ * ]
577 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
580 c_pretty_printer::direct_abstract_declarator (tree t
)
582 bool add_space
= false;
584 switch (TREE_CODE (t
))
587 abstract_declarator (t
);
591 pp_c_parameter_type_list (this, t
);
592 direct_abstract_declarator (TREE_TYPE (t
));
596 pp_c_left_bracket (this);
598 if (int quals
= TYPE_QUALS (t
))
600 /* Print the array qualifiers such as in "T[const restrict 3]". */
601 pp_c_cv_qualifiers (this, quals
, false);
605 if (tree arr
= lookup_attribute ("array", TYPE_ATTRIBUTES (t
)))
607 if (TREE_VALUE (arr
))
609 /* Print the specifier as in "T[static 3]" that's not actually
610 part of the type but may be added by the front end. */
611 pp_c_ws_string (this, "static");
614 else if (!TYPE_DOMAIN (t
))
615 /* For arrays of unspecified bound using the [*] notation. */
616 pp_character (this, '*');
619 if (tree dom
= TYPE_DOMAIN (t
))
621 if (tree maxval
= TYPE_MAX_VALUE (dom
))
626 tree type
= TREE_TYPE (maxval
);
628 if (tree_fits_shwi_p (maxval
))
629 pp_wide_integer (this, tree_to_shwi (maxval
) + 1);
630 else if (TREE_CODE (maxval
) == INTEGER_CST
)
631 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
632 build_int_cst (type
, 1)));
635 /* Strip the expressions from around a VLA bound added
636 internally to make it fit the domain mold, including
638 if (TREE_CODE (maxval
) == NOP_EXPR
)
639 maxval
= TREE_OPERAND (maxval
, 0);
640 if (TREE_CODE (maxval
) == PLUS_EXPR
641 && integer_all_onesp (TREE_OPERAND (maxval
, 1)))
643 maxval
= TREE_OPERAND (maxval
, 0);
644 if (TREE_CODE (maxval
) == NOP_EXPR
)
645 maxval
= TREE_OPERAND (maxval
, 0);
647 if (TREE_CODE (maxval
) == SAVE_EXPR
)
649 maxval
= TREE_OPERAND (maxval
, 0);
650 if (TREE_CODE (maxval
) == NOP_EXPR
)
651 maxval
= TREE_OPERAND (maxval
, 0);
657 else if (TYPE_SIZE (t
))
658 /* Print zero for zero-length arrays but not for flexible
659 array members whose TYPE_SIZE is null. */
660 pp_string (this, "0");
662 pp_c_right_bracket (this);
663 direct_abstract_declarator (TREE_TYPE (t
));
666 case IDENTIFIER_NODE
:
672 case FIXED_POINT_TYPE
:
683 pp_unsupported_tree (this, t
);
689 specifier-qualifier-list abstract-declarator(opt) */
692 c_pretty_printer::type_id (tree t
)
694 pp_c_specifier_qualifier_list (this, t
);
695 abstract_declarator (t
);
698 /* storage-class-specifier:
706 c_pretty_printer::storage_class_specifier (tree t
)
708 if (TREE_CODE (t
) == TYPE_DECL
)
709 pp_c_ws_string (this, "typedef");
712 if (DECL_REGISTER (t
))
713 pp_c_ws_string (this, "register");
714 else if (TREE_STATIC (t
) && VAR_P (t
))
715 pp_c_ws_string (this, "static");
719 /* function-specifier:
723 c_pretty_printer::function_specifier (tree t
)
725 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
726 pp_c_ws_string (this, "inline");
729 /* declaration-specifiers:
730 storage-class-specifier declaration-specifiers(opt)
731 type-specifier declaration-specifiers(opt)
732 type-qualifier declaration-specifiers(opt)
733 function-specifier declaration-specifiers(opt) */
736 c_pretty_printer::declaration_specifiers (tree t
)
738 storage_class_specifier (t
);
739 function_specifier (t
);
740 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
746 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
747 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
748 direct-declarator [ type-qualifier-list static assignment-expression ]
749 direct-declarator [ type-qualifier-list * ]
750 direct-declarator ( parameter-type-list )
751 direct-declarator ( identifier-list(opt) ) */
754 c_pretty_printer::direct_declarator (tree t
)
756 switch (TREE_CODE (t
))
763 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
764 pp_c_tree_decl_identifier (this, t
);
769 abstract_declarator (TREE_TYPE (t
));
773 pp_parameter_list (this, t
);
774 abstract_declarator (TREE_TYPE (t
));
778 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
779 pp_c_tree_decl_identifier (this, t
);
780 if (flags
& pp_c_flag_abstract
)
781 abstract_declarator (TREE_TYPE (t
));
784 pp_parameter_list (this, t
);
785 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
791 case FIXED_POINT_TYPE
:
798 pp_unsupported_tree (this, t
);
805 pointer(opt) direct-declarator */
808 c_pretty_printer::declarator (tree t
)
810 switch (TREE_CODE (t
))
814 case FIXED_POINT_TYPE
:
827 direct_declarator (t
);
832 pp_unsupported_tree (this, t
);
838 declaration-specifiers init-declarator-list(opt) ; */
841 c_pretty_printer::declaration (tree t
)
843 declaration_specifiers (t
);
844 pp_c_init_declarator (this, t
);
847 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
850 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
852 if (attributes
== NULL_TREE
)
855 pp_c_ws_string (pp
, "__attribute__");
856 pp_c_left_paren (pp
);
857 pp_c_left_paren (pp
);
858 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
860 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
861 if (TREE_VALUE (attributes
))
862 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
864 if (TREE_CHAIN (attributes
))
865 pp_separate_with (pp
, ',');
867 pp_c_right_paren (pp
);
868 pp_c_right_paren (pp
);
871 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
872 marked to be displayed on disgnostic. */
875 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
877 bool is_first
= true;
882 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
884 const struct attribute_spec
*as
;
885 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
886 if (!as
|| as
->affects_type_identity
== false)
889 && !strcmp ("transaction_safe", as
->name
))
890 /* In C++ transaction_safe is printed at the end of the declarator. */
894 pp_c_ws_string (pp
, "__attribute__");
895 pp_c_left_paren (pp
);
896 pp_c_left_paren (pp
);
901 pp_separate_with (pp
, ',');
903 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
905 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
910 pp_c_right_paren (pp
);
911 pp_c_right_paren (pp
);
912 pp_c_whitespace (pp
);
916 /* function-definition:
917 declaration-specifiers declarator compound-statement */
920 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
922 pp
->declaration_specifiers (t
);
924 pp_needs_newline (pp
) = true;
925 pp
->statement (DECL_SAVED_TREE (t
));
926 pp_newline_and_flush (pp
);
932 /* Print out a c-char. This is called solely for characters which are
933 in the *target* execution character set. We ought to convert them
934 back to the *host* execution character set before printing, but we
935 have no way to do this at present. A decent compromise is to print
936 all characters as if they were in the host execution character set,
937 and not attempt to recover any named escape characters, but render
938 all unprintables as octal escapes. If the host and target character
939 sets are the same, this produces relatively readable output. If they
940 are not the same, strings may appear as gibberish, but that's okay
941 (in fact, it may well be what the reader wants, e.g. if they are looking
942 to see if conversion to the target character set happened correctly).
944 A special case: we need to prefix \, ", and ' with backslashes. It is
945 correct to do so for the *host*'s \, ", and ', because the rest of the
946 file appears in the host character set. */
949 pp_c_char (c_pretty_printer
*pp
, int c
)
955 case '\\': pp_string (pp
, "\\\\"); break;
956 case '\'': pp_string (pp
, "\\\'"); break;
957 case '\"': pp_string (pp
, "\\\""); break;
958 default: pp_character (pp
, c
);
962 pp_scalar (pp
, "\\%03o", (unsigned) c
);
965 /* Print out a STRING literal. */
968 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
970 const char *p
= TREE_STRING_POINTER (s
);
971 int n
= TREE_STRING_LENGTH (s
) - 1;
974 for (i
= 0; i
< n
; ++i
)
975 pp_c_char (pp
, p
[i
]);
979 /* Pretty-print a VOID_CST (void_node). */
982 pp_c_void_constant (c_pretty_printer
*pp
)
984 pp_c_type_cast (pp
, void_type_node
);
988 /* Pretty-print an INTEGER literal. */
991 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
993 if (tree_fits_shwi_p (i
))
994 pp_wide_integer (pp
, tree_to_shwi (i
));
995 else if (tree_fits_uhwi_p (i
))
996 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
999 wide_int wi
= wi::to_wide (i
);
1001 if (wi::lt_p (wi::to_wide (i
), 0, TYPE_SIGN (TREE_TYPE (i
))))
1006 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
1007 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
1011 /* Print out a CHARACTER literal. */
1014 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
1017 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
1021 /* Print out a BOOLEAN literal. */
1024 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
1026 if (b
== boolean_false_node
)
1028 if (c_dialect_cxx ())
1029 pp_c_ws_string (pp
, "false");
1030 else if (flag_isoc99
)
1031 pp_c_ws_string (pp
, "_False");
1033 pp_unsupported_tree (pp
, b
);
1035 else if (b
== boolean_true_node
)
1037 if (c_dialect_cxx ())
1038 pp_c_ws_string (pp
, "true");
1039 else if (flag_isoc99
)
1040 pp_c_ws_string (pp
, "_True");
1042 pp_unsupported_tree (pp
, b
);
1044 else if (TREE_CODE (b
) == INTEGER_CST
)
1045 pp_c_integer_constant (pp
, b
);
1047 pp_unsupported_tree (pp
, b
);
1050 /* Given a value e of ENUMERAL_TYPE:
1051 Print out the first ENUMERATOR id with value e, if one is found,
1052 else print out the value as a C-style cast (type-id)value. */
1055 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
1057 tree type
= TREE_TYPE (e
);
1058 tree value
= NULL_TREE
;
1060 /* Find the name of this constant. */
1061 if ((pp
->flags
& pp_c_flag_gnu_v3
) == 0)
1062 for (value
= TYPE_VALUES (type
); value
!= NULL_TREE
;
1063 value
= TREE_CHAIN (value
))
1064 if (tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value
)), e
))
1067 if (value
!= NULL_TREE
)
1068 pp
->id_expression (TREE_PURPOSE (value
));
1071 /* Value must have been cast. */
1072 pp_c_type_cast (pp
, type
);
1073 pp_c_integer_constant (pp
, e
);
1077 /* Print out a REAL value as a decimal-floating-constant. */
1080 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1082 const struct real_format
*fmt
1083 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1085 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1086 bool is_decimal
= floating_cst
.decimal
;
1088 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1089 log10(2) to 7 significant digits. */
1090 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1092 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1093 sizeof (pp_buffer (pp
)->digit_buffer
),
1096 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1097 if (TREE_TYPE (r
) == float_type_node
)
1098 pp_character (pp
, 'f');
1099 else if (TREE_TYPE (r
) == long_double_type_node
)
1100 pp_character (pp
, 'l');
1101 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1102 pp_string (pp
, "dl");
1103 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1104 pp_string (pp
, "dd");
1105 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1106 pp_string (pp
, "df");
1107 else if (TREE_TYPE (r
) != double_type_node
)
1108 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1109 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1111 pp_character (pp
, 'f');
1112 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1113 if (floatn_nx_types
[i
].extended
)
1114 pp_character (pp
, 'x');
1119 /* Print out a FIXED value as a decimal-floating-constant. */
1122 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1124 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1125 sizeof (pp_buffer (pp
)->digit_buffer
));
1126 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1129 /* Pretty-print a compound literal expression. GNU extensions include
1130 vector constants. */
1133 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1135 tree type
= TREE_TYPE (e
);
1136 pp_c_type_cast (pp
, type
);
1138 switch (TREE_CODE (type
))
1145 pp_c_brace_enclosed_initializer_list (pp
, e
);
1149 pp_unsupported_tree (pp
, e
);
1154 /* Pretty-print a COMPLEX_EXPR expression. */
1157 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1159 /* Handle a few common special cases, otherwise fallback
1160 to printing it as compound literal. */
1161 tree type
= TREE_TYPE (e
);
1162 tree realexpr
= TREE_OPERAND (e
, 0);
1163 tree imagexpr
= TREE_OPERAND (e
, 1);
1165 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1166 if (TREE_CODE (realexpr
) == NOP_EXPR
1167 && TREE_CODE (imagexpr
) == NOP_EXPR
1168 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1169 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1170 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1171 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1172 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1173 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1175 pp_c_type_cast (pp
, type
);
1176 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1180 /* Cast of an scalar expression to COMPLEX_TYPE. */
1181 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1182 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1184 pp_c_type_cast (pp
, type
);
1185 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1186 realexpr
= TREE_OPERAND (realexpr
, 0);
1187 pp
->expression (realexpr
);
1191 pp_c_compound_literal (pp
, e
);
1197 fixed-point-constant
1198 enumeration-constant
1199 character-constant */
1202 c_pretty_printer::constant (tree e
)
1204 const enum tree_code code
= TREE_CODE (e
);
1209 pp_c_void_constant (this);
1214 tree type
= TREE_TYPE (e
);
1215 if (type
== boolean_type_node
)
1216 pp_c_bool_constant (this, e
);
1217 else if (type
== char_type_node
)
1218 pp_c_character_constant (this, e
);
1219 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1220 pp_c_enumeration_constant (this, e
);
1222 pp_c_integer_constant (this, e
);
1227 pp_c_floating_constant (this, e
);
1231 pp_c_fixed_constant (this, e
);
1235 pp_c_string_literal (this, e
);
1239 /* Sometimes, we are confused and we think a complex literal
1240 is a constant. Such thing is a compound literal which
1241 grammatically belongs to postfix-expr production. */
1242 pp_c_compound_literal (this, e
);
1246 pp_unsupported_tree (this, e
);
1251 /* Pretty-print a string such as an identifier, without changing its
1252 encoding, preceded by whitespace is necessary. */
1255 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1257 pp_c_maybe_whitespace (pp
);
1258 pp_string (pp
, str
);
1259 pp
->padding
= pp_before
;
1263 c_pretty_printer::translate_string (const char *gmsgid
)
1265 if (pp_translate_identifiers (this))
1266 pp_c_ws_string (this, _(gmsgid
));
1268 pp_c_ws_string (this, gmsgid
);
1271 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1272 that need converting to the locale encoding, preceded by whitespace
1276 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1278 pp_c_maybe_whitespace (pp
);
1279 pp_identifier (pp
, id
);
1280 pp
->padding
= pp_before
;
1283 /* Pretty-print a C primary-expression.
1291 c_pretty_printer::primary_expression (tree e
)
1293 switch (TREE_CODE (e
))
1301 pp_c_tree_decl_identifier (this, e
);
1304 case IDENTIFIER_NODE
:
1305 pp_c_tree_identifier (this, e
);
1309 translate_string ("<erroneous-expression>");
1313 translate_string ("<return-value>");
1325 pp_c_ws_string (this, "__builtin_memcpy");
1326 pp_c_left_paren (this);
1327 pp_ampersand (this);
1328 primary_expression (TREE_OPERAND (e
, 0));
1329 pp_separate_with (this, ',');
1330 pp_ampersand (this);
1331 initializer (TREE_OPERAND (e
, 1));
1332 if (TREE_OPERAND (e
, 2))
1334 pp_separate_with (this, ',');
1335 expression (TREE_OPERAND (e
, 2));
1337 pp_c_right_paren (this);
1341 if (SSA_NAME_VAR (e
))
1343 tree var
= SSA_NAME_VAR (e
);
1344 if (tree id
= SSA_NAME_IDENTIFIER (e
))
1346 const char *name
= IDENTIFIER_POINTER (id
);
1348 if (DECL_ARTIFICIAL (var
) && (dot
= strchr (name
, '.')))
1350 /* Print the name without the . suffix (such as in VLAs).
1351 Use pp_c_identifier so that it can be converted into
1352 the appropriate encoding. */
1353 size_t size
= dot
- name
;
1354 char *ident
= XALLOCAVEC (char, size
+ 1);
1355 memcpy (ident
, name
, size
);
1357 pp_c_identifier (this, ident
);
1360 primary_expression (var
);
1363 primary_expression (var
);
1367 /* Print only the right side of the GIMPLE assignment. */
1368 gimple
*def_stmt
= SSA_NAME_DEF_STMT (e
);
1369 pp_gimple_stmt_1 (this, def_stmt
, 0, TDF_RHS_ONLY
);
1374 /* FIXME: Make sure we won't get into an infinite loop. */
1375 if (location_wrapper_p (e
))
1379 pp_c_left_paren (this);
1381 pp_c_right_paren (this);
1387 /* Print out a C initializer -- also support C compound-literals.
1389 assignment-expression:
1390 { initializer-list }
1391 { initializer-list , } */
1394 c_pretty_printer::initializer (tree e
)
1396 if (TREE_CODE (e
) == CONSTRUCTOR
)
1397 pp_c_brace_enclosed_initializer_list (this, e
);
1404 declarator = initializer */
1407 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1410 /* We don't want to output function definitions here. There are handled
1411 elsewhere (and the syntactic form is bogus anyway). */
1412 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1414 tree init
= DECL_INITIAL (t
);
1415 /* This C++ bit is handled here because it is easier to do so.
1416 In templates, the C++ parser builds a TREE_LIST for a
1417 direct-initialization; the TREE_PURPOSE is the variable to
1418 initialize and the TREE_VALUE is the initializer. */
1419 if (TREE_CODE (init
) == TREE_LIST
)
1421 pp_c_left_paren (pp
);
1422 pp
->expression (TREE_VALUE (init
));
1423 pp_right_paren (pp
);
1430 pp
->initializer (init
);
1435 /* initializer-list:
1436 designation(opt) initializer
1437 initializer-list , designation(opt) initializer
1444 designator-list designator
1447 [ constant-expression ]
1451 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1453 tree type
= TREE_TYPE (e
);
1454 const enum tree_code code
= TREE_CODE (type
);
1456 if (TREE_CODE (e
) == CONSTRUCTOR
)
1458 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1468 tree init
= TREE_OPERAND (e
, 0);
1469 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1471 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1474 pp
->primary_expression (TREE_PURPOSE (init
));
1478 pp_c_left_bracket (pp
);
1479 if (TREE_PURPOSE (init
))
1480 pp
->constant (TREE_PURPOSE (init
));
1481 pp_c_right_bracket (pp
);
1483 pp_c_whitespace (pp
);
1485 pp_c_whitespace (pp
);
1486 pp
->initializer (TREE_VALUE (init
));
1487 if (TREE_CHAIN (init
))
1488 pp_separate_with (pp
, ',');
1494 if (TREE_CODE (e
) == VECTOR_CST
)
1496 /* We don't create variable-length VECTOR_CSTs. */
1497 unsigned int nunits
= VECTOR_CST_NELTS (e
).to_constant ();
1498 for (unsigned int i
= 0; i
< nunits
; ++i
)
1501 pp_separate_with (pp
, ',');
1502 pp
->expression (VECTOR_CST_ELT (e
, i
));
1510 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1512 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1513 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1514 pp_separate_with (pp
, ',');
1515 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1525 pp_unsupported_tree (pp
, type
);
1528 /* Pretty-print a brace-enclosed initializer-list. */
1531 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1533 pp_c_left_brace (pp
);
1534 pp_c_initializer_list (pp
, l
);
1535 pp_c_right_brace (pp
);
1539 /* This is a convenient function, used to bridge gap between C and C++
1546 c_pretty_printer::id_expression (tree t
)
1548 switch (TREE_CODE (t
))
1557 pp_c_tree_decl_identifier (this, t
);
1560 case IDENTIFIER_NODE
:
1561 pp_c_tree_identifier (this, t
);
1565 pp_unsupported_tree (this, t
);
1570 /* postfix-expression:
1572 postfix-expression [ expression ]
1573 postfix-expression ( argument-expression-list(opt) )
1574 postfix-expression . identifier
1575 postfix-expression -> identifier
1576 postfix-expression ++
1577 postfix-expression --
1578 ( type-name ) { initializer-list }
1579 ( type-name ) { initializer-list , } */
1582 c_pretty_printer::postfix_expression (tree e
)
1584 enum tree_code code
= TREE_CODE (e
);
1587 case POSTINCREMENT_EXPR
:
1588 case POSTDECREMENT_EXPR
:
1589 postfix_expression (TREE_OPERAND (e
, 0));
1590 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1594 postfix_expression (TREE_OPERAND (e
, 0));
1595 pp_c_left_bracket (this);
1596 expression (TREE_OPERAND (e
, 1));
1597 pp_c_right_bracket (this);
1602 call_expr_arg_iterator iter
;
1604 postfix_expression (CALL_EXPR_FN (e
));
1605 pp_c_left_paren (this);
1606 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1609 if (more_call_expr_args_p (&iter
))
1610 pp_separate_with (this, ',');
1612 pp_c_right_paren (this);
1616 case UNORDERED_EXPR
:
1617 pp_c_ws_string (this, flag_isoc99
1619 : "__builtin_isunordered");
1623 pp_c_ws_string (this, flag_isoc99
1625 : "!__builtin_isunordered");
1629 pp_c_ws_string (this, flag_isoc99
1631 : "!__builtin_isgreaterequal");
1635 pp_c_ws_string (this, flag_isoc99
1637 : "!__builtin_isgreater");
1641 pp_c_ws_string (this, flag_isoc99
1643 : "!__builtin_islessequal");
1647 pp_c_ws_string (this, flag_isoc99
1649 : "!__builtin_isless");
1653 pp_c_ws_string (this, flag_isoc99
1655 : "!__builtin_islessgreater");
1659 pp_c_ws_string (this, flag_isoc99
1661 : "__builtin_islessgreater");
1665 pp_c_ws_string (this, "max");
1669 pp_c_ws_string (this, "min");
1673 pp_c_left_paren (this);
1674 expression (TREE_OPERAND (e
, 0));
1675 pp_separate_with (this, ',');
1676 expression (TREE_OPERAND (e
, 1));
1677 pp_c_right_paren (this);
1681 pp_c_ws_string (this, "__builtin_abs");
1682 pp_c_left_paren (this);
1683 expression (TREE_OPERAND (e
, 0));
1684 pp_c_right_paren (this);
1689 tree object
= TREE_OPERAND (e
, 0);
1690 if (INDIRECT_REF_P (object
))
1692 postfix_expression (TREE_OPERAND (object
, 0));
1697 postfix_expression (object
);
1700 expression (TREE_OPERAND (e
, 1));
1706 tree type
= TREE_TYPE (e
);
1708 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1710 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1712 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1713 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1714 if ((bitpos
% size
) == 0)
1716 pp_c_left_paren (this);
1717 pp_c_left_paren (this);
1720 pp_c_right_paren (this);
1721 pp_c_ampersand (this);
1722 expression (TREE_OPERAND (e
, 0));
1723 pp_c_right_paren (this);
1724 pp_c_left_bracket (this);
1725 pp_wide_integer (this, bitpos
/ size
);
1726 pp_c_right_bracket (this);
1730 pp_unsupported_tree (this, e
);
1735 case TARGET_MEM_REF
:
1741 pp_c_compound_literal (this, e
);
1745 pp_c_complex_expr (this, e
);
1748 case COMPOUND_LITERAL_EXPR
:
1749 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1756 pp_c_ws_string (this, "__builtin_va_arg");
1757 pp_c_left_paren (this);
1758 assignment_expression (TREE_OPERAND (e
, 0));
1759 pp_separate_with (this, ',');
1760 type_id (TREE_TYPE (e
));
1761 pp_c_right_paren (this);
1765 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1767 id_expression (TREE_OPERAND (e
, 0));
1773 primary_expression (e
);
1778 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1781 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1783 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1785 pp
->expression (TREE_VALUE (e
));
1787 pp_separate_with (pp
, ',');
1791 /* Print out V, which contains the elements of a constructor. */
1794 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1796 unsigned HOST_WIDE_INT ix
;
1799 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1801 pp
->expression (value
);
1802 if (ix
!= vec_safe_length (v
) - 1)
1803 pp_separate_with (pp
, ',');
1807 /* Print out an expression-list in parens, as if it were the argument
1808 list to a function. */
1811 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1813 pp_c_left_paren (pp
);
1814 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1815 pp_c_expression_list (pp
, t
);
1816 pp_c_right_paren (pp
);
1819 /* Try to fold *(type *)&op into op.fld.fld2[1] if possible.
1820 Only used for printing expressions. Should punt if ambiguous
1821 (e.g. in unions). */
1824 c_fold_indirect_ref_for_warn (location_t loc
, tree type
, tree op
,
1827 tree optype
= TREE_TYPE (op
);
1830 if (lang_hooks
.types_compatible_p (optype
, type
))
1832 /* *(foo *)&complexfoo => __real__ complexfoo */
1833 else if (TREE_CODE (optype
) == COMPLEX_TYPE
1834 && lang_hooks
.types_compatible_p (type
, TREE_TYPE (optype
)))
1835 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
1837 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
1838 else if (TREE_CODE (optype
) == COMPLEX_TYPE
1839 && lang_hooks
.types_compatible_p (type
, TREE_TYPE (optype
))
1840 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
1843 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
1845 /* ((foo *)&fooarray)[x] => fooarray[x] */
1846 if (TREE_CODE (optype
) == ARRAY_TYPE
1847 && TYPE_SIZE_UNIT (TREE_TYPE (optype
))
1848 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (optype
))) == INTEGER_CST
1849 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
1851 tree type_domain
= TYPE_DOMAIN (optype
);
1852 tree min_val
= size_zero_node
;
1853 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
1854 min_val
= TYPE_MIN_VALUE (type_domain
);
1855 offset_int el_sz
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
1856 offset_int idx
= off
/ el_sz
;
1857 offset_int rem
= off
% el_sz
;
1858 if (TREE_CODE (min_val
) == INTEGER_CST
)
1861 = wide_int_to_tree (sizetype
, idx
+ wi::to_offset (min_val
));
1862 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
1863 NULL_TREE
, NULL_TREE
);
1865 if (tree ret
= c_fold_indirect_ref_for_warn (loc
, type
, op
, off
))
1870 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
1871 else if (TREE_CODE (optype
) == RECORD_TYPE
)
1873 for (tree field
= TYPE_FIELDS (optype
);
1874 field
; field
= DECL_CHAIN (field
))
1875 if (TREE_CODE (field
) == FIELD_DECL
1876 && TREE_TYPE (field
) != error_mark_node
1877 && TYPE_SIZE_UNIT (TREE_TYPE (field
))
1878 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (field
))) == INTEGER_CST
)
1880 tree pos
= byte_position (field
);
1881 if (TREE_CODE (pos
) != INTEGER_CST
)
1883 offset_int upos
= wi::to_offset (pos
);
1885 = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
1886 if (upos
<= off
&& off
< upos
+ el_sz
)
1888 tree cop
= build3_loc (loc
, COMPONENT_REF
, TREE_TYPE (field
),
1889 op
, field
, NULL_TREE
);
1891 if (tree ret
= c_fold_indirect_ref_for_warn (loc
, type
, cop
,
1898 /* Similarly for unions, but in this case try to be very conservative,
1899 only match if some field has type compatible with type and it is the
1901 else if (TREE_CODE (optype
) == UNION_TYPE
)
1903 tree fld
= NULL_TREE
;
1904 for (tree field
= TYPE_FIELDS (optype
);
1905 field
; field
= DECL_CHAIN (field
))
1906 if (TREE_CODE (field
) == FIELD_DECL
1907 && TREE_TYPE (field
) != error_mark_node
1908 && lang_hooks
.types_compatible_p (TREE_TYPE (field
), type
))
1918 return build3_loc (loc
, COMPONENT_REF
, TREE_TYPE (fld
), op
, fld
,
1926 /* Print the MEM_REF expression REF, including its type and offset.
1927 Apply casts as necessary if the type of the access is different
1928 from the type of the accessed object. Produce compact output
1929 designed to include both the element index as well as any
1930 misalignment by preferring
1931 ((int*)((char*)p + 1))[2]
1933 *(int*)((char*)p + 9)
1934 The former is more verbose but makes it clearer that the access
1935 to the third element of the array is misaligned by one byte. */
1938 print_mem_ref (c_pretty_printer
*pp
, tree e
)
1940 tree arg
= TREE_OPERAND (e
, 0);
1942 /* The byte offset. Initially equal to the MEM_REF offset, then
1943 adjusted to the remainder of the division by the byte size of
1945 offset_int byte_off
= wi::to_offset (TREE_OPERAND (e
, 1));
1946 /* The result of dividing BYTE_OFF by the size of the access. */
1947 offset_int elt_idx
= 0;
1948 /* True to include a cast to char* (for a nonzero final BYTE_OFF). */
1949 bool char_cast
= false;
1950 tree op
= NULL_TREE
;
1951 bool array_ref_only
= false;
1952 if (TREE_CODE (arg
) == ADDR_EXPR
)
1954 op
= c_fold_indirect_ref_for_warn (EXPR_LOCATION (e
), TREE_TYPE (e
),
1955 TREE_OPERAND (arg
, 0), byte_off
);
1956 /* Try to fold it back to component, array ref or their combination,
1957 but print it only if the types and TBAA types are compatible. */
1960 && lang_hooks
.types_compatible_p (TREE_TYPE (e
), TREE_TYPE (op
))
1961 && (!flag_strict_aliasing
1962 || (get_deref_alias_set (TREE_OPERAND (e
, 1))
1963 == get_alias_set (op
))))
1965 pp
->expression (op
);
1968 if (op
== NULL_TREE
)
1969 op
= TREE_OPERAND (arg
, 0);
1970 /* If the types or TBAA types are incompatible, undo the
1971 UNION_TYPE handling from c_fold_indirect_ref_for_warn, and similarly
1972 undo __real__/__imag__ the code below doesn't try to handle. */
1973 if (op
!= TREE_OPERAND (arg
, 0)
1974 && ((TREE_CODE (op
) == COMPONENT_REF
1975 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == UNION_TYPE
)
1976 || TREE_CODE (op
) == REALPART_EXPR
1977 || TREE_CODE (op
) == IMAGPART_EXPR
))
1978 op
= TREE_OPERAND (op
, 0);
1979 if (op
!= TREE_OPERAND (arg
, 0))
1981 array_ref_only
= true;
1982 for (tree ref
= op
; ref
!= TREE_OPERAND (arg
, 0);
1983 ref
= TREE_OPERAND (ref
, 0))
1984 if (TREE_CODE (ref
) != ARRAY_REF
)
1986 array_ref_only
= false;
1992 tree access_type
= TREE_TYPE (e
);
1993 tree arg_type
= TREE_TYPE (TREE_TYPE (arg
));
1994 if (tree access_size
= TYPE_SIZE_UNIT (access_type
))
1996 && TREE_CODE (access_size
) == INTEGER_CST
1997 && !integer_zerop (access_size
))
1999 offset_int asize
= wi::to_offset (access_size
);
2000 elt_idx
= byte_off
/ asize
;
2001 byte_off
= byte_off
% asize
;
2004 /* True to include a cast to the accessed type. */
2005 const bool access_cast
2006 = ((op
&& op
!= TREE_OPERAND (arg
, 0))
2007 || VOID_TYPE_P (arg_type
)
2008 || !lang_hooks
.types_compatible_p (access_type
, arg_type
));
2009 const bool has_off
= byte_off
!= 0 || (op
&& op
!= TREE_OPERAND (arg
, 0));
2011 if (has_off
&& (byte_off
!= 0 || !array_ref_only
))
2013 /* When printing the byte offset for a pointer to a type of
2014 a different size than char, include a cast to char* first,
2015 before printing the cast to a pointer to the accessed type. */
2016 tree size
= TYPE_SIZE (arg_type
);
2017 if (size
== NULL_TREE
2018 || TREE_CODE (size
) != INTEGER_CST
2019 || wi::to_wide (size
) != BITS_PER_UNIT
)
2025 else if (access_cast
|| char_cast
)
2026 pp_c_left_paren (pp
);
2030 /* Include a cast to the accessed type if it isn't compatible
2031 with the type of the referenced object (or if the object
2033 pp_c_left_paren (pp
);
2034 pp
->type_id (build_pointer_type (access_type
));
2035 pp_c_right_paren (pp
);
2039 pp_c_left_paren (pp
);
2043 /* Include a cast to char *. */
2044 pp_c_left_paren (pp
);
2045 pp
->type_id (string_type_node
);
2046 pp_c_right_paren (pp
);
2049 pp
->unary_expression (arg
);
2051 if (op
&& op
!= TREE_OPERAND (arg
, 0))
2053 auto_vec
<tree
, 16> refs
;
2056 bool array_refs
= true;
2057 for (ref
= op
; ref
!= TREE_OPERAND (arg
, 0); ref
= TREE_OPERAND (ref
, 0))
2058 refs
.safe_push (ref
);
2059 FOR_EACH_VEC_ELT_REVERSE (refs
, i
, ref
)
2060 if (array_refs
&& TREE_CODE (ref
) == ARRAY_REF
)
2062 pp_c_left_bracket (pp
);
2063 pp
->expression (TREE_OPERAND (ref
, 1));
2064 pp_c_right_bracket (pp
);
2071 pp_string (pp
, " + offsetof");
2072 pp_c_left_paren (pp
);
2073 pp
->type_id (TREE_TYPE (TREE_OPERAND (ref
, 0)));
2076 else if (TREE_CODE (ref
) == COMPONENT_REF
)
2078 if (TREE_CODE (ref
) == COMPONENT_REF
)
2079 pp
->expression (TREE_OPERAND (ref
, 1));
2082 pp_c_left_bracket (pp
);
2083 pp
->expression (TREE_OPERAND (ref
, 1));
2084 pp_c_right_bracket (pp
);
2088 pp_c_right_paren (pp
);
2096 tree off
= wide_int_to_tree (ssizetype
, byte_off
);
2101 pp_c_right_paren (pp
);
2105 if (access_cast
|| char_cast
)
2106 pp_c_right_paren (pp
);
2108 pp_c_left_bracket (pp
);
2109 tree idx
= wide_int_to_tree (ssizetype
, elt_idx
);
2111 pp_c_right_bracket (pp
);
2115 /* unary-expression:
2119 unary-operator cast-expression
2120 sizeof unary-expression
2123 unary-operator: one of
2128 __alignof__ unary-expression
2129 __alignof__ ( type-id )
2130 __real__ unary-expression
2131 __imag__ unary-expression */
2134 c_pretty_printer::unary_expression (tree e
)
2136 enum tree_code code
= TREE_CODE (e
);
2139 case PREINCREMENT_EXPR
:
2140 case PREDECREMENT_EXPR
:
2141 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
2142 unary_expression (TREE_OPERAND (e
, 0));
2149 case TRUTH_NOT_EXPR
:
2151 /* String literal are used by address. */
2152 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
2153 pp_ampersand (this);
2154 else if (code
== INDIRECT_REF
)
2156 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
2157 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2158 /* Reference decay is implicit, don't print anything. */;
2162 else if (code
== NEGATE_EXPR
)
2164 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
2165 pp_complement (this);
2166 else if (code
== TRUTH_NOT_EXPR
)
2167 pp_exclamation (this);
2168 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
2172 print_mem_ref (this, e
);
2175 case TARGET_MEM_REF
:
2176 /* TARGET_MEM_REF can't appear directly from source, but can appear
2177 during late GIMPLE optimizations and through late diagnostic we might
2178 need to support it. Print it as dereferencing of a pointer after
2179 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2180 pointer to single byte types, so
2181 *(type *)((char *) ptr + step * index + index2) if all the operands
2182 are present and the casts are needed. */
2184 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))) == NULL_TREE
2185 || !integer_onep (TYPE_SIZE_UNIT
2186 (TREE_TYPE (TREE_TYPE (TMR_BASE (e
))))))
2188 if (TYPE_SIZE_UNIT (TREE_TYPE (e
))
2189 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (e
))))
2191 pp_c_left_paren (this);
2192 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2196 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2197 pp_c_left_paren (this);
2198 pp_c_type_cast (this, build_pointer_type (char_type_node
));
2201 else if (!lang_hooks
.types_compatible_p
2202 (TREE_TYPE (e
), TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))))
2204 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2205 pp_c_left_paren (this);
2208 pp_c_left_paren (this);
2209 pp_c_cast_expression (this, TMR_BASE (e
));
2210 if (TMR_STEP (e
) && TMR_INDEX (e
))
2213 pp_c_cast_expression (this, TMR_INDEX (e
));
2215 pp_c_cast_expression (this, TMR_STEP (e
));
2220 pp_c_cast_expression (this, TMR_INDEX2 (e
));
2222 if (!integer_zerop (TMR_OFFSET (e
)))
2225 pp_c_integer_constant (this,
2226 fold_convert (ssizetype
, TMR_OFFSET (e
)));
2228 pp_c_right_paren (this);
2233 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
2234 pp_c_whitespace (this);
2235 unary_expression (TREE_OPERAND (e
, 0));
2239 postfix_expression (e
);
2246 ( type-name ) cast-expression */
2249 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
2251 switch (TREE_CODE (e
))
2254 case FIX_TRUNC_EXPR
:
2256 case VIEW_CONVERT_EXPR
:
2257 if (!location_wrapper_p (e
))
2258 pp_c_type_cast (pp
, TREE_TYPE (e
));
2259 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
2263 pp
->unary_expression (e
);
2267 /* multiplicative-expression:
2269 multiplicative-expression * cast-expression
2270 multiplicative-expression / cast-expression
2271 multiplicative-expression % cast-expression */
2274 c_pretty_printer::multiplicative_expression (tree e
)
2276 enum tree_code code
= TREE_CODE (e
);
2280 case TRUNC_DIV_EXPR
:
2281 case TRUNC_MOD_EXPR
:
2282 case EXACT_DIV_EXPR
:
2284 multiplicative_expression (TREE_OPERAND (e
, 0));
2285 pp_c_whitespace (this);
2286 if (code
== MULT_EXPR
)
2288 else if (code
!= TRUNC_MOD_EXPR
)
2292 pp_c_whitespace (this);
2293 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
2297 pp_c_cast_expression (this, e
);
2302 /* additive-expression:
2303 multiplicative-expression
2304 additive-expression + multiplicative-expression
2305 additive-expression - multiplicative-expression */
2308 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
2310 enum tree_code code
= TREE_CODE (e
);
2313 case POINTER_PLUS_EXPR
:
2315 case POINTER_DIFF_EXPR
:
2317 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
2318 pp_c_whitespace (pp
);
2319 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
2323 pp_c_whitespace (pp
);
2325 tree op1
= TREE_OPERAND (e
, 1);
2326 if (code
== POINTER_PLUS_EXPR
2327 && TREE_CODE (op1
) == INTEGER_CST
2328 && tree_int_cst_sign_bit (op1
))
2329 /* A pointer minus an integer is represented internally as plus a very
2330 large number, don't expose that to users. */
2331 op1
= convert (ssizetype
, op1
);
2332 pp
->multiplicative_expression (op1
);
2337 pp
->multiplicative_expression (e
);
2342 /* additive-expression:
2344 shift-expression << additive-expression
2345 shift-expression >> additive-expression */
2348 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
2350 enum tree_code code
= TREE_CODE (e
);
2357 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
2358 pp_c_whitespace (pp
);
2359 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
2360 code
== RSHIFT_EXPR
? ">>" :
2361 code
== LROTATE_EXPR
? "<<<" : ">>>");
2362 pp_c_whitespace (pp
);
2363 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
2367 pp_c_additive_expression (pp
, e
);
2371 /* relational-expression:
2373 relational-expression < shift-expression
2374 relational-expression > shift-expression
2375 relational-expression <= shift-expression
2376 relational-expression >= shift-expression */
2379 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
2381 enum tree_code code
= TREE_CODE (e
);
2388 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
2389 pp_c_whitespace (pp
);
2390 if (code
== LT_EXPR
)
2392 else if (code
== GT_EXPR
)
2394 else if (code
== LE_EXPR
)
2396 else if (code
== GE_EXPR
)
2397 pp_greater_equal (pp
);
2398 pp_c_whitespace (pp
);
2399 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
2403 pp_c_shift_expression (pp
, e
);
2408 /* equality-expression:
2409 relational-expression
2410 equality-expression == relational-expression
2411 equality-equality != relational-expression */
2414 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
2416 enum tree_code code
= TREE_CODE (e
);
2421 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
2422 pp_c_whitespace (pp
);
2423 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
2424 pp_c_whitespace (pp
);
2425 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
2429 pp_c_relational_expression (pp
, e
);
2436 AND-expression & equality-equality */
2439 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
2441 if (TREE_CODE (e
) == BIT_AND_EXPR
)
2443 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
2444 pp_c_whitespace (pp
);
2446 pp_c_whitespace (pp
);
2447 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
2450 pp_c_equality_expression (pp
, e
);
2453 /* exclusive-OR-expression:
2455 exclusive-OR-expression ^ AND-expression */
2458 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2460 if (TREE_CODE (e
) == BIT_XOR_EXPR
2461 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2463 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2464 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2465 pp_c_maybe_whitespace (pp
);
2467 pp_c_whitespace (pp
);
2469 pp_c_whitespace (pp
);
2470 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2473 pp_c_and_expression (pp
, e
);
2476 /* inclusive-OR-expression:
2477 exclusive-OR-expression
2478 inclusive-OR-expression | exclusive-OR-expression */
2481 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2483 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2485 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2486 pp_c_whitespace (pp
);
2488 pp_c_whitespace (pp
);
2489 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2492 pp_c_exclusive_or_expression (pp
, e
);
2495 /* logical-AND-expression:
2496 inclusive-OR-expression
2497 logical-AND-expression && inclusive-OR-expression */
2500 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2502 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2503 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2505 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2506 pp_c_whitespace (pp
);
2507 pp_ampersand_ampersand (pp
);
2508 pp_c_whitespace (pp
);
2509 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2512 pp_c_inclusive_or_expression (pp
, e
);
2515 /* logical-OR-expression:
2516 logical-AND-expression
2517 logical-OR-expression || logical-AND-expression */
2520 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2522 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2523 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2525 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2526 pp_c_whitespace (pp
);
2528 pp_c_whitespace (pp
);
2529 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2532 pp_c_logical_and_expression (pp
, e
);
2535 /* conditional-expression:
2536 logical-OR-expression
2537 logical-OR-expression ? expression : conditional-expression */
2540 c_pretty_printer::conditional_expression (tree e
)
2542 if (TREE_CODE (e
) == COND_EXPR
)
2544 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2545 pp_c_whitespace (this);
2547 pp_c_whitespace (this);
2548 expression (TREE_OPERAND (e
, 1));
2549 pp_c_whitespace (this);
2551 pp_c_whitespace (this);
2552 conditional_expression (TREE_OPERAND (e
, 2));
2555 pp_c_logical_or_expression (this, e
);
2559 /* assignment-expression:
2560 conditional-expression
2561 unary-expression assignment-operator assignment-expression
2563 assignment-expression: one of
2564 = *= /= %= += -= >>= <<= &= ^= |= */
2567 c_pretty_printer::assignment_expression (tree e
)
2569 if (TREE_CODE (e
) == MODIFY_EXPR
2570 || TREE_CODE (e
) == INIT_EXPR
)
2572 unary_expression (TREE_OPERAND (e
, 0));
2573 pp_c_whitespace (this);
2576 expression (TREE_OPERAND (e
, 1));
2579 conditional_expression (e
);
2583 assignment-expression
2584 expression , assignment-expression
2586 Implementation note: instead of going through the usual recursion
2587 chain, I take the liberty of dispatching nodes to the appropriate
2588 functions. This makes some redundancy, but it worths it. That also
2589 prevents a possible infinite recursion between primary_expression ()
2590 and expression (). */
2593 c_pretty_printer::expression (tree e
)
2595 switch (TREE_CODE (e
))
2598 pp_c_void_constant (this);
2602 pp_c_integer_constant (this, e
);
2606 pp_c_floating_constant (this, e
);
2610 pp_c_fixed_constant (this, e
);
2614 pp_c_string_literal (this, e
);
2617 case IDENTIFIER_NODE
:
2626 primary_expression (e
);
2630 if (SSA_NAME_VAR (e
)
2631 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2632 expression (SSA_NAME_VAR (e
));
2634 translate_string ("<unknown>");
2637 case POSTINCREMENT_EXPR
:
2638 case POSTDECREMENT_EXPR
:
2647 case UNORDERED_EXPR
:
2658 case COMPOUND_LITERAL_EXPR
:
2660 postfix_expression (e
);
2667 case TARGET_MEM_REF
:
2670 case TRUTH_NOT_EXPR
:
2671 case PREINCREMENT_EXPR
:
2672 case PREDECREMENT_EXPR
:
2675 unary_expression (e
);
2679 case FIX_TRUNC_EXPR
:
2681 case VIEW_CONVERT_EXPR
:
2682 pp_c_cast_expression (this, e
);
2686 case TRUNC_MOD_EXPR
:
2687 case TRUNC_DIV_EXPR
:
2688 case EXACT_DIV_EXPR
:
2690 multiplicative_expression (e
);
2697 pp_c_shift_expression (this, e
);
2704 pp_c_relational_expression (this, e
);
2708 pp_c_and_expression (this, e
);
2712 case TRUTH_XOR_EXPR
:
2713 pp_c_exclusive_or_expression (this, e
);
2717 pp_c_inclusive_or_expression (this, e
);
2720 case TRUTH_ANDIF_EXPR
:
2721 case TRUTH_AND_EXPR
:
2722 pp_c_logical_and_expression (this, e
);
2725 case TRUTH_ORIF_EXPR
:
2727 pp_c_logical_or_expression (this, e
);
2732 pp_c_equality_expression (this, e
);
2736 conditional_expression (e
);
2739 case POINTER_PLUS_EXPR
:
2741 case POINTER_DIFF_EXPR
:
2743 pp_c_additive_expression (this, e
);
2748 assignment_expression (e
);
2752 pp_c_left_paren (this);
2753 expression (TREE_OPERAND (e
, 0));
2754 pp_separate_with (this, ',');
2755 assignment_expression (TREE_OPERAND (e
, 1));
2756 pp_c_right_paren (this);
2759 case NON_LVALUE_EXPR
:
2761 expression (TREE_OPERAND (e
, 0));
2765 postfix_expression (TREE_OPERAND (e
, 1));
2770 /* We don't yet have a way of dumping statements in a
2771 human-readable format. */
2772 pp_string (this, "({...})");
2775 case C_MAYBE_CONST_EXPR
:
2776 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2780 pp_unsupported_tree (this, e
);
2790 c_pretty_printer::statement (tree t
)
2795 switch (TREE_CODE (t
))
2799 pp_c_ws_string (this, "switch");
2801 pp_c_left_paren (this);
2802 expression (SWITCH_STMT_COND (t
));
2803 pp_c_right_paren (this);
2804 pp_indentation (this) += 3;
2805 pp_needs_newline (this) = true;
2806 statement (SWITCH_STMT_BODY (t
));
2807 pp_newline_and_indent (this, -3);
2810 /* iteration-statement:
2811 while ( expression ) statement
2812 do statement while ( expression ) ;
2813 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2814 for ( declaration expression(opt) ; expression(opt) ) statement */
2816 pp_c_ws_string (this, "while");
2818 pp_c_left_paren (this);
2819 expression (WHILE_COND (t
));
2820 pp_c_right_paren (this);
2821 pp_newline_and_indent (this, 3);
2822 statement (WHILE_BODY (t
));
2823 pp_indentation (this) -= 3;
2824 pp_needs_newline (this) = true;
2828 pp_c_ws_string (this, "do");
2829 pp_newline_and_indent (this, 3);
2830 statement (DO_BODY (t
));
2831 pp_newline_and_indent (this, -3);
2832 pp_c_ws_string (this, "while");
2834 pp_c_left_paren (this);
2835 expression (DO_COND (t
));
2836 pp_c_right_paren (this);
2837 pp_c_semicolon (this);
2838 pp_needs_newline (this) = true;
2842 pp_c_ws_string (this, "for");
2844 pp_c_left_paren (this);
2845 if (FOR_INIT_STMT (t
))
2846 statement (FOR_INIT_STMT (t
));
2848 pp_c_semicolon (this);
2849 pp_needs_newline (this) = false;
2850 pp_c_whitespace (this);
2852 expression (FOR_COND (t
));
2853 pp_c_semicolon (this);
2854 pp_needs_newline (this) = false;
2855 pp_c_whitespace (this);
2857 expression (FOR_EXPR (t
));
2858 pp_c_right_paren (this);
2859 pp_newline_and_indent (this, 3);
2860 statement (FOR_BODY (t
));
2861 pp_indentation (this) -= 3;
2862 pp_needs_newline (this) = true;
2868 return expression(opt) ; */
2871 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
2872 pp_c_semicolon (this);
2873 pp_needs_newline (this) = true;
2877 if (pp_needs_newline (this))
2878 pp_newline_and_indent (this, 0);
2879 dump_generic_node (this, t
, pp_indentation (this), TDF_NONE
, true);
2884 /* Initialize the PRETTY-PRINTER for handling C codes. */
2886 c_pretty_printer::c_pretty_printer ()
2887 : pretty_printer (),
2891 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2892 ptr_operator
= pp_c_pointer
;
2893 parameter_list
= pp_c_parameter_type_list
;
2896 /* c_pretty_printer's implementation of pretty_printer::clone vfunc. */
2899 c_pretty_printer::clone () const
2901 return new c_pretty_printer (*this);
2904 /* Print the tree T in full, on file FILE. */
2907 print_c_tree (FILE *file
, tree t
)
2909 c_pretty_printer pp
;
2911 pp_needs_newline (&pp
) = true;
2912 pp
.buffer
->stream
= file
;
2914 pp_newline_and_flush (&pp
);
2917 /* Print the tree T in full, on stderr. */
2920 debug_c_tree (tree t
)
2922 print_c_tree (stderr
, t
);
2923 fputc ('\n', stderr
);
2926 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2927 up of T's memory address. */
2930 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2934 gcc_assert (DECL_P (t
));
2937 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2940 static char xname
[8];
2941 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2946 pp_c_identifier (pp
, name
);
2951 namespace selftest
{
2953 /* Selftests for pretty-printing trees. */
2955 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2956 LOC as the effective location for any failures. */
2959 assert_c_pretty_printer_output (const location
&loc
, const char *expected
,
2962 c_pretty_printer pp
;
2963 pp
.expression (expr
);
2964 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2967 /* Helper function for calling assert_c_pretty_printer_output.
2968 This is to avoid having to write SELFTEST_LOCATION. */
2970 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2971 SELFTEST_BEGIN_STMT \
2972 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
2977 /* Verify that location wrappers don't show up in pretty-printed output. */
2980 test_location_wrappers ()
2983 tree id
= get_identifier ("foo");
2984 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, id
,
2986 tree wrapped_decl
= maybe_wrap_with_location (decl
, BUILTINS_LOCATION
);
2987 ASSERT_NE (wrapped_decl
, decl
);
2988 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl
);
2989 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl
);
2992 tree int_cst
= build_int_cst (integer_type_node
, 42);
2993 tree wrapped_cst
= maybe_wrap_with_location (int_cst
, BUILTINS_LOCATION
);
2994 ASSERT_NE (wrapped_cst
, int_cst
);
2995 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst
);
2996 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst
);
2999 /* Run all of the selftests within this file. */
3002 c_pretty_print_c_tests ()
3004 test_location_wrappers ();
3007 } // namespace selftest
3009 #endif /* CHECKING_P */