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
:
682 pp_unsupported_tree (this, t
);
688 specifier-qualifier-list abstract-declarator(opt) */
691 c_pretty_printer::type_id (tree t
)
693 pp_c_specifier_qualifier_list (this, t
);
694 abstract_declarator (t
);
697 /* storage-class-specifier:
705 c_pretty_printer::storage_class_specifier (tree t
)
707 if (TREE_CODE (t
) == TYPE_DECL
)
708 pp_c_ws_string (this, "typedef");
711 if (DECL_REGISTER (t
))
712 pp_c_ws_string (this, "register");
713 else if (TREE_STATIC (t
) && VAR_P (t
))
714 pp_c_ws_string (this, "static");
718 /* function-specifier:
722 c_pretty_printer::function_specifier (tree t
)
724 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
725 pp_c_ws_string (this, "inline");
728 /* declaration-specifiers:
729 storage-class-specifier declaration-specifiers(opt)
730 type-specifier declaration-specifiers(opt)
731 type-qualifier declaration-specifiers(opt)
732 function-specifier declaration-specifiers(opt) */
735 c_pretty_printer::declaration_specifiers (tree t
)
737 storage_class_specifier (t
);
738 function_specifier (t
);
739 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
745 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
746 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
747 direct-declarator [ type-qualifier-list static assignment-expression ]
748 direct-declarator [ type-qualifier-list * ]
749 direct-declarator ( parameter-type-list )
750 direct-declarator ( identifier-list(opt) ) */
753 c_pretty_printer::direct_declarator (tree t
)
755 switch (TREE_CODE (t
))
762 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
763 pp_c_tree_decl_identifier (this, t
);
768 abstract_declarator (TREE_TYPE (t
));
772 pp_parameter_list (this, t
);
773 abstract_declarator (TREE_TYPE (t
));
777 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
778 pp_c_tree_decl_identifier (this, t
);
779 if (flags
& pp_c_flag_abstract
)
780 abstract_declarator (TREE_TYPE (t
));
783 pp_parameter_list (this, t
);
784 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
790 case FIXED_POINT_TYPE
:
797 pp_unsupported_tree (this, t
);
804 pointer(opt) direct-declarator */
807 c_pretty_printer::declarator (tree t
)
809 switch (TREE_CODE (t
))
813 case FIXED_POINT_TYPE
:
826 direct_declarator (t
);
831 pp_unsupported_tree (this, t
);
837 declaration-specifiers init-declarator-list(opt) ; */
840 c_pretty_printer::declaration (tree t
)
842 declaration_specifiers (t
);
843 pp_c_init_declarator (this, t
);
846 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
849 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
851 if (attributes
== NULL_TREE
)
854 pp_c_ws_string (pp
, "__attribute__");
855 pp_c_left_paren (pp
);
856 pp_c_left_paren (pp
);
857 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
859 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
860 if (TREE_VALUE (attributes
))
861 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
863 if (TREE_CHAIN (attributes
))
864 pp_separate_with (pp
, ',');
866 pp_c_right_paren (pp
);
867 pp_c_right_paren (pp
);
870 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
871 marked to be displayed on disgnostic. */
874 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
876 bool is_first
= true;
881 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
883 const struct attribute_spec
*as
;
884 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
885 if (!as
|| as
->affects_type_identity
== false)
888 && !strcmp ("transaction_safe", as
->name
))
889 /* In C++ transaction_safe is printed at the end of the declarator. */
893 pp_c_ws_string (pp
, "__attribute__");
894 pp_c_left_paren (pp
);
895 pp_c_left_paren (pp
);
900 pp_separate_with (pp
, ',');
902 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
904 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
909 pp_c_right_paren (pp
);
910 pp_c_right_paren (pp
);
911 pp_c_whitespace (pp
);
915 /* function-definition:
916 declaration-specifiers declarator compound-statement */
919 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
921 pp
->declaration_specifiers (t
);
923 pp_needs_newline (pp
) = true;
924 pp
->statement (DECL_SAVED_TREE (t
));
925 pp_newline_and_flush (pp
);
931 /* Print out a c-char. This is called solely for characters which are
932 in the *target* execution character set. We ought to convert them
933 back to the *host* execution character set before printing, but we
934 have no way to do this at present. A decent compromise is to print
935 all characters as if they were in the host execution character set,
936 and not attempt to recover any named escape characters, but render
937 all unprintables as octal escapes. If the host and target character
938 sets are the same, this produces relatively readable output. If they
939 are not the same, strings may appear as gibberish, but that's okay
940 (in fact, it may well be what the reader wants, e.g. if they are looking
941 to see if conversion to the target character set happened correctly).
943 A special case: we need to prefix \, ", and ' with backslashes. It is
944 correct to do so for the *host*'s \, ", and ', because the rest of the
945 file appears in the host character set. */
948 pp_c_char (c_pretty_printer
*pp
, int c
)
954 case '\\': pp_string (pp
, "\\\\"); break;
955 case '\'': pp_string (pp
, "\\\'"); break;
956 case '\"': pp_string (pp
, "\\\""); break;
957 default: pp_character (pp
, c
);
961 pp_scalar (pp
, "\\%03o", (unsigned) c
);
964 /* Print out a STRING literal. */
967 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
969 const char *p
= TREE_STRING_POINTER (s
);
970 int n
= TREE_STRING_LENGTH (s
) - 1;
973 for (i
= 0; i
< n
; ++i
)
974 pp_c_char (pp
, p
[i
]);
978 /* Pretty-print a VOID_CST (void_node). */
981 pp_c_void_constant (c_pretty_printer
*pp
)
983 pp_c_type_cast (pp
, void_type_node
);
987 /* Pretty-print an INTEGER literal. */
990 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
992 if (tree_fits_shwi_p (i
))
993 pp_wide_integer (pp
, tree_to_shwi (i
));
994 else if (tree_fits_uhwi_p (i
))
995 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
998 wide_int wi
= wi::to_wide (i
);
1000 if (wi::lt_p (wi::to_wide (i
), 0, TYPE_SIGN (TREE_TYPE (i
))))
1005 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
1006 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
1010 /* Print out a CHARACTER literal. */
1013 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
1016 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
1020 /* Print out a BOOLEAN literal. */
1023 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
1025 if (b
== boolean_false_node
)
1027 if (c_dialect_cxx ())
1028 pp_c_ws_string (pp
, "false");
1029 else if (flag_isoc99
)
1030 pp_c_ws_string (pp
, "_False");
1032 pp_unsupported_tree (pp
, b
);
1034 else if (b
== boolean_true_node
)
1036 if (c_dialect_cxx ())
1037 pp_c_ws_string (pp
, "true");
1038 else if (flag_isoc99
)
1039 pp_c_ws_string (pp
, "_True");
1041 pp_unsupported_tree (pp
, b
);
1043 else if (TREE_CODE (b
) == INTEGER_CST
)
1044 pp_c_integer_constant (pp
, b
);
1046 pp_unsupported_tree (pp
, b
);
1049 /* Given a value e of ENUMERAL_TYPE:
1050 Print out the first ENUMERATOR id with value e, if one is found,
1051 else print out the value as a C-style cast (type-id)value. */
1054 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
1056 tree type
= TREE_TYPE (e
);
1057 tree value
= NULL_TREE
;
1059 /* Find the name of this constant. */
1060 if ((pp
->flags
& pp_c_flag_gnu_v3
) == 0)
1061 for (value
= TYPE_VALUES (type
); value
!= NULL_TREE
;
1062 value
= TREE_CHAIN (value
))
1063 if (tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value
)), e
))
1066 if (value
!= NULL_TREE
)
1067 pp
->id_expression (TREE_PURPOSE (value
));
1070 /* Value must have been cast. */
1071 pp_c_type_cast (pp
, type
);
1072 pp_c_integer_constant (pp
, e
);
1076 /* Print out a REAL value as a decimal-floating-constant. */
1079 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1081 const struct real_format
*fmt
1082 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1084 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1085 bool is_decimal
= floating_cst
.decimal
;
1087 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1088 log10(2) to 7 significant digits. */
1089 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1091 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1092 sizeof (pp_buffer (pp
)->digit_buffer
),
1095 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1096 if (TREE_TYPE (r
) == float_type_node
)
1097 pp_character (pp
, 'f');
1098 else if (TREE_TYPE (r
) == long_double_type_node
)
1099 pp_character (pp
, 'l');
1100 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1101 pp_string (pp
, "dl");
1102 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1103 pp_string (pp
, "dd");
1104 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1105 pp_string (pp
, "df");
1106 else if (TREE_TYPE (r
) != double_type_node
)
1107 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1108 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1110 pp_character (pp
, 'f');
1111 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1112 if (floatn_nx_types
[i
].extended
)
1113 pp_character (pp
, 'x');
1118 /* Print out a FIXED value as a decimal-floating-constant. */
1121 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1123 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1124 sizeof (pp_buffer (pp
)->digit_buffer
));
1125 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1128 /* Pretty-print a compound literal expression. GNU extensions include
1129 vector constants. */
1132 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1134 tree type
= TREE_TYPE (e
);
1135 pp_c_type_cast (pp
, type
);
1137 switch (TREE_CODE (type
))
1144 pp_c_brace_enclosed_initializer_list (pp
, e
);
1148 pp_unsupported_tree (pp
, e
);
1153 /* Pretty-print a COMPLEX_EXPR expression. */
1156 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1158 /* Handle a few common special cases, otherwise fallback
1159 to printing it as compound literal. */
1160 tree type
= TREE_TYPE (e
);
1161 tree realexpr
= TREE_OPERAND (e
, 0);
1162 tree imagexpr
= TREE_OPERAND (e
, 1);
1164 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1165 if (TREE_CODE (realexpr
) == NOP_EXPR
1166 && TREE_CODE (imagexpr
) == NOP_EXPR
1167 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1168 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1169 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1170 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1171 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1172 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1174 pp_c_type_cast (pp
, type
);
1175 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1179 /* Cast of an scalar expression to COMPLEX_TYPE. */
1180 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1181 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1183 pp_c_type_cast (pp
, type
);
1184 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1185 realexpr
= TREE_OPERAND (realexpr
, 0);
1186 pp
->expression (realexpr
);
1190 pp_c_compound_literal (pp
, e
);
1196 fixed-point-constant
1197 enumeration-constant
1198 character-constant */
1201 c_pretty_printer::constant (tree e
)
1203 const enum tree_code code
= TREE_CODE (e
);
1208 pp_c_void_constant (this);
1213 tree type
= TREE_TYPE (e
);
1214 if (type
== boolean_type_node
)
1215 pp_c_bool_constant (this, e
);
1216 else if (type
== char_type_node
)
1217 pp_c_character_constant (this, e
);
1218 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1219 pp_c_enumeration_constant (this, e
);
1221 pp_c_integer_constant (this, e
);
1226 pp_c_floating_constant (this, e
);
1230 pp_c_fixed_constant (this, e
);
1234 pp_c_string_literal (this, e
);
1238 /* Sometimes, we are confused and we think a complex literal
1239 is a constant. Such thing is a compound literal which
1240 grammatically belongs to postfix-expr production. */
1241 pp_c_compound_literal (this, e
);
1245 pp_unsupported_tree (this, e
);
1250 /* Pretty-print a string such as an identifier, without changing its
1251 encoding, preceded by whitespace is necessary. */
1254 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1256 pp_c_maybe_whitespace (pp
);
1257 pp_string (pp
, str
);
1258 pp
->padding
= pp_before
;
1262 c_pretty_printer::translate_string (const char *gmsgid
)
1264 if (pp_translate_identifiers (this))
1265 pp_c_ws_string (this, _(gmsgid
));
1267 pp_c_ws_string (this, gmsgid
);
1270 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1271 that need converting to the locale encoding, preceded by whitespace
1275 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1277 pp_c_maybe_whitespace (pp
);
1278 pp_identifier (pp
, id
);
1279 pp
->padding
= pp_before
;
1282 /* Pretty-print a C primary-expression.
1290 c_pretty_printer::primary_expression (tree e
)
1292 switch (TREE_CODE (e
))
1300 pp_c_tree_decl_identifier (this, e
);
1303 case IDENTIFIER_NODE
:
1304 pp_c_tree_identifier (this, e
);
1308 translate_string ("<erroneous-expression>");
1312 translate_string ("<return-value>");
1324 pp_c_ws_string (this, "__builtin_memcpy");
1325 pp_c_left_paren (this);
1326 pp_ampersand (this);
1327 primary_expression (TREE_OPERAND (e
, 0));
1328 pp_separate_with (this, ',');
1329 pp_ampersand (this);
1330 initializer (TREE_OPERAND (e
, 1));
1331 if (TREE_OPERAND (e
, 2))
1333 pp_separate_with (this, ',');
1334 expression (TREE_OPERAND (e
, 2));
1336 pp_c_right_paren (this);
1340 if (SSA_NAME_VAR (e
))
1342 tree var
= SSA_NAME_VAR (e
);
1343 if (tree id
= SSA_NAME_IDENTIFIER (e
))
1345 const char *name
= IDENTIFIER_POINTER (id
);
1347 if (DECL_ARTIFICIAL (var
) && (dot
= strchr (name
, '.')))
1349 /* Print the name without the . suffix (such as in VLAs).
1350 Use pp_c_identifier so that it can be converted into
1351 the appropriate encoding. */
1352 size_t size
= dot
- name
;
1353 char *ident
= XALLOCAVEC (char, size
+ 1);
1354 memcpy (ident
, name
, size
);
1356 pp_c_identifier (this, ident
);
1359 primary_expression (var
);
1362 primary_expression (var
);
1366 /* Print only the right side of the GIMPLE assignment. */
1367 gimple
*def_stmt
= SSA_NAME_DEF_STMT (e
);
1368 pp_gimple_stmt_1 (this, def_stmt
, 0, TDF_RHS_ONLY
);
1373 /* FIXME: Make sure we won't get into an infinite loop. */
1374 if (location_wrapper_p (e
))
1378 pp_c_left_paren (this);
1380 pp_c_right_paren (this);
1386 /* Print out a C initializer -- also support C compound-literals.
1388 assignment-expression:
1389 { initializer-list }
1390 { initializer-list , } */
1393 c_pretty_printer::initializer (tree e
)
1395 if (TREE_CODE (e
) == CONSTRUCTOR
)
1396 pp_c_brace_enclosed_initializer_list (this, e
);
1403 declarator = initializer */
1406 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1409 /* We don't want to output function definitions here. There are handled
1410 elsewhere (and the syntactic form is bogus anyway). */
1411 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1413 tree init
= DECL_INITIAL (t
);
1414 /* This C++ bit is handled here because it is easier to do so.
1415 In templates, the C++ parser builds a TREE_LIST for a
1416 direct-initialization; the TREE_PURPOSE is the variable to
1417 initialize and the TREE_VALUE is the initializer. */
1418 if (TREE_CODE (init
) == TREE_LIST
)
1420 pp_c_left_paren (pp
);
1421 pp
->expression (TREE_VALUE (init
));
1422 pp_right_paren (pp
);
1429 pp
->initializer (init
);
1434 /* initializer-list:
1435 designation(opt) initializer
1436 initializer-list , designation(opt) initializer
1443 designator-list designator
1446 [ constant-expression ]
1450 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1452 tree type
= TREE_TYPE (e
);
1453 const enum tree_code code
= TREE_CODE (type
);
1455 if (TREE_CODE (e
) == CONSTRUCTOR
)
1457 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1467 tree init
= TREE_OPERAND (e
, 0);
1468 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1470 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1473 pp
->primary_expression (TREE_PURPOSE (init
));
1477 pp_c_left_bracket (pp
);
1478 if (TREE_PURPOSE (init
))
1479 pp
->constant (TREE_PURPOSE (init
));
1480 pp_c_right_bracket (pp
);
1482 pp_c_whitespace (pp
);
1484 pp_c_whitespace (pp
);
1485 pp
->initializer (TREE_VALUE (init
));
1486 if (TREE_CHAIN (init
))
1487 pp_separate_with (pp
, ',');
1493 if (TREE_CODE (e
) == VECTOR_CST
)
1495 /* We don't create variable-length VECTOR_CSTs. */
1496 unsigned int nunits
= VECTOR_CST_NELTS (e
).to_constant ();
1497 for (unsigned int i
= 0; i
< nunits
; ++i
)
1500 pp_separate_with (pp
, ',');
1501 pp
->expression (VECTOR_CST_ELT (e
, i
));
1509 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1511 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1512 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1513 pp_separate_with (pp
, ',');
1514 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1524 pp_unsupported_tree (pp
, type
);
1527 /* Pretty-print a brace-enclosed initializer-list. */
1530 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1532 pp_c_left_brace (pp
);
1533 pp_c_initializer_list (pp
, l
);
1534 pp_c_right_brace (pp
);
1538 /* This is a convenient function, used to bridge gap between C and C++
1545 c_pretty_printer::id_expression (tree t
)
1547 switch (TREE_CODE (t
))
1556 pp_c_tree_decl_identifier (this, t
);
1559 case IDENTIFIER_NODE
:
1560 pp_c_tree_identifier (this, t
);
1564 pp_unsupported_tree (this, t
);
1569 /* postfix-expression:
1571 postfix-expression [ expression ]
1572 postfix-expression ( argument-expression-list(opt) )
1573 postfix-expression . identifier
1574 postfix-expression -> identifier
1575 postfix-expression ++
1576 postfix-expression --
1577 ( type-name ) { initializer-list }
1578 ( type-name ) { initializer-list , } */
1581 c_pretty_printer::postfix_expression (tree e
)
1583 enum tree_code code
= TREE_CODE (e
);
1586 case POSTINCREMENT_EXPR
:
1587 case POSTDECREMENT_EXPR
:
1588 postfix_expression (TREE_OPERAND (e
, 0));
1589 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1593 postfix_expression (TREE_OPERAND (e
, 0));
1594 pp_c_left_bracket (this);
1595 expression (TREE_OPERAND (e
, 1));
1596 pp_c_right_bracket (this);
1601 call_expr_arg_iterator iter
;
1603 postfix_expression (CALL_EXPR_FN (e
));
1604 pp_c_left_paren (this);
1605 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1608 if (more_call_expr_args_p (&iter
))
1609 pp_separate_with (this, ',');
1611 pp_c_right_paren (this);
1615 case UNORDERED_EXPR
:
1616 pp_c_ws_string (this, flag_isoc99
1618 : "__builtin_isunordered");
1622 pp_c_ws_string (this, flag_isoc99
1624 : "!__builtin_isunordered");
1628 pp_c_ws_string (this, flag_isoc99
1630 : "!__builtin_isgreaterequal");
1634 pp_c_ws_string (this, flag_isoc99
1636 : "!__builtin_isgreater");
1640 pp_c_ws_string (this, flag_isoc99
1642 : "!__builtin_islessequal");
1646 pp_c_ws_string (this, flag_isoc99
1648 : "!__builtin_isless");
1652 pp_c_ws_string (this, flag_isoc99
1654 : "!__builtin_islessgreater");
1658 pp_c_ws_string (this, flag_isoc99
1660 : "__builtin_islessgreater");
1664 pp_c_ws_string (this, "max");
1668 pp_c_ws_string (this, "min");
1672 pp_c_left_paren (this);
1673 expression (TREE_OPERAND (e
, 0));
1674 pp_separate_with (this, ',');
1675 expression (TREE_OPERAND (e
, 1));
1676 pp_c_right_paren (this);
1680 pp_c_ws_string (this, "__builtin_abs");
1681 pp_c_left_paren (this);
1682 expression (TREE_OPERAND (e
, 0));
1683 pp_c_right_paren (this);
1688 tree object
= TREE_OPERAND (e
, 0);
1689 if (INDIRECT_REF_P (object
))
1691 postfix_expression (TREE_OPERAND (object
, 0));
1696 postfix_expression (object
);
1699 expression (TREE_OPERAND (e
, 1));
1705 tree type
= TREE_TYPE (e
);
1707 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1709 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1711 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1712 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1713 if ((bitpos
% size
) == 0)
1715 pp_c_left_paren (this);
1716 pp_c_left_paren (this);
1719 pp_c_right_paren (this);
1720 pp_c_ampersand (this);
1721 expression (TREE_OPERAND (e
, 0));
1722 pp_c_right_paren (this);
1723 pp_c_left_bracket (this);
1724 pp_wide_integer (this, bitpos
/ size
);
1725 pp_c_right_bracket (this);
1729 pp_unsupported_tree (this, e
);
1734 case TARGET_MEM_REF
:
1740 pp_c_compound_literal (this, e
);
1744 pp_c_complex_expr (this, e
);
1747 case COMPOUND_LITERAL_EXPR
:
1748 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1755 pp_c_ws_string (this, "__builtin_va_arg");
1756 pp_c_left_paren (this);
1757 assignment_expression (TREE_OPERAND (e
, 0));
1758 pp_separate_with (this, ',');
1759 type_id (TREE_TYPE (e
));
1760 pp_c_right_paren (this);
1764 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1766 id_expression (TREE_OPERAND (e
, 0));
1772 primary_expression (e
);
1777 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1780 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1782 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1784 pp
->expression (TREE_VALUE (e
));
1786 pp_separate_with (pp
, ',');
1790 /* Print out V, which contains the elements of a constructor. */
1793 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1795 unsigned HOST_WIDE_INT ix
;
1798 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1800 pp
->expression (value
);
1801 if (ix
!= vec_safe_length (v
) - 1)
1802 pp_separate_with (pp
, ',');
1806 /* Print out an expression-list in parens, as if it were the argument
1807 list to a function. */
1810 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1812 pp_c_left_paren (pp
);
1813 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1814 pp_c_expression_list (pp
, t
);
1815 pp_c_right_paren (pp
);
1818 /* Try to fold *(type *)&op into op.fld.fld2[1] if possible.
1819 Only used for printing expressions. Should punt if ambiguous
1820 (e.g. in unions). */
1823 c_fold_indirect_ref_for_warn (location_t loc
, tree type
, tree op
,
1826 tree optype
= TREE_TYPE (op
);
1829 if (lang_hooks
.types_compatible_p (optype
, type
))
1831 /* *(foo *)&complexfoo => __real__ complexfoo */
1832 else if (TREE_CODE (optype
) == COMPLEX_TYPE
1833 && lang_hooks
.types_compatible_p (type
, TREE_TYPE (optype
)))
1834 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
1836 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
1837 else if (TREE_CODE (optype
) == COMPLEX_TYPE
1838 && lang_hooks
.types_compatible_p (type
, TREE_TYPE (optype
))
1839 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
1842 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
1844 /* ((foo *)&fooarray)[x] => fooarray[x] */
1845 if (TREE_CODE (optype
) == ARRAY_TYPE
1846 && TYPE_SIZE_UNIT (TREE_TYPE (optype
))
1847 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (optype
))) == INTEGER_CST
1848 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
1850 tree type_domain
= TYPE_DOMAIN (optype
);
1851 tree min_val
= size_zero_node
;
1852 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
1853 min_val
= TYPE_MIN_VALUE (type_domain
);
1854 offset_int el_sz
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
1855 offset_int idx
= off
/ el_sz
;
1856 offset_int rem
= off
% el_sz
;
1857 if (TREE_CODE (min_val
) == INTEGER_CST
)
1860 = wide_int_to_tree (sizetype
, idx
+ wi::to_offset (min_val
));
1861 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
1862 NULL_TREE
, NULL_TREE
);
1864 if (tree ret
= c_fold_indirect_ref_for_warn (loc
, type
, op
, off
))
1869 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
1870 else if (TREE_CODE (optype
) == RECORD_TYPE
)
1872 for (tree field
= TYPE_FIELDS (optype
);
1873 field
; field
= DECL_CHAIN (field
))
1874 if (TREE_CODE (field
) == FIELD_DECL
1875 && TREE_TYPE (field
) != error_mark_node
1876 && TYPE_SIZE_UNIT (TREE_TYPE (field
))
1877 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (field
))) == INTEGER_CST
)
1879 tree pos
= byte_position (field
);
1880 if (TREE_CODE (pos
) != INTEGER_CST
)
1882 offset_int upos
= wi::to_offset (pos
);
1884 = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
1885 if (upos
<= off
&& off
< upos
+ el_sz
)
1887 tree cop
= build3_loc (loc
, COMPONENT_REF
, TREE_TYPE (field
),
1888 op
, field
, NULL_TREE
);
1890 if (tree ret
= c_fold_indirect_ref_for_warn (loc
, type
, cop
,
1897 /* Similarly for unions, but in this case try to be very conservative,
1898 only match if some field has type compatible with type and it is the
1900 else if (TREE_CODE (optype
) == UNION_TYPE
)
1902 tree fld
= NULL_TREE
;
1903 for (tree field
= TYPE_FIELDS (optype
);
1904 field
; field
= DECL_CHAIN (field
))
1905 if (TREE_CODE (field
) == FIELD_DECL
1906 && TREE_TYPE (field
) != error_mark_node
1907 && lang_hooks
.types_compatible_p (TREE_TYPE (field
), type
))
1917 return build3_loc (loc
, COMPONENT_REF
, TREE_TYPE (fld
), op
, fld
,
1925 /* Print the MEM_REF expression REF, including its type and offset.
1926 Apply casts as necessary if the type of the access is different
1927 from the type of the accessed object. Produce compact output
1928 designed to include both the element index as well as any
1929 misalignment by preferring
1930 ((int*)((char*)p + 1))[2]
1932 *(int*)((char*)p + 9)
1933 The former is more verbose but makes it clearer that the access
1934 to the third element of the array is misaligned by one byte. */
1937 print_mem_ref (c_pretty_printer
*pp
, tree e
)
1939 tree arg
= TREE_OPERAND (e
, 0);
1941 /* The byte offset. Initially equal to the MEM_REF offset, then
1942 adjusted to the remainder of the division by the byte size of
1944 offset_int byte_off
= wi::to_offset (TREE_OPERAND (e
, 1));
1945 /* The result of dividing BYTE_OFF by the size of the access. */
1946 offset_int elt_idx
= 0;
1947 /* True to include a cast to char* (for a nonzero final BYTE_OFF). */
1948 bool char_cast
= false;
1949 tree op
= NULL_TREE
;
1950 bool array_ref_only
= false;
1951 if (TREE_CODE (arg
) == ADDR_EXPR
)
1953 op
= c_fold_indirect_ref_for_warn (EXPR_LOCATION (e
), TREE_TYPE (e
),
1954 TREE_OPERAND (arg
, 0), byte_off
);
1955 /* Try to fold it back to component, array ref or their combination,
1956 but print it only if the types and TBAA types are compatible. */
1959 && lang_hooks
.types_compatible_p (TREE_TYPE (e
), TREE_TYPE (op
))
1960 && (!flag_strict_aliasing
1961 || (get_deref_alias_set (TREE_OPERAND (e
, 1))
1962 == get_alias_set (op
))))
1964 pp
->expression (op
);
1967 if (op
== NULL_TREE
)
1968 op
= TREE_OPERAND (arg
, 0);
1969 /* If the types or TBAA types are incompatible, undo the
1970 UNION_TYPE handling from c_fold_indirect_ref_for_warn, and similarly
1971 undo __real__/__imag__ the code below doesn't try to handle. */
1972 if (op
!= TREE_OPERAND (arg
, 0)
1973 && ((TREE_CODE (op
) == COMPONENT_REF
1974 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == UNION_TYPE
)
1975 || TREE_CODE (op
) == REALPART_EXPR
1976 || TREE_CODE (op
) == IMAGPART_EXPR
))
1977 op
= TREE_OPERAND (op
, 0);
1978 if (op
!= TREE_OPERAND (arg
, 0))
1980 array_ref_only
= true;
1981 for (tree ref
= op
; ref
!= TREE_OPERAND (arg
, 0);
1982 ref
= TREE_OPERAND (ref
, 0))
1983 if (TREE_CODE (ref
) != ARRAY_REF
)
1985 array_ref_only
= false;
1991 tree access_type
= TREE_TYPE (e
);
1992 tree arg_type
= TREE_TYPE (TREE_TYPE (arg
));
1993 if (tree access_size
= TYPE_SIZE_UNIT (access_type
))
1995 && TREE_CODE (access_size
) == INTEGER_CST
1996 && !integer_zerop (access_size
))
1998 offset_int asize
= wi::to_offset (access_size
);
1999 elt_idx
= byte_off
/ asize
;
2000 byte_off
= byte_off
% asize
;
2003 /* True to include a cast to the accessed type. */
2004 const bool access_cast
2005 = ((op
&& op
!= TREE_OPERAND (arg
, 0))
2006 || VOID_TYPE_P (arg_type
)
2007 || !lang_hooks
.types_compatible_p (access_type
, arg_type
));
2008 const bool has_off
= byte_off
!= 0 || (op
&& op
!= TREE_OPERAND (arg
, 0));
2010 if (has_off
&& (byte_off
!= 0 || !array_ref_only
))
2012 /* When printing the byte offset for a pointer to a type of
2013 a different size than char, include a cast to char* first,
2014 before printing the cast to a pointer to the accessed type. */
2015 tree size
= TYPE_SIZE (arg_type
);
2016 if (size
== NULL_TREE
2017 || TREE_CODE (size
) != INTEGER_CST
2018 || wi::to_wide (size
) != BITS_PER_UNIT
)
2024 else if (access_cast
|| char_cast
)
2025 pp_c_left_paren (pp
);
2029 /* Include a cast to the accessed type if it isn't compatible
2030 with the type of the referenced object (or if the object
2032 pp_c_left_paren (pp
);
2033 pp
->type_id (build_pointer_type (access_type
));
2034 pp_c_right_paren (pp
);
2038 pp_c_left_paren (pp
);
2042 /* Include a cast to char *. */
2043 pp_c_left_paren (pp
);
2044 pp
->type_id (string_type_node
);
2045 pp_c_right_paren (pp
);
2048 pp
->unary_expression (arg
);
2050 if (op
&& op
!= TREE_OPERAND (arg
, 0))
2052 auto_vec
<tree
, 16> refs
;
2055 bool array_refs
= true;
2056 for (ref
= op
; ref
!= TREE_OPERAND (arg
, 0); ref
= TREE_OPERAND (ref
, 0))
2057 refs
.safe_push (ref
);
2058 FOR_EACH_VEC_ELT_REVERSE (refs
, i
, ref
)
2059 if (array_refs
&& TREE_CODE (ref
) == ARRAY_REF
)
2061 pp_c_left_bracket (pp
);
2062 pp
->expression (TREE_OPERAND (ref
, 1));
2063 pp_c_right_bracket (pp
);
2070 pp_string (pp
, " + offsetof");
2071 pp_c_left_paren (pp
);
2072 pp
->type_id (TREE_TYPE (TREE_OPERAND (ref
, 0)));
2075 else if (TREE_CODE (ref
) == COMPONENT_REF
)
2077 if (TREE_CODE (ref
) == COMPONENT_REF
)
2078 pp
->expression (TREE_OPERAND (ref
, 1));
2081 pp_c_left_bracket (pp
);
2082 pp
->expression (TREE_OPERAND (ref
, 1));
2083 pp_c_right_bracket (pp
);
2087 pp_c_right_paren (pp
);
2095 tree off
= wide_int_to_tree (ssizetype
, byte_off
);
2100 pp_c_right_paren (pp
);
2104 if (access_cast
|| char_cast
)
2105 pp_c_right_paren (pp
);
2107 pp_c_left_bracket (pp
);
2108 tree idx
= wide_int_to_tree (ssizetype
, elt_idx
);
2110 pp_c_right_bracket (pp
);
2114 /* unary-expression:
2118 unary-operator cast-expression
2119 sizeof unary-expression
2122 unary-operator: one of
2127 __alignof__ unary-expression
2128 __alignof__ ( type-id )
2129 __real__ unary-expression
2130 __imag__ unary-expression */
2133 c_pretty_printer::unary_expression (tree e
)
2135 enum tree_code code
= TREE_CODE (e
);
2138 case PREINCREMENT_EXPR
:
2139 case PREDECREMENT_EXPR
:
2140 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
2141 unary_expression (TREE_OPERAND (e
, 0));
2148 case TRUTH_NOT_EXPR
:
2150 /* String literal are used by address. */
2151 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
2152 pp_ampersand (this);
2153 else if (code
== INDIRECT_REF
)
2155 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
2156 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2157 /* Reference decay is implicit, don't print anything. */;
2161 else if (code
== NEGATE_EXPR
)
2163 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
2164 pp_complement (this);
2165 else if (code
== TRUTH_NOT_EXPR
)
2166 pp_exclamation (this);
2167 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
2171 print_mem_ref (this, e
);
2174 case TARGET_MEM_REF
:
2175 /* TARGET_MEM_REF can't appear directly from source, but can appear
2176 during late GIMPLE optimizations and through late diagnostic we might
2177 need to support it. Print it as dereferencing of a pointer after
2178 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2179 pointer to single byte types, so
2180 *(type *)((char *) ptr + step * index + index2) if all the operands
2181 are present and the casts are needed. */
2183 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))) == NULL_TREE
2184 || !integer_onep (TYPE_SIZE_UNIT
2185 (TREE_TYPE (TREE_TYPE (TMR_BASE (e
))))))
2187 if (TYPE_SIZE_UNIT (TREE_TYPE (e
))
2188 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (e
))))
2190 pp_c_left_paren (this);
2191 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2195 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2196 pp_c_left_paren (this);
2197 pp_c_type_cast (this, build_pointer_type (char_type_node
));
2200 else if (!lang_hooks
.types_compatible_p
2201 (TREE_TYPE (e
), TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))))
2203 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
2204 pp_c_left_paren (this);
2207 pp_c_left_paren (this);
2208 pp_c_cast_expression (this, TMR_BASE (e
));
2209 if (TMR_STEP (e
) && TMR_INDEX (e
))
2212 pp_c_cast_expression (this, TMR_INDEX (e
));
2214 pp_c_cast_expression (this, TMR_STEP (e
));
2219 pp_c_cast_expression (this, TMR_INDEX2 (e
));
2221 if (!integer_zerop (TMR_OFFSET (e
)))
2224 pp_c_integer_constant (this,
2225 fold_convert (ssizetype
, TMR_OFFSET (e
)));
2227 pp_c_right_paren (this);
2232 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
2233 pp_c_whitespace (this);
2234 unary_expression (TREE_OPERAND (e
, 0));
2238 postfix_expression (e
);
2245 ( type-name ) cast-expression */
2248 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
2250 switch (TREE_CODE (e
))
2253 case FIX_TRUNC_EXPR
:
2255 case VIEW_CONVERT_EXPR
:
2256 if (!location_wrapper_p (e
))
2257 pp_c_type_cast (pp
, TREE_TYPE (e
));
2258 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
2262 pp
->unary_expression (e
);
2266 /* multiplicative-expression:
2268 multiplicative-expression * cast-expression
2269 multiplicative-expression / cast-expression
2270 multiplicative-expression % cast-expression */
2273 c_pretty_printer::multiplicative_expression (tree e
)
2275 enum tree_code code
= TREE_CODE (e
);
2279 case TRUNC_DIV_EXPR
:
2280 case TRUNC_MOD_EXPR
:
2281 case EXACT_DIV_EXPR
:
2283 multiplicative_expression (TREE_OPERAND (e
, 0));
2284 pp_c_whitespace (this);
2285 if (code
== MULT_EXPR
)
2287 else if (code
!= TRUNC_MOD_EXPR
)
2291 pp_c_whitespace (this);
2292 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
2296 pp_c_cast_expression (this, e
);
2301 /* additive-expression:
2302 multiplicative-expression
2303 additive-expression + multiplicative-expression
2304 additive-expression - multiplicative-expression */
2307 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
2309 enum tree_code code
= TREE_CODE (e
);
2312 case POINTER_PLUS_EXPR
:
2314 case POINTER_DIFF_EXPR
:
2316 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
2317 pp_c_whitespace (pp
);
2318 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
2322 pp_c_whitespace (pp
);
2324 tree op1
= TREE_OPERAND (e
, 1);
2325 if (code
== POINTER_PLUS_EXPR
2326 && TREE_CODE (op1
) == INTEGER_CST
2327 && tree_int_cst_sign_bit (op1
))
2328 /* A pointer minus an integer is represented internally as plus a very
2329 large number, don't expose that to users. */
2330 op1
= convert (ssizetype
, op1
);
2331 pp
->multiplicative_expression (op1
);
2336 pp
->multiplicative_expression (e
);
2341 /* additive-expression:
2343 shift-expression << additive-expression
2344 shift-expression >> additive-expression */
2347 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
2349 enum tree_code code
= TREE_CODE (e
);
2356 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
2357 pp_c_whitespace (pp
);
2358 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
2359 code
== RSHIFT_EXPR
? ">>" :
2360 code
== LROTATE_EXPR
? "<<<" : ">>>");
2361 pp_c_whitespace (pp
);
2362 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
2366 pp_c_additive_expression (pp
, e
);
2370 /* relational-expression:
2372 relational-expression < shift-expression
2373 relational-expression > shift-expression
2374 relational-expression <= shift-expression
2375 relational-expression >= shift-expression */
2378 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
2380 enum tree_code code
= TREE_CODE (e
);
2387 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
2388 pp_c_whitespace (pp
);
2389 if (code
== LT_EXPR
)
2391 else if (code
== GT_EXPR
)
2393 else if (code
== LE_EXPR
)
2395 else if (code
== GE_EXPR
)
2396 pp_greater_equal (pp
);
2397 pp_c_whitespace (pp
);
2398 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
2402 pp_c_shift_expression (pp
, e
);
2407 /* equality-expression:
2408 relational-expression
2409 equality-expression == relational-expression
2410 equality-equality != relational-expression */
2413 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
2415 enum tree_code code
= TREE_CODE (e
);
2420 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
2421 pp_c_whitespace (pp
);
2422 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
2423 pp_c_whitespace (pp
);
2424 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
2428 pp_c_relational_expression (pp
, e
);
2435 AND-expression & equality-equality */
2438 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
2440 if (TREE_CODE (e
) == BIT_AND_EXPR
)
2442 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
2443 pp_c_whitespace (pp
);
2445 pp_c_whitespace (pp
);
2446 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
2449 pp_c_equality_expression (pp
, e
);
2452 /* exclusive-OR-expression:
2454 exclusive-OR-expression ^ AND-expression */
2457 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2459 if (TREE_CODE (e
) == BIT_XOR_EXPR
2460 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2462 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2463 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2464 pp_c_maybe_whitespace (pp
);
2466 pp_c_whitespace (pp
);
2468 pp_c_whitespace (pp
);
2469 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2472 pp_c_and_expression (pp
, e
);
2475 /* inclusive-OR-expression:
2476 exclusive-OR-expression
2477 inclusive-OR-expression | exclusive-OR-expression */
2480 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2482 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2484 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2485 pp_c_whitespace (pp
);
2487 pp_c_whitespace (pp
);
2488 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2491 pp_c_exclusive_or_expression (pp
, e
);
2494 /* logical-AND-expression:
2495 inclusive-OR-expression
2496 logical-AND-expression && inclusive-OR-expression */
2499 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2501 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2502 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2504 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2505 pp_c_whitespace (pp
);
2506 pp_ampersand_ampersand (pp
);
2507 pp_c_whitespace (pp
);
2508 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2511 pp_c_inclusive_or_expression (pp
, e
);
2514 /* logical-OR-expression:
2515 logical-AND-expression
2516 logical-OR-expression || logical-AND-expression */
2519 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2521 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2522 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2524 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2525 pp_c_whitespace (pp
);
2527 pp_c_whitespace (pp
);
2528 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2531 pp_c_logical_and_expression (pp
, e
);
2534 /* conditional-expression:
2535 logical-OR-expression
2536 logical-OR-expression ? expression : conditional-expression */
2539 c_pretty_printer::conditional_expression (tree e
)
2541 if (TREE_CODE (e
) == COND_EXPR
)
2543 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2544 pp_c_whitespace (this);
2546 pp_c_whitespace (this);
2547 expression (TREE_OPERAND (e
, 1));
2548 pp_c_whitespace (this);
2550 pp_c_whitespace (this);
2551 conditional_expression (TREE_OPERAND (e
, 2));
2554 pp_c_logical_or_expression (this, e
);
2558 /* assignment-expression:
2559 conditional-expression
2560 unary-expression assignment-operator assignment-expression
2562 assignment-expression: one of
2563 = *= /= %= += -= >>= <<= &= ^= |= */
2566 c_pretty_printer::assignment_expression (tree e
)
2568 if (TREE_CODE (e
) == MODIFY_EXPR
2569 || TREE_CODE (e
) == INIT_EXPR
)
2571 unary_expression (TREE_OPERAND (e
, 0));
2572 pp_c_whitespace (this);
2575 expression (TREE_OPERAND (e
, 1));
2578 conditional_expression (e
);
2582 assignment-expression
2583 expression , assignment-expression
2585 Implementation note: instead of going through the usual recursion
2586 chain, I take the liberty of dispatching nodes to the appropriate
2587 functions. This makes some redundancy, but it worths it. That also
2588 prevents a possible infinite recursion between primary_expression ()
2589 and expression (). */
2592 c_pretty_printer::expression (tree e
)
2594 switch (TREE_CODE (e
))
2597 pp_c_void_constant (this);
2601 pp_c_integer_constant (this, e
);
2605 pp_c_floating_constant (this, e
);
2609 pp_c_fixed_constant (this, e
);
2613 pp_c_string_literal (this, e
);
2616 case IDENTIFIER_NODE
:
2625 primary_expression (e
);
2629 if (SSA_NAME_VAR (e
)
2630 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2631 expression (SSA_NAME_VAR (e
));
2633 translate_string ("<unknown>");
2636 case POSTINCREMENT_EXPR
:
2637 case POSTDECREMENT_EXPR
:
2646 case UNORDERED_EXPR
:
2657 case COMPOUND_LITERAL_EXPR
:
2659 postfix_expression (e
);
2666 case TARGET_MEM_REF
:
2669 case TRUTH_NOT_EXPR
:
2670 case PREINCREMENT_EXPR
:
2671 case PREDECREMENT_EXPR
:
2674 unary_expression (e
);
2678 case FIX_TRUNC_EXPR
:
2680 case VIEW_CONVERT_EXPR
:
2681 pp_c_cast_expression (this, e
);
2685 case TRUNC_MOD_EXPR
:
2686 case TRUNC_DIV_EXPR
:
2687 case EXACT_DIV_EXPR
:
2689 multiplicative_expression (e
);
2696 pp_c_shift_expression (this, e
);
2703 pp_c_relational_expression (this, e
);
2707 pp_c_and_expression (this, e
);
2711 case TRUTH_XOR_EXPR
:
2712 pp_c_exclusive_or_expression (this, e
);
2716 pp_c_inclusive_or_expression (this, e
);
2719 case TRUTH_ANDIF_EXPR
:
2720 case TRUTH_AND_EXPR
:
2721 pp_c_logical_and_expression (this, e
);
2724 case TRUTH_ORIF_EXPR
:
2726 pp_c_logical_or_expression (this, e
);
2731 pp_c_equality_expression (this, e
);
2735 conditional_expression (e
);
2738 case POINTER_PLUS_EXPR
:
2740 case POINTER_DIFF_EXPR
:
2742 pp_c_additive_expression (this, e
);
2747 assignment_expression (e
);
2751 pp_c_left_paren (this);
2752 expression (TREE_OPERAND (e
, 0));
2753 pp_separate_with (this, ',');
2754 assignment_expression (TREE_OPERAND (e
, 1));
2755 pp_c_right_paren (this);
2758 case NON_LVALUE_EXPR
:
2760 expression (TREE_OPERAND (e
, 0));
2764 postfix_expression (TREE_OPERAND (e
, 1));
2769 /* We don't yet have a way of dumping statements in a
2770 human-readable format. */
2771 pp_string (this, "({...})");
2774 case C_MAYBE_CONST_EXPR
:
2775 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2779 pp_unsupported_tree (this, e
);
2789 c_pretty_printer::statement (tree t
)
2794 switch (TREE_CODE (t
))
2798 pp_c_ws_string (this, "switch");
2800 pp_c_left_paren (this);
2801 expression (SWITCH_STMT_COND (t
));
2802 pp_c_right_paren (this);
2803 pp_indentation (this) += 3;
2804 pp_needs_newline (this) = true;
2805 statement (SWITCH_STMT_BODY (t
));
2806 pp_newline_and_indent (this, -3);
2809 /* iteration-statement:
2810 while ( expression ) statement
2811 do statement while ( expression ) ;
2812 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2813 for ( declaration expression(opt) ; expression(opt) ) statement */
2815 pp_c_ws_string (this, "while");
2817 pp_c_left_paren (this);
2818 expression (WHILE_COND (t
));
2819 pp_c_right_paren (this);
2820 pp_newline_and_indent (this, 3);
2821 statement (WHILE_BODY (t
));
2822 pp_indentation (this) -= 3;
2823 pp_needs_newline (this) = true;
2827 pp_c_ws_string (this, "do");
2828 pp_newline_and_indent (this, 3);
2829 statement (DO_BODY (t
));
2830 pp_newline_and_indent (this, -3);
2831 pp_c_ws_string (this, "while");
2833 pp_c_left_paren (this);
2834 expression (DO_COND (t
));
2835 pp_c_right_paren (this);
2836 pp_c_semicolon (this);
2837 pp_needs_newline (this) = true;
2841 pp_c_ws_string (this, "for");
2843 pp_c_left_paren (this);
2844 if (FOR_INIT_STMT (t
))
2845 statement (FOR_INIT_STMT (t
));
2847 pp_c_semicolon (this);
2848 pp_needs_newline (this) = false;
2849 pp_c_whitespace (this);
2851 expression (FOR_COND (t
));
2852 pp_c_semicolon (this);
2853 pp_needs_newline (this) = false;
2854 pp_c_whitespace (this);
2856 expression (FOR_EXPR (t
));
2857 pp_c_right_paren (this);
2858 pp_newline_and_indent (this, 3);
2859 statement (FOR_BODY (t
));
2860 pp_indentation (this) -= 3;
2861 pp_needs_newline (this) = true;
2867 return expression(opt) ; */
2870 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
2871 pp_c_semicolon (this);
2872 pp_needs_newline (this) = true;
2876 if (pp_needs_newline (this))
2877 pp_newline_and_indent (this, 0);
2878 dump_generic_node (this, t
, pp_indentation (this), TDF_NONE
, true);
2883 /* Initialize the PRETTY-PRINTER for handling C codes. */
2885 c_pretty_printer::c_pretty_printer ()
2886 : pretty_printer (),
2890 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2891 ptr_operator
= pp_c_pointer
;
2892 parameter_list
= pp_c_parameter_type_list
;
2895 /* c_pretty_printer's implementation of pretty_printer::clone vfunc. */
2898 c_pretty_printer::clone () const
2900 return new c_pretty_printer (*this);
2903 /* Print the tree T in full, on file FILE. */
2906 print_c_tree (FILE *file
, tree t
)
2908 c_pretty_printer pp
;
2910 pp_needs_newline (&pp
) = true;
2911 pp
.buffer
->stream
= file
;
2913 pp_newline_and_flush (&pp
);
2916 /* Print the tree T in full, on stderr. */
2919 debug_c_tree (tree t
)
2921 print_c_tree (stderr
, t
);
2922 fputc ('\n', stderr
);
2925 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2926 up of T's memory address. */
2929 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2933 gcc_assert (DECL_P (t
));
2936 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2939 static char xname
[8];
2940 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2945 pp_c_identifier (pp
, name
);
2950 namespace selftest
{
2952 /* Selftests for pretty-printing trees. */
2954 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2955 LOC as the effective location for any failures. */
2958 assert_c_pretty_printer_output (const location
&loc
, const char *expected
,
2961 c_pretty_printer pp
;
2962 pp
.expression (expr
);
2963 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2966 /* Helper function for calling assert_c_pretty_printer_output.
2967 This is to avoid having to write SELFTEST_LOCATION. */
2969 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2970 SELFTEST_BEGIN_STMT \
2971 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
2976 /* Verify that location wrappers don't show up in pretty-printed output. */
2979 test_location_wrappers ()
2982 tree id
= get_identifier ("foo");
2983 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, id
,
2985 tree wrapped_decl
= maybe_wrap_with_location (decl
, BUILTINS_LOCATION
);
2986 ASSERT_NE (wrapped_decl
, decl
);
2987 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl
);
2988 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl
);
2991 tree int_cst
= build_int_cst (integer_type_node
, 42);
2992 tree wrapped_cst
= maybe_wrap_with_location (int_cst
, BUILTINS_LOCATION
);
2993 ASSERT_NE (wrapped_cst
, int_cst
);
2994 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst
);
2995 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst
);
2998 /* Run all of the selftests within this file. */
3001 c_pretty_print_c_tests ()
3003 test_location_wrappers ();
3006 } // namespace selftest
3008 #endif /* CHECKING_P */