1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2020 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
24 #include "c-pretty-print.h"
25 #include "diagnostic.h"
26 #include "stor-layout.h"
27 #include "stringpool.h"
30 #include "tree-pretty-print.h"
32 #include "langhooks.h"
34 /* The pretty-printer code is primarily designed to closely follow
35 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
36 codes we used to have in the past. Following a structured
37 approach (preferably the official grammars) is believed to make it
38 much easier to add extensions and nifty pretty-printing effects that
39 takes expression or declaration contexts into account. */
42 #define pp_c_maybe_whitespace(PP) \
44 if ((PP)->padding == pp_before) \
45 pp_c_whitespace (PP); \
49 static void pp_c_char (c_pretty_printer
*, int);
51 /* postfix-expression */
52 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
53 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
55 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
56 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
57 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
58 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
59 static void pp_c_and_expression (c_pretty_printer
*, tree
);
60 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
61 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
62 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
67 /* Helper functions. */
70 pp_c_whitespace (c_pretty_printer
*pp
)
73 pp
->padding
= pp_none
;
77 pp_c_left_paren (c_pretty_printer
*pp
)
80 pp
->padding
= pp_none
;
84 pp_c_right_paren (c_pretty_printer
*pp
)
87 pp
->padding
= pp_none
;
91 pp_c_left_brace (c_pretty_printer
*pp
)
94 pp
->padding
= pp_none
;
98 pp_c_right_brace (c_pretty_printer
*pp
)
101 pp
->padding
= pp_none
;
105 pp_c_left_bracket (c_pretty_printer
*pp
)
107 pp_left_bracket (pp
);
108 pp
->padding
= pp_none
;
112 pp_c_right_bracket (c_pretty_printer
*pp
)
114 pp_right_bracket (pp
);
115 pp
->padding
= pp_none
;
119 pp_c_dot (c_pretty_printer
*pp
)
122 pp
->padding
= pp_none
;
126 pp_c_ampersand (c_pretty_printer
*pp
)
129 pp
->padding
= pp_none
;
133 pp_c_star (c_pretty_printer
*pp
)
136 pp
->padding
= pp_none
;
140 pp_c_arrow (c_pretty_printer
*pp
)
143 pp
->padding
= pp_none
;
147 pp_c_semicolon (c_pretty_printer
*pp
)
150 pp
->padding
= pp_none
;
154 pp_c_complement (c_pretty_printer
*pp
)
157 pp
->padding
= pp_none
;
161 pp_c_exclamation (c_pretty_printer
*pp
)
164 pp
->padding
= pp_none
;
167 /* Print out the external representation of QUALIFIERS. */
170 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
172 const char *p
= pp_last_position_in_text (pp
);
177 /* The C programming language does not have references, but it is much
178 simpler to handle those here rather than going through the same
179 logic in the C++ pretty-printer. */
180 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
181 pp_c_whitespace (pp
);
183 if (qualifiers
& TYPE_QUAL_ATOMIC
)
184 pp_c_ws_string (pp
, "_Atomic");
185 if (qualifiers
& TYPE_QUAL_CONST
)
186 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
187 if (qualifiers
& TYPE_QUAL_VOLATILE
)
188 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
189 if (qualifiers
& TYPE_QUAL_RESTRICT
)
190 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
191 ? "restrict" : "__restrict__"));
194 /* Pretty-print T using the type-cast notation '( type-name )'. */
197 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
199 pp_c_left_paren (pp
);
201 pp_c_right_paren (pp
);
204 /* We're about to pretty-print a pointer type as indicated by T.
205 Output a whitespace, if needed, preparing for subsequent output. */
208 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
210 if (POINTER_TYPE_P (t
))
212 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
213 if (TREE_CODE (pointee
) != ARRAY_TYPE
214 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
215 pp_c_whitespace (pp
);
222 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
223 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
224 of its type. Take care of possible extensions.
228 type-qualifier-list type-qualifier
233 __restrict__ -- GNU C
234 address-space-qualifier -- GNU C
238 address-space-qualifier:
239 identifier -- GNU C */
242 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
246 if (!t
|| t
== error_mark_node
)
252 if (TREE_CODE (t
) != ARRAY_TYPE
)
254 qualifiers
= TYPE_QUALS (t
);
255 pp_c_cv_qualifiers (pp
, qualifiers
,
256 TREE_CODE (t
) == FUNCTION_TYPE
);
259 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
261 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
262 pp_c_identifier (pp
, as
);
267 * type-qualifier-list(opt)
268 * type-qualifier-list(opt) pointer */
271 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
273 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
275 switch (TREE_CODE (t
))
278 /* It is easier to handle C++ reference types here. */
280 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
281 pp_c_pointer (pp
, TREE_TYPE (t
));
282 if (TREE_CODE (t
) == POINTER_TYPE
)
287 if (TYPE_REF_IS_RVALUE (t
))
290 pp_c_type_qualifier_list (pp
, t
);
293 /* ??? This node is now in GENERIC and so shouldn't be here. But
294 we'll fix that later. */
296 pp
->declaration (DECL_EXPR_DECL (t
));
297 pp_needs_newline (pp
) = true;
301 pp_unsupported_tree (pp
, t
);
305 /* simple-type-specifier:
321 struct-or-union-specifier
326 simple-type-specifier:
331 c_pretty_printer::simple_type_specifier (tree t
)
333 const enum tree_code code
= TREE_CODE (t
);
337 translate_string ("<type-error>");
340 case IDENTIFIER_NODE
:
341 pp_c_identifier (this, IDENTIFIER_POINTER (t
));
348 case FIXED_POINT_TYPE
:
352 simple_type_specifier (t
);
356 int prec
= TYPE_PRECISION (t
);
358 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
359 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
360 TYPE_SATURATING (t
));
362 common_t
= c_common_type_for_mode (TYPE_MODE (t
),
364 if (common_t
&& TYPE_NAME (common_t
))
366 simple_type_specifier (common_t
);
367 if (TYPE_PRECISION (common_t
) != prec
)
370 pp_decimal_int (this, prec
);
378 translate_string (TYPE_UNSIGNED (t
)
379 ? "<unnamed-unsigned:"
380 : "<unnamed-signed:");
383 translate_string ("<unnamed-float:");
385 case FIXED_POINT_TYPE
:
386 translate_string ("<unnamed-fixed:");
391 pp_decimal_int (this, prec
);
401 translate_string ("<typedef-error>");
407 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
408 /* Don't decorate the type if this is a typedef name. */;
409 else if (code
== UNION_TYPE
)
410 pp_c_ws_string (this, "union");
411 else if (code
== RECORD_TYPE
)
412 pp_c_ws_string (this, "struct");
413 else if (code
== ENUMERAL_TYPE
)
414 pp_c_ws_string (this, "enum");
416 translate_string ("<tag-error>");
419 id_expression (TYPE_NAME (t
));
421 translate_string ("<anonymous>");
425 pp_unsupported_tree (this, t
);
430 /* specifier-qualifier-list:
431 type-specifier specifier-qualifier-list-opt
432 type-qualifier specifier-qualifier-list-opt
435 Implementation note: Because of the non-linearities in array or
436 function declarations, this routine prints not just the
437 specifier-qualifier-list of such entities or types of such entities,
438 but also the 'pointer' production part of their declarators. The
439 remaining part is done by declarator() or abstract_declarator(). */
442 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
444 const enum tree_code code
= TREE_CODE (t
);
446 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
447 pp_c_type_qualifier_list (pp
, t
);
453 /* Get the types-specifier of this type. */
454 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
455 pp_c_specifier_qualifier_list (pp
, pointee
);
456 if (TREE_CODE (pointee
) == ARRAY_TYPE
457 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
459 pp_c_whitespace (pp
);
460 pp_c_left_paren (pp
);
461 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
463 else if (!c_dialect_cxx ())
464 pp_c_whitespace (pp
);
465 pp_ptr_operator (pp
, t
);
471 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
476 if (code
== COMPLEX_TYPE
)
477 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
478 ? "_Complex" : "__complex__"));
479 else if (code
== VECTOR_TYPE
)
481 /* The syntax we print for vector types isn't real C or C++ syntax,
482 so it's better to print the type name if we have one. */
483 tree name
= TYPE_NAME (t
);
484 if (!(pp
->flags
& pp_c_flag_gnu_v3
)
486 && TREE_CODE (name
) == TYPE_DECL
)
488 pp
->id_expression (name
);
491 pp_c_ws_string (pp
, "__vector");
492 pp_c_left_paren (pp
);
493 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
494 pp_c_right_paren (pp
);
495 pp_c_whitespace (pp
);
497 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
501 pp
->simple_type_specifier (t
);
504 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
505 pp_c_type_qualifier_list (pp
, t
);
508 /* parameter-type-list:
513 parameter-declaration
514 parameter-list , parameter-declaration
516 parameter-declaration:
517 declaration-specifiers declarator
518 declaration-specifiers abstract-declarator(opt) */
521 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
523 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
524 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
525 pp_c_left_paren (pp
);
526 if (parms
== void_list_node
)
527 pp_c_ws_string (pp
, "void");
531 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
534 pp_separate_with (pp
, ',');
536 pp
->declaration_specifiers
537 (want_parm_decl
? parms
: TREE_VALUE (parms
));
539 pp
->declarator (parms
);
541 pp
->abstract_declarator (TREE_VALUE (parms
));
543 if (!first
&& !parms
)
545 pp_separate_with (pp
, ',');
546 pp_string (pp
, "...");
549 pp_c_right_paren (pp
);
552 /* abstract-declarator:
554 pointer(opt) direct-abstract-declarator */
557 c_pretty_printer::abstract_declarator (tree t
)
559 if (TREE_CODE (t
) == POINTER_TYPE
)
561 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
562 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
563 pp_c_right_paren (this);
567 direct_abstract_declarator (t
);
570 /* direct-abstract-declarator:
571 ( abstract-declarator )
572 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
573 direct-abstract-declarator(opt) [ * ]
574 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
577 c_pretty_printer::direct_abstract_declarator (tree t
)
579 bool add_space
= false;
581 switch (TREE_CODE (t
))
584 abstract_declarator (t
);
588 pp_c_parameter_type_list (this, t
);
589 direct_abstract_declarator (TREE_TYPE (t
));
593 pp_c_left_bracket (this);
595 if (int quals
= TYPE_QUALS (t
))
597 /* Print the array qualifiers such as in "T[const restrict 3]". */
598 pp_c_cv_qualifiers (this, quals
, false);
602 if (tree arr
= lookup_attribute ("array", TYPE_ATTRIBUTES (t
)))
604 if (TREE_VALUE (arr
))
606 /* Print the specifier as in "T[static 3]" that's not actually
607 part of the type but may be added by the front end. */
608 pp_c_ws_string (this, "static");
611 else if (!TYPE_DOMAIN (t
))
612 /* For arrays of unspecified bound using the [*] notation. */
613 pp_character (this, '*');
616 if (tree dom
= TYPE_DOMAIN (t
))
618 if (tree maxval
= TYPE_MAX_VALUE (dom
))
623 tree type
= TREE_TYPE (maxval
);
625 if (tree_fits_shwi_p (maxval
))
626 pp_wide_integer (this, tree_to_shwi (maxval
) + 1);
627 else if (TREE_CODE (maxval
) == INTEGER_CST
)
628 expression (fold_build2 (PLUS_EXPR
, type
, maxval
,
629 build_int_cst (type
, 1)));
632 /* Strip the expressions from around a VLA bound added
633 internally to make it fit the domain mold, including
635 if (TREE_CODE (maxval
) == NOP_EXPR
)
636 maxval
= TREE_OPERAND (maxval
, 0);
637 if (TREE_CODE (maxval
) == PLUS_EXPR
638 && integer_all_onesp (TREE_OPERAND (maxval
, 1)))
640 maxval
= TREE_OPERAND (maxval
, 0);
641 if (TREE_CODE (maxval
) == NOP_EXPR
)
642 maxval
= TREE_OPERAND (maxval
, 0);
644 if (TREE_CODE (maxval
) == SAVE_EXPR
)
646 maxval
= TREE_OPERAND (maxval
, 0);
647 if (TREE_CODE (maxval
) == NOP_EXPR
)
648 maxval
= TREE_OPERAND (maxval
, 0);
654 else if (TYPE_SIZE (t
))
655 /* Print zero for zero-length arrays but not for flexible
656 array members whose TYPE_SIZE is null. */
657 pp_string (this, "0");
659 pp_c_right_bracket (this);
660 direct_abstract_declarator (TREE_TYPE (t
));
663 case IDENTIFIER_NODE
:
668 case FIXED_POINT_TYPE
:
678 pp_unsupported_tree (this, t
);
684 specifier-qualifier-list abstract-declarator(opt) */
687 c_pretty_printer::type_id (tree t
)
689 pp_c_specifier_qualifier_list (this, t
);
690 abstract_declarator (t
);
693 /* storage-class-specifier:
701 c_pretty_printer::storage_class_specifier (tree t
)
703 if (TREE_CODE (t
) == TYPE_DECL
)
704 pp_c_ws_string (this, "typedef");
707 if (DECL_REGISTER (t
))
708 pp_c_ws_string (this, "register");
709 else if (TREE_STATIC (t
) && VAR_P (t
))
710 pp_c_ws_string (this, "static");
714 /* function-specifier:
718 c_pretty_printer::function_specifier (tree t
)
720 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
721 pp_c_ws_string (this, "inline");
724 /* declaration-specifiers:
725 storage-class-specifier declaration-specifiers(opt)
726 type-specifier declaration-specifiers(opt)
727 type-qualifier declaration-specifiers(opt)
728 function-specifier declaration-specifiers(opt) */
731 c_pretty_printer::declaration_specifiers (tree t
)
733 storage_class_specifier (t
);
734 function_specifier (t
);
735 pp_c_specifier_qualifier_list (this, DECL_P (t
) ? TREE_TYPE (t
) : t
);
741 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
742 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
743 direct-declarator [ type-qualifier-list static assignment-expression ]
744 direct-declarator [ type-qualifier-list * ]
745 direct-declarator ( parameter-type-list )
746 direct-declarator ( identifier-list(opt) ) */
749 c_pretty_printer::direct_declarator (tree t
)
751 switch (TREE_CODE (t
))
758 pp_c_space_for_pointer_operator (this, TREE_TYPE (t
));
759 pp_c_tree_decl_identifier (this, t
);
764 abstract_declarator (TREE_TYPE (t
));
768 pp_parameter_list (this, t
);
769 abstract_declarator (TREE_TYPE (t
));
773 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
774 pp_c_tree_decl_identifier (this, t
);
775 if (flags
& pp_c_flag_abstract
)
776 abstract_declarator (TREE_TYPE (t
));
779 pp_parameter_list (this, t
);
780 abstract_declarator (TREE_TYPE (TREE_TYPE (t
)));
786 case FIXED_POINT_TYPE
:
793 pp_unsupported_tree (this, t
);
800 pointer(opt) direct-declarator */
803 c_pretty_printer::declarator (tree t
)
805 switch (TREE_CODE (t
))
809 case FIXED_POINT_TYPE
:
822 direct_declarator (t
);
827 pp_unsupported_tree (this, t
);
833 declaration-specifiers init-declarator-list(opt) ; */
836 c_pretty_printer::declaration (tree t
)
838 declaration_specifiers (t
);
839 pp_c_init_declarator (this, t
);
842 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
845 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
847 if (attributes
== NULL_TREE
)
850 pp_c_ws_string (pp
, "__attribute__");
851 pp_c_left_paren (pp
);
852 pp_c_left_paren (pp
);
853 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
855 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
856 if (TREE_VALUE (attributes
))
857 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
859 if (TREE_CHAIN (attributes
))
860 pp_separate_with (pp
, ',');
862 pp_c_right_paren (pp
);
863 pp_c_right_paren (pp
);
866 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
867 marked to be displayed on disgnostic. */
870 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
872 bool is_first
= true;
877 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
879 const struct attribute_spec
*as
;
880 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
881 if (!as
|| as
->affects_type_identity
== false)
884 && !strcmp ("transaction_safe", as
->name
))
885 /* In C++ transaction_safe is printed at the end of the declarator. */
889 pp_c_ws_string (pp
, "__attribute__");
890 pp_c_left_paren (pp
);
891 pp_c_left_paren (pp
);
896 pp_separate_with (pp
, ',');
898 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
900 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
905 pp_c_right_paren (pp
);
906 pp_c_right_paren (pp
);
907 pp_c_whitespace (pp
);
911 /* function-definition:
912 declaration-specifiers declarator compound-statement */
915 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
917 pp
->declaration_specifiers (t
);
919 pp_needs_newline (pp
) = true;
920 pp
->statement (DECL_SAVED_TREE (t
));
921 pp_newline_and_flush (pp
);
927 /* Print out a c-char. This is called solely for characters which are
928 in the *target* execution character set. We ought to convert them
929 back to the *host* execution character set before printing, but we
930 have no way to do this at present. A decent compromise is to print
931 all characters as if they were in the host execution character set,
932 and not attempt to recover any named escape characters, but render
933 all unprintables as octal escapes. If the host and target character
934 sets are the same, this produces relatively readable output. If they
935 are not the same, strings may appear as gibberish, but that's okay
936 (in fact, it may well be what the reader wants, e.g. if they are looking
937 to see if conversion to the target character set happened correctly).
939 A special case: we need to prefix \, ", and ' with backslashes. It is
940 correct to do so for the *host*'s \, ", and ', because the rest of the
941 file appears in the host character set. */
944 pp_c_char (c_pretty_printer
*pp
, int c
)
950 case '\\': pp_string (pp
, "\\\\"); break;
951 case '\'': pp_string (pp
, "\\\'"); break;
952 case '\"': pp_string (pp
, "\\\""); break;
953 default: pp_character (pp
, c
);
957 pp_scalar (pp
, "\\%03o", (unsigned) c
);
960 /* Print out a STRING literal. */
963 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
965 const char *p
= TREE_STRING_POINTER (s
);
966 int n
= TREE_STRING_LENGTH (s
) - 1;
969 for (i
= 0; i
< n
; ++i
)
970 pp_c_char (pp
, p
[i
]);
974 /* Pretty-print a VOID_CST (void_node). */
977 pp_c_void_constant (c_pretty_printer
*pp
)
979 pp_c_type_cast (pp
, void_type_node
);
983 /* Pretty-print an INTEGER literal. */
986 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
988 if (tree_fits_shwi_p (i
))
989 pp_wide_integer (pp
, tree_to_shwi (i
));
990 else if (tree_fits_uhwi_p (i
))
991 pp_unsigned_wide_integer (pp
, tree_to_uhwi (i
));
994 wide_int wi
= wi::to_wide (i
);
996 if (wi::lt_p (wi::to_wide (i
), 0, TYPE_SIGN (TREE_TYPE (i
))))
1001 print_hex (wi
, pp_buffer (pp
)->digit_buffer
);
1002 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
1006 /* Print out a CHARACTER literal. */
1009 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
1012 pp_c_char (pp
, (unsigned) TREE_INT_CST_LOW (c
));
1016 /* Print out a BOOLEAN literal. */
1019 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
1021 if (b
== boolean_false_node
)
1023 if (c_dialect_cxx ())
1024 pp_c_ws_string (pp
, "false");
1025 else if (flag_isoc99
)
1026 pp_c_ws_string (pp
, "_False");
1028 pp_unsupported_tree (pp
, b
);
1030 else if (b
== boolean_true_node
)
1032 if (c_dialect_cxx ())
1033 pp_c_ws_string (pp
, "true");
1034 else if (flag_isoc99
)
1035 pp_c_ws_string (pp
, "_True");
1037 pp_unsupported_tree (pp
, b
);
1039 else if (TREE_CODE (b
) == INTEGER_CST
)
1040 pp_c_integer_constant (pp
, b
);
1042 pp_unsupported_tree (pp
, b
);
1045 /* Given a value e of ENUMERAL_TYPE:
1046 Print out the first ENUMERATOR id with value e, if one is found,
1047 else print out the value as a C-style cast (type-id)value. */
1050 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
1052 tree type
= TREE_TYPE (e
);
1053 tree value
= NULL_TREE
;
1055 /* Find the name of this constant. */
1056 if ((pp
->flags
& pp_c_flag_gnu_v3
) == 0)
1057 for (value
= TYPE_VALUES (type
); value
!= NULL_TREE
;
1058 value
= TREE_CHAIN (value
))
1059 if (tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value
)), e
))
1062 if (value
!= NULL_TREE
)
1063 pp
->id_expression (TREE_PURPOSE (value
));
1066 /* Value must have been cast. */
1067 pp_c_type_cast (pp
, type
);
1068 pp_c_integer_constant (pp
, e
);
1072 /* Print out a REAL value as a decimal-floating-constant. */
1075 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1077 const struct real_format
*fmt
1078 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1080 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1081 bool is_decimal
= floating_cst
.decimal
;
1083 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1084 log10(2) to 7 significant digits. */
1085 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1087 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1088 sizeof (pp_buffer (pp
)->digit_buffer
),
1091 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1092 if (TREE_TYPE (r
) == float_type_node
)
1093 pp_character (pp
, 'f');
1094 else if (TREE_TYPE (r
) == long_double_type_node
)
1095 pp_character (pp
, 'l');
1096 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1097 pp_string (pp
, "dl");
1098 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1099 pp_string (pp
, "dd");
1100 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1101 pp_string (pp
, "df");
1102 else if (TREE_TYPE (r
) != double_type_node
)
1103 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
1104 if (TREE_TYPE (r
) == FLOATN_NX_TYPE_NODE (i
))
1106 pp_character (pp
, 'f');
1107 pp_decimal_int (pp
, floatn_nx_types
[i
].n
);
1108 if (floatn_nx_types
[i
].extended
)
1109 pp_character (pp
, 'x');
1114 /* Print out a FIXED value as a decimal-floating-constant. */
1117 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1119 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1120 sizeof (pp_buffer (pp
)->digit_buffer
));
1121 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1124 /* Pretty-print a compound literal expression. GNU extensions include
1125 vector constants. */
1128 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1130 tree type
= TREE_TYPE (e
);
1131 pp_c_type_cast (pp
, type
);
1133 switch (TREE_CODE (type
))
1140 pp_c_brace_enclosed_initializer_list (pp
, e
);
1144 pp_unsupported_tree (pp
, e
);
1149 /* Pretty-print a COMPLEX_EXPR expression. */
1152 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1154 /* Handle a few common special cases, otherwise fallback
1155 to printing it as compound literal. */
1156 tree type
= TREE_TYPE (e
);
1157 tree realexpr
= TREE_OPERAND (e
, 0);
1158 tree imagexpr
= TREE_OPERAND (e
, 1);
1160 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1161 if (TREE_CODE (realexpr
) == NOP_EXPR
1162 && TREE_CODE (imagexpr
) == NOP_EXPR
1163 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1164 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1165 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1166 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1167 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1168 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1170 pp_c_type_cast (pp
, type
);
1171 pp
->expression (TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1175 /* Cast of an scalar expression to COMPLEX_TYPE. */
1176 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1177 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1179 pp_c_type_cast (pp
, type
);
1180 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1181 realexpr
= TREE_OPERAND (realexpr
, 0);
1182 pp
->expression (realexpr
);
1186 pp_c_compound_literal (pp
, e
);
1192 fixed-point-constant
1193 enumeration-constant
1194 character-constant */
1197 c_pretty_printer::constant (tree e
)
1199 const enum tree_code code
= TREE_CODE (e
);
1204 pp_c_void_constant (this);
1209 tree type
= TREE_TYPE (e
);
1210 if (type
== boolean_type_node
)
1211 pp_c_bool_constant (this, e
);
1212 else if (type
== char_type_node
)
1213 pp_c_character_constant (this, e
);
1214 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1215 pp_c_enumeration_constant (this, e
);
1217 pp_c_integer_constant (this, e
);
1222 pp_c_floating_constant (this, e
);
1226 pp_c_fixed_constant (this, e
);
1230 pp_c_string_literal (this, e
);
1234 /* Sometimes, we are confused and we think a complex literal
1235 is a constant. Such thing is a compound literal which
1236 grammatically belongs to postfix-expr production. */
1237 pp_c_compound_literal (this, e
);
1241 pp_unsupported_tree (this, e
);
1246 /* Pretty-print a string such as an identifier, without changing its
1247 encoding, preceded by whitespace is necessary. */
1250 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1252 pp_c_maybe_whitespace (pp
);
1253 pp_string (pp
, str
);
1254 pp
->padding
= pp_before
;
1258 c_pretty_printer::translate_string (const char *gmsgid
)
1260 if (pp_translate_identifiers (this))
1261 pp_c_ws_string (this, _(gmsgid
));
1263 pp_c_ws_string (this, gmsgid
);
1266 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1267 that need converting to the locale encoding, preceded by whitespace
1271 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1273 pp_c_maybe_whitespace (pp
);
1274 pp_identifier (pp
, id
);
1275 pp
->padding
= pp_before
;
1278 /* Pretty-print a C primary-expression.
1286 c_pretty_printer::primary_expression (tree e
)
1288 switch (TREE_CODE (e
))
1296 pp_c_tree_decl_identifier (this, e
);
1299 case IDENTIFIER_NODE
:
1300 pp_c_tree_identifier (this, e
);
1304 translate_string ("<erroneous-expression>");
1308 translate_string ("<return-value>");
1320 pp_c_ws_string (this, "__builtin_memcpy");
1321 pp_c_left_paren (this);
1322 pp_ampersand (this);
1323 primary_expression (TREE_OPERAND (e
, 0));
1324 pp_separate_with (this, ',');
1325 pp_ampersand (this);
1326 initializer (TREE_OPERAND (e
, 1));
1327 if (TREE_OPERAND (e
, 2))
1329 pp_separate_with (this, ',');
1330 expression (TREE_OPERAND (e
, 2));
1332 pp_c_right_paren (this);
1336 /* FIXME: Make sure we won't get into an infinite loop. */
1337 if (location_wrapper_p (e
))
1341 pp_c_left_paren (this);
1343 pp_c_right_paren (this);
1349 /* Print out a C initializer -- also support C compound-literals.
1351 assignment-expression:
1352 { initializer-list }
1353 { initializer-list , } */
1356 c_pretty_printer::initializer (tree e
)
1358 if (TREE_CODE (e
) == CONSTRUCTOR
)
1359 pp_c_brace_enclosed_initializer_list (this, e
);
1366 declarator = initializer */
1369 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1372 /* We don't want to output function definitions here. There are handled
1373 elsewhere (and the syntactic form is bogus anyway). */
1374 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1376 tree init
= DECL_INITIAL (t
);
1377 /* This C++ bit is handled here because it is easier to do so.
1378 In templates, the C++ parser builds a TREE_LIST for a
1379 direct-initialization; the TREE_PURPOSE is the variable to
1380 initialize and the TREE_VALUE is the initializer. */
1381 if (TREE_CODE (init
) == TREE_LIST
)
1383 pp_c_left_paren (pp
);
1384 pp
->expression (TREE_VALUE (init
));
1385 pp_right_paren (pp
);
1392 pp
->initializer (init
);
1397 /* initializer-list:
1398 designation(opt) initializer
1399 initializer-list , designation(opt) initializer
1406 designator-list designator
1409 [ constant-expression ]
1413 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1415 tree type
= TREE_TYPE (e
);
1416 const enum tree_code code
= TREE_CODE (type
);
1418 if (TREE_CODE (e
) == CONSTRUCTOR
)
1420 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1430 tree init
= TREE_OPERAND (e
, 0);
1431 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1433 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1436 pp
->primary_expression (TREE_PURPOSE (init
));
1440 pp_c_left_bracket (pp
);
1441 if (TREE_PURPOSE (init
))
1442 pp
->constant (TREE_PURPOSE (init
));
1443 pp_c_right_bracket (pp
);
1445 pp_c_whitespace (pp
);
1447 pp_c_whitespace (pp
);
1448 pp
->initializer (TREE_VALUE (init
));
1449 if (TREE_CHAIN (init
))
1450 pp_separate_with (pp
, ',');
1456 if (TREE_CODE (e
) == VECTOR_CST
)
1458 /* We don't create variable-length VECTOR_CSTs. */
1459 unsigned int nunits
= VECTOR_CST_NELTS (e
).to_constant ();
1460 for (unsigned int i
= 0; i
< nunits
; ++i
)
1463 pp_separate_with (pp
, ',');
1464 pp
->expression (VECTOR_CST_ELT (e
, i
));
1472 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1474 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1475 pp
->expression (cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1476 pp_separate_with (pp
, ',');
1477 pp
->expression (cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1487 pp_unsupported_tree (pp
, type
);
1490 /* Pretty-print a brace-enclosed initializer-list. */
1493 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1495 pp_c_left_brace (pp
);
1496 pp_c_initializer_list (pp
, l
);
1497 pp_c_right_brace (pp
);
1501 /* This is a convenient function, used to bridge gap between C and C++
1508 c_pretty_printer::id_expression (tree t
)
1510 switch (TREE_CODE (t
))
1519 pp_c_tree_decl_identifier (this, t
);
1522 case IDENTIFIER_NODE
:
1523 pp_c_tree_identifier (this, t
);
1527 pp_unsupported_tree (this, t
);
1532 /* postfix-expression:
1534 postfix-expression [ expression ]
1535 postfix-expression ( argument-expression-list(opt) )
1536 postfix-expression . identifier
1537 postfix-expression -> identifier
1538 postfix-expression ++
1539 postfix-expression --
1540 ( type-name ) { initializer-list }
1541 ( type-name ) { initializer-list , } */
1544 c_pretty_printer::postfix_expression (tree e
)
1546 enum tree_code code
= TREE_CODE (e
);
1549 case POSTINCREMENT_EXPR
:
1550 case POSTDECREMENT_EXPR
:
1551 postfix_expression (TREE_OPERAND (e
, 0));
1552 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1556 postfix_expression (TREE_OPERAND (e
, 0));
1557 pp_c_left_bracket (this);
1558 expression (TREE_OPERAND (e
, 1));
1559 pp_c_right_bracket (this);
1564 call_expr_arg_iterator iter
;
1566 postfix_expression (CALL_EXPR_FN (e
));
1567 pp_c_left_paren (this);
1568 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1571 if (more_call_expr_args_p (&iter
))
1572 pp_separate_with (this, ',');
1574 pp_c_right_paren (this);
1578 case UNORDERED_EXPR
:
1579 pp_c_ws_string (this, flag_isoc99
1581 : "__builtin_isunordered");
1585 pp_c_ws_string (this, flag_isoc99
1587 : "!__builtin_isunordered");
1591 pp_c_ws_string (this, flag_isoc99
1593 : "!__builtin_isgreaterequal");
1597 pp_c_ws_string (this, flag_isoc99
1599 : "!__builtin_isgreater");
1603 pp_c_ws_string (this, flag_isoc99
1605 : "!__builtin_islessequal");
1609 pp_c_ws_string (this, flag_isoc99
1611 : "!__builtin_isless");
1615 pp_c_ws_string (this, flag_isoc99
1617 : "!__builtin_islessgreater");
1621 pp_c_ws_string (this, flag_isoc99
1623 : "__builtin_islessgreater");
1627 pp_c_ws_string (this, "max");
1631 pp_c_ws_string (this, "min");
1635 pp_c_left_paren (this);
1636 expression (TREE_OPERAND (e
, 0));
1637 pp_separate_with (this, ',');
1638 expression (TREE_OPERAND (e
, 1));
1639 pp_c_right_paren (this);
1643 pp_c_ws_string (this, "__builtin_abs");
1644 pp_c_left_paren (this);
1645 expression (TREE_OPERAND (e
, 0));
1646 pp_c_right_paren (this);
1651 tree object
= TREE_OPERAND (e
, 0);
1652 if (INDIRECT_REF_P (object
))
1654 postfix_expression (TREE_OPERAND (object
, 0));
1659 postfix_expression (object
);
1662 expression (TREE_OPERAND (e
, 1));
1668 tree type
= TREE_TYPE (e
);
1670 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1672 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1674 HOST_WIDE_INT bitpos
= tree_to_shwi (TREE_OPERAND (e
, 2));
1675 HOST_WIDE_INT size
= tree_to_shwi (TYPE_SIZE (type
));
1676 if ((bitpos
% size
) == 0)
1678 pp_c_left_paren (this);
1679 pp_c_left_paren (this);
1682 pp_c_right_paren (this);
1683 pp_c_ampersand (this);
1684 expression (TREE_OPERAND (e
, 0));
1685 pp_c_right_paren (this);
1686 pp_c_left_bracket (this);
1687 pp_wide_integer (this, bitpos
/ size
);
1688 pp_c_right_bracket (this);
1692 pp_unsupported_tree (this, e
);
1697 case TARGET_MEM_REF
:
1703 pp_c_compound_literal (this, e
);
1707 pp_c_complex_expr (this, e
);
1710 case COMPOUND_LITERAL_EXPR
:
1711 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1718 pp_c_ws_string (this, "__builtin_va_arg");
1719 pp_c_left_paren (this);
1720 assignment_expression (TREE_OPERAND (e
, 0));
1721 pp_separate_with (this, ',');
1722 type_id (TREE_TYPE (e
));
1723 pp_c_right_paren (this);
1727 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1729 id_expression (TREE_OPERAND (e
, 0));
1735 primary_expression (e
);
1740 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1743 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1745 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1747 pp
->expression (TREE_VALUE (e
));
1749 pp_separate_with (pp
, ',');
1753 /* Print out V, which contains the elements of a constructor. */
1756 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1758 unsigned HOST_WIDE_INT ix
;
1761 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1763 pp
->expression (value
);
1764 if (ix
!= vec_safe_length (v
) - 1)
1765 pp_separate_with (pp
, ',');
1769 /* Print out an expression-list in parens, as if it were the argument
1770 list to a function. */
1773 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1775 pp_c_left_paren (pp
);
1776 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1777 pp_c_expression_list (pp
, t
);
1778 pp_c_right_paren (pp
);
1781 /* unary-expression:
1785 unary-operator cast-expression
1786 sizeof unary-expression
1789 unary-operator: one of
1794 __alignof__ unary-expression
1795 __alignof__ ( type-id )
1796 __real__ unary-expression
1797 __imag__ unary-expression */
1800 c_pretty_printer::unary_expression (tree e
)
1802 enum tree_code code
= TREE_CODE (e
);
1805 case PREINCREMENT_EXPR
:
1806 case PREDECREMENT_EXPR
:
1807 pp_string (this, code
== PREINCREMENT_EXPR
? "++" : "--");
1808 unary_expression (TREE_OPERAND (e
, 0));
1815 case TRUTH_NOT_EXPR
:
1817 /* String literal are used by address. */
1818 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1819 pp_ampersand (this);
1820 else if (code
== INDIRECT_REF
)
1822 tree type
= TREE_TYPE (TREE_OPERAND (e
, 0));
1823 if (type
&& TREE_CODE (type
) == REFERENCE_TYPE
)
1824 /* Reference decay is implicit, don't print anything. */;
1828 else if (code
== NEGATE_EXPR
)
1830 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1831 pp_complement (this);
1832 else if (code
== TRUTH_NOT_EXPR
)
1833 pp_exclamation (this);
1834 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1838 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1839 && integer_zerop (TREE_OPERAND (e
, 1)))
1840 expression (TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1844 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1846 pp_c_left_paren (this);
1847 tree type
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0)));
1848 if (TYPE_SIZE_UNIT (type
) == NULL_TREE
1849 || !integer_onep (TYPE_SIZE_UNIT (type
)))
1850 pp_c_type_cast (this, ptr_type_node
);
1852 pp_c_cast_expression (this, TREE_OPERAND (e
, 0));
1853 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1856 pp_c_integer_constant (this,
1857 fold_convert (ssizetype
,
1858 TREE_OPERAND (e
, 1)));
1859 pp_c_right_paren (this);
1864 case TARGET_MEM_REF
:
1865 /* TARGET_MEM_REF can't appear directly from source, but can appear
1866 during late GIMPLE optimizations and through late diagnostic we might
1867 need to support it. Print it as dereferencing of a pointer after
1868 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
1869 pointer to single byte types, so
1870 *(type *)((char *) ptr + step * index + index2) if all the operands
1871 are present and the casts are needed. */
1873 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))) == NULL_TREE
1874 || !integer_onep (TYPE_SIZE_UNIT
1875 (TREE_TYPE (TREE_TYPE (TMR_BASE (e
))))))
1877 if (TYPE_SIZE_UNIT (TREE_TYPE (e
))
1878 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (e
))))
1880 pp_c_left_paren (this);
1881 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
1885 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
1886 pp_c_left_paren (this);
1887 pp_c_type_cast (this, build_pointer_type (char_type_node
));
1890 else if (!lang_hooks
.types_compatible_p
1891 (TREE_TYPE (e
), TREE_TYPE (TREE_TYPE (TMR_BASE (e
)))))
1893 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e
)));
1894 pp_c_left_paren (this);
1897 pp_c_left_paren (this);
1898 pp_c_cast_expression (this, TMR_BASE (e
));
1899 if (TMR_STEP (e
) && TMR_INDEX (e
))
1902 pp_c_cast_expression (this, TMR_INDEX (e
));
1904 pp_c_cast_expression (this, TMR_STEP (e
));
1909 pp_c_cast_expression (this, TMR_INDEX2 (e
));
1911 if (!integer_zerop (TMR_OFFSET (e
)))
1914 pp_c_integer_constant (this,
1915 fold_convert (ssizetype
, TMR_OFFSET (e
)));
1917 pp_c_right_paren (this);
1922 pp_c_ws_string (this, code
== REALPART_EXPR
? "__real__" : "__imag__");
1923 pp_c_whitespace (this);
1924 unary_expression (TREE_OPERAND (e
, 0));
1928 postfix_expression (e
);
1935 ( type-name ) cast-expression */
1938 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1940 switch (TREE_CODE (e
))
1943 case FIX_TRUNC_EXPR
:
1945 case VIEW_CONVERT_EXPR
:
1946 if (!location_wrapper_p (e
))
1947 pp_c_type_cast (pp
, TREE_TYPE (e
));
1948 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1952 pp
->unary_expression (e
);
1956 /* multiplicative-expression:
1958 multiplicative-expression * cast-expression
1959 multiplicative-expression / cast-expression
1960 multiplicative-expression % cast-expression */
1963 c_pretty_printer::multiplicative_expression (tree e
)
1965 enum tree_code code
= TREE_CODE (e
);
1969 case TRUNC_DIV_EXPR
:
1970 case TRUNC_MOD_EXPR
:
1971 case EXACT_DIV_EXPR
:
1973 multiplicative_expression (TREE_OPERAND (e
, 0));
1974 pp_c_whitespace (this);
1975 if (code
== MULT_EXPR
)
1977 else if (code
!= TRUNC_MOD_EXPR
)
1981 pp_c_whitespace (this);
1982 pp_c_cast_expression (this, TREE_OPERAND (e
, 1));
1986 pp_c_cast_expression (this, e
);
1991 /* additive-expression:
1992 multiplicative-expression
1993 additive-expression + multiplicative-expression
1994 additive-expression - multiplicative-expression */
1997 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1999 enum tree_code code
= TREE_CODE (e
);
2002 case POINTER_PLUS_EXPR
:
2004 case POINTER_DIFF_EXPR
:
2006 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
2007 pp_c_whitespace (pp
);
2008 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
2012 pp_c_whitespace (pp
);
2014 tree op1
= TREE_OPERAND (e
, 1);
2015 if (code
== POINTER_PLUS_EXPR
2016 && TREE_CODE (op1
) == INTEGER_CST
2017 && tree_int_cst_sign_bit (op1
))
2018 /* A pointer minus an integer is represented internally as plus a very
2019 large number, don't expose that to users. */
2020 op1
= convert (ssizetype
, op1
);
2021 pp
->multiplicative_expression (op1
);
2026 pp
->multiplicative_expression (e
);
2031 /* additive-expression:
2033 shift-expression << additive-expression
2034 shift-expression >> additive-expression */
2037 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
2039 enum tree_code code
= TREE_CODE (e
);
2046 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
2047 pp_c_whitespace (pp
);
2048 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" :
2049 code
== RSHIFT_EXPR
? ">>" :
2050 code
== LROTATE_EXPR
? "<<<" : ">>>");
2051 pp_c_whitespace (pp
);
2052 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
2056 pp_c_additive_expression (pp
, e
);
2060 /* relational-expression:
2062 relational-expression < shift-expression
2063 relational-expression > shift-expression
2064 relational-expression <= shift-expression
2065 relational-expression >= shift-expression */
2068 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
2070 enum tree_code code
= TREE_CODE (e
);
2077 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
2078 pp_c_whitespace (pp
);
2079 if (code
== LT_EXPR
)
2081 else if (code
== GT_EXPR
)
2083 else if (code
== LE_EXPR
)
2085 else if (code
== GE_EXPR
)
2086 pp_greater_equal (pp
);
2087 pp_c_whitespace (pp
);
2088 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
2092 pp_c_shift_expression (pp
, e
);
2097 /* equality-expression:
2098 relational-expression
2099 equality-expression == relational-expression
2100 equality-equality != relational-expression */
2103 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
2105 enum tree_code code
= TREE_CODE (e
);
2110 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
2111 pp_c_whitespace (pp
);
2112 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
2113 pp_c_whitespace (pp
);
2114 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
2118 pp_c_relational_expression (pp
, e
);
2125 AND-expression & equality-equality */
2128 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
2130 if (TREE_CODE (e
) == BIT_AND_EXPR
)
2132 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
2133 pp_c_whitespace (pp
);
2135 pp_c_whitespace (pp
);
2136 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
2139 pp_c_equality_expression (pp
, e
);
2142 /* exclusive-OR-expression:
2144 exclusive-OR-expression ^ AND-expression */
2147 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2149 if (TREE_CODE (e
) == BIT_XOR_EXPR
2150 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2152 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2153 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2154 pp_c_maybe_whitespace (pp
);
2156 pp_c_whitespace (pp
);
2158 pp_c_whitespace (pp
);
2159 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2162 pp_c_and_expression (pp
, e
);
2165 /* inclusive-OR-expression:
2166 exclusive-OR-expression
2167 inclusive-OR-expression | exclusive-OR-expression */
2170 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2172 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2174 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2175 pp_c_whitespace (pp
);
2177 pp_c_whitespace (pp
);
2178 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2181 pp_c_exclusive_or_expression (pp
, e
);
2184 /* logical-AND-expression:
2185 inclusive-OR-expression
2186 logical-AND-expression && inclusive-OR-expression */
2189 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2191 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2192 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2194 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2195 pp_c_whitespace (pp
);
2196 pp_ampersand_ampersand (pp
);
2197 pp_c_whitespace (pp
);
2198 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2201 pp_c_inclusive_or_expression (pp
, e
);
2204 /* logical-OR-expression:
2205 logical-AND-expression
2206 logical-OR-expression || logical-AND-expression */
2209 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2211 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2212 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2214 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2215 pp_c_whitespace (pp
);
2217 pp_c_whitespace (pp
);
2218 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2221 pp_c_logical_and_expression (pp
, e
);
2224 /* conditional-expression:
2225 logical-OR-expression
2226 logical-OR-expression ? expression : conditional-expression */
2229 c_pretty_printer::conditional_expression (tree e
)
2231 if (TREE_CODE (e
) == COND_EXPR
)
2233 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
2234 pp_c_whitespace (this);
2236 pp_c_whitespace (this);
2237 expression (TREE_OPERAND (e
, 1));
2238 pp_c_whitespace (this);
2240 pp_c_whitespace (this);
2241 conditional_expression (TREE_OPERAND (e
, 2));
2244 pp_c_logical_or_expression (this, e
);
2248 /* assignment-expression:
2249 conditional-expression
2250 unary-expression assignment-operator assignment-expression
2252 assignment-expression: one of
2253 = *= /= %= += -= >>= <<= &= ^= |= */
2256 c_pretty_printer::assignment_expression (tree e
)
2258 if (TREE_CODE (e
) == MODIFY_EXPR
2259 || TREE_CODE (e
) == INIT_EXPR
)
2261 unary_expression (TREE_OPERAND (e
, 0));
2262 pp_c_whitespace (this);
2265 expression (TREE_OPERAND (e
, 1));
2268 conditional_expression (e
);
2272 assignment-expression
2273 expression , assignment-expression
2275 Implementation note: instead of going through the usual recursion
2276 chain, I take the liberty of dispatching nodes to the appropriate
2277 functions. This makes some redundancy, but it worths it. That also
2278 prevents a possible infinite recursion between primary_expression ()
2279 and expression (). */
2282 c_pretty_printer::expression (tree e
)
2284 switch (TREE_CODE (e
))
2287 pp_c_void_constant (this);
2291 pp_c_integer_constant (this, e
);
2295 pp_c_floating_constant (this, e
);
2299 pp_c_fixed_constant (this, e
);
2303 pp_c_string_literal (this, e
);
2306 case IDENTIFIER_NODE
:
2315 primary_expression (e
);
2319 if (SSA_NAME_VAR (e
)
2320 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2321 expression (SSA_NAME_VAR (e
));
2323 translate_string ("<unknown>");
2326 case POSTINCREMENT_EXPR
:
2327 case POSTDECREMENT_EXPR
:
2336 case UNORDERED_EXPR
:
2347 case COMPOUND_LITERAL_EXPR
:
2349 postfix_expression (e
);
2356 case TARGET_MEM_REF
:
2359 case TRUTH_NOT_EXPR
:
2360 case PREINCREMENT_EXPR
:
2361 case PREDECREMENT_EXPR
:
2364 unary_expression (e
);
2368 case FIX_TRUNC_EXPR
:
2370 case VIEW_CONVERT_EXPR
:
2371 pp_c_cast_expression (this, e
);
2375 case TRUNC_MOD_EXPR
:
2376 case TRUNC_DIV_EXPR
:
2377 case EXACT_DIV_EXPR
:
2379 multiplicative_expression (e
);
2386 pp_c_shift_expression (this, e
);
2393 pp_c_relational_expression (this, e
);
2397 pp_c_and_expression (this, e
);
2401 case TRUTH_XOR_EXPR
:
2402 pp_c_exclusive_or_expression (this, e
);
2406 pp_c_inclusive_or_expression (this, e
);
2409 case TRUTH_ANDIF_EXPR
:
2410 case TRUTH_AND_EXPR
:
2411 pp_c_logical_and_expression (this, e
);
2414 case TRUTH_ORIF_EXPR
:
2416 pp_c_logical_or_expression (this, e
);
2421 pp_c_equality_expression (this, e
);
2425 conditional_expression (e
);
2428 case POINTER_PLUS_EXPR
:
2430 case POINTER_DIFF_EXPR
:
2432 pp_c_additive_expression (this, e
);
2437 assignment_expression (e
);
2441 pp_c_left_paren (this);
2442 expression (TREE_OPERAND (e
, 0));
2443 pp_separate_with (this, ',');
2444 assignment_expression (TREE_OPERAND (e
, 1));
2445 pp_c_right_paren (this);
2448 case NON_LVALUE_EXPR
:
2450 expression (TREE_OPERAND (e
, 0));
2454 postfix_expression (TREE_OPERAND (e
, 1));
2459 /* We don't yet have a way of dumping statements in a
2460 human-readable format. */
2461 pp_string (this, "({...})");
2464 case C_MAYBE_CONST_EXPR
:
2465 expression (C_MAYBE_CONST_EXPR_EXPR (e
));
2469 pp_unsupported_tree (this, e
);
2479 c_pretty_printer::statement (tree t
)
2484 switch (TREE_CODE (t
))
2488 pp_c_ws_string (this, "switch");
2490 pp_c_left_paren (this);
2491 expression (SWITCH_STMT_COND (t
));
2492 pp_c_right_paren (this);
2493 pp_indentation (this) += 3;
2494 pp_needs_newline (this) = true;
2495 statement (SWITCH_STMT_BODY (t
));
2496 pp_newline_and_indent (this, -3);
2499 /* iteration-statement:
2500 while ( expression ) statement
2501 do statement while ( expression ) ;
2502 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2503 for ( declaration expression(opt) ; expression(opt) ) statement */
2505 pp_c_ws_string (this, "while");
2507 pp_c_left_paren (this);
2508 expression (WHILE_COND (t
));
2509 pp_c_right_paren (this);
2510 pp_newline_and_indent (this, 3);
2511 statement (WHILE_BODY (t
));
2512 pp_indentation (this) -= 3;
2513 pp_needs_newline (this) = true;
2517 pp_c_ws_string (this, "do");
2518 pp_newline_and_indent (this, 3);
2519 statement (DO_BODY (t
));
2520 pp_newline_and_indent (this, -3);
2521 pp_c_ws_string (this, "while");
2523 pp_c_left_paren (this);
2524 expression (DO_COND (t
));
2525 pp_c_right_paren (this);
2526 pp_c_semicolon (this);
2527 pp_needs_newline (this) = true;
2531 pp_c_ws_string (this, "for");
2533 pp_c_left_paren (this);
2534 if (FOR_INIT_STMT (t
))
2535 statement (FOR_INIT_STMT (t
));
2537 pp_c_semicolon (this);
2538 pp_needs_newline (this) = false;
2539 pp_c_whitespace (this);
2541 expression (FOR_COND (t
));
2542 pp_c_semicolon (this);
2543 pp_needs_newline (this) = false;
2544 pp_c_whitespace (this);
2546 expression (FOR_EXPR (t
));
2547 pp_c_right_paren (this);
2548 pp_newline_and_indent (this, 3);
2549 statement (FOR_BODY (t
));
2550 pp_indentation (this) -= 3;
2551 pp_needs_newline (this) = true;
2557 return expression(opt) ; */
2560 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
2561 pp_c_semicolon (this);
2562 pp_needs_newline (this) = true;
2566 if (pp_needs_newline (this))
2567 pp_newline_and_indent (this, 0);
2568 dump_generic_node (this, t
, pp_indentation (this), TDF_NONE
, true);
2573 /* Initialize the PRETTY-PRINTER for handling C codes. */
2575 c_pretty_printer::c_pretty_printer ()
2576 : pretty_printer (),
2580 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2581 ptr_operator
= pp_c_pointer
;
2582 parameter_list
= pp_c_parameter_type_list
;
2585 /* c_pretty_printer's implementation of pretty_printer::clone vfunc. */
2588 c_pretty_printer::clone () const
2590 return new c_pretty_printer (*this);
2593 /* Print the tree T in full, on file FILE. */
2596 print_c_tree (FILE *file
, tree t
)
2598 c_pretty_printer pp
;
2600 pp_needs_newline (&pp
) = true;
2601 pp
.buffer
->stream
= file
;
2603 pp_newline_and_flush (&pp
);
2606 /* Print the tree T in full, on stderr. */
2609 debug_c_tree (tree t
)
2611 print_c_tree (stderr
, t
);
2612 fputc ('\n', stderr
);
2615 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2616 up of T's memory address. */
2619 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2623 gcc_assert (DECL_P (t
));
2626 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2629 static char xname
[8];
2630 sprintf (xname
, "<U%4hx>", ((unsigned short) ((uintptr_t) (t
)
2635 pp_c_identifier (pp
, name
);
2640 namespace selftest
{
2642 /* Selftests for pretty-printing trees. */
2644 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2645 LOC as the effective location for any failures. */
2648 assert_c_pretty_printer_output (const location
&loc
, const char *expected
,
2651 c_pretty_printer pp
;
2652 pp
.expression (expr
);
2653 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2656 /* Helper function for calling assert_c_pretty_printer_output.
2657 This is to avoid having to write SELFTEST_LOCATION. */
2659 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2660 SELFTEST_BEGIN_STMT \
2661 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
2666 /* Verify that location wrappers don't show up in pretty-printed output. */
2669 test_location_wrappers ()
2672 tree id
= get_identifier ("foo");
2673 tree decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
, id
,
2675 tree wrapped_decl
= maybe_wrap_with_location (decl
, BUILTINS_LOCATION
);
2676 ASSERT_NE (wrapped_decl
, decl
);
2677 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl
);
2678 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl
);
2681 tree int_cst
= build_int_cst (integer_type_node
, 42);
2682 tree wrapped_cst
= maybe_wrap_with_location (int_cst
, BUILTINS_LOCATION
);
2683 ASSERT_NE (wrapped_cst
, int_cst
);
2684 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst
);
2685 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst
);
2688 /* Run all of the selftests within this file. */
2691 c_pretty_print_c_tests ()
2693 test_location_wrappers ();
2696 } // namespace selftest
2698 #endif /* CHECKING_P */