1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2013 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"
27 #include "c-pretty-print.h"
28 #include "tree-pretty-print.h"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
42 if ((PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
47 static void pp_c_char (c_pretty_printer
*, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
53 static void pp_c_multiplicative_expression (c_pretty_printer
*, tree
);
54 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
55 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
56 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
57 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
58 static void pp_c_and_expression (c_pretty_printer
*, tree
);
59 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
60 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
61 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
62 static void pp_c_conditional_expression (c_pretty_printer
*, tree
);
63 static void pp_c_assignment_expression (c_pretty_printer
*, tree
);
68 /* Helper functions. */
71 pp_c_whitespace (c_pretty_printer
*pp
)
74 pp
->padding
= pp_none
;
78 pp_c_left_paren (c_pretty_printer
*pp
)
81 pp
->padding
= pp_none
;
85 pp_c_right_paren (c_pretty_printer
*pp
)
88 pp
->padding
= pp_none
;
92 pp_c_left_brace (c_pretty_printer
*pp
)
95 pp
->padding
= pp_none
;
99 pp_c_right_brace (c_pretty_printer
*pp
)
102 pp
->padding
= pp_none
;
106 pp_c_left_bracket (c_pretty_printer
*pp
)
108 pp_left_bracket (pp
);
109 pp
->padding
= pp_none
;
113 pp_c_right_bracket (c_pretty_printer
*pp
)
115 pp_right_bracket (pp
);
116 pp
->padding
= pp_none
;
120 pp_c_dot (c_pretty_printer
*pp
)
123 pp
->padding
= pp_none
;
127 pp_c_ampersand (c_pretty_printer
*pp
)
130 pp
->padding
= pp_none
;
134 pp_c_star (c_pretty_printer
*pp
)
137 pp
->padding
= pp_none
;
141 pp_c_arrow (c_pretty_printer
*pp
)
144 pp
->padding
= pp_none
;
148 pp_c_semicolon (c_pretty_printer
*pp
)
151 pp
->padding
= pp_none
;
155 pp_c_complement (c_pretty_printer
*pp
)
158 pp
->padding
= pp_none
;
162 pp_c_exclamation (c_pretty_printer
*pp
)
165 pp
->padding
= pp_none
;
168 /* Print out the external representation of QUALIFIERS. */
171 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
173 const char *p
= pp_last_position_in_text (pp
);
174 bool previous
= false;
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_CONST
)
187 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
191 if (qualifiers
& TYPE_QUAL_VOLATILE
)
194 pp_c_whitespace (pp
);
195 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
199 if (qualifiers
& TYPE_QUAL_RESTRICT
)
202 pp_c_whitespace (pp
);
203 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
204 ? "restrict" : "__restrict__"));
208 /* Pretty-print T using the type-cast notation '( type-name )'. */
211 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
213 pp_c_left_paren (pp
);
215 pp_c_right_paren (pp
);
218 /* We're about to pretty-print a pointer type as indicated by T.
219 Output a whitespace, if needed, preparing for subsequent output. */
222 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
224 if (POINTER_TYPE_P (t
))
226 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
227 if (TREE_CODE (pointee
) != ARRAY_TYPE
228 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
229 pp_c_whitespace (pp
);
236 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
237 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
238 of its type. Take care of possible extensions.
242 type-qualifier-list type-qualifier
247 __restrict__ -- GNU C
248 address-space-qualifier -- GNU C
251 address-space-qualifier:
252 identifier -- GNU C */
255 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
259 if (!t
|| t
== error_mark_node
)
265 qualifiers
= TYPE_QUALS (t
);
266 pp_c_cv_qualifiers (pp
, qualifiers
,
267 TREE_CODE (t
) == FUNCTION_TYPE
);
269 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
271 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
272 pp_c_identifier (pp
, as
);
277 * type-qualifier-list(opt)
278 * type-qualifier-list(opt) pointer */
281 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
283 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
285 switch (TREE_CODE (t
))
288 /* It is easier to handle C++ reference types here. */
290 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
291 pp_c_pointer (pp
, TREE_TYPE (t
));
292 if (TREE_CODE (t
) == POINTER_TYPE
)
296 pp_c_type_qualifier_list (pp
, t
);
299 /* ??? This node is now in GENERIC and so shouldn't be here. But
300 we'll fix that later. */
302 pp_declaration (pp
, DECL_EXPR_DECL (t
));
303 pp_needs_newline (pp
) = true;
307 pp_unsupported_tree (pp
, t
);
324 struct-or-union-specifier
329 simple-type-specifier:
334 pp_c_type_specifier (c_pretty_printer
*pp
, tree t
)
336 const enum tree_code code
= TREE_CODE (t
);
340 pp
->translate_string ("<type-error>");
343 case IDENTIFIER_NODE
:
344 pp_c_identifier (pp
, IDENTIFIER_POINTER (t
));
351 case FIXED_POINT_TYPE
:
355 pp_c_type_specifier (pp
, t
);
359 int prec
= TYPE_PRECISION (t
);
360 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
361 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_SATURATING (t
));
363 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_UNSIGNED (t
));
366 pp_c_type_specifier (pp
, t
);
367 if (TYPE_PRECISION (t
) != prec
)
370 pp_decimal_int (pp
, prec
);
378 pp
->translate_string (TYPE_UNSIGNED (t
)
379 ? "<unnamed-unsigned:"
380 : "<unnamed-signed:");
383 pp
->translate_string ("<unnamed-float:");
385 case FIXED_POINT_TYPE
:
386 pp
->translate_string ("<unnamed-fixed:");
391 pp_decimal_int (pp
, prec
);
399 pp_id_expression (pp
, t
);
401 pp
->translate_string ("<typedef-error>");
407 if (code
== UNION_TYPE
)
408 pp_c_ws_string (pp
, "union");
409 else if (code
== RECORD_TYPE
)
410 pp_c_ws_string (pp
, "struct");
411 else if (code
== ENUMERAL_TYPE
)
412 pp_c_ws_string (pp
, "enum");
414 pp
->translate_string ("<tag-error>");
417 pp_id_expression (pp
, TYPE_NAME (t
));
419 pp
->translate_string ("<anonymous>");
423 pp_unsupported_tree (pp
, t
);
428 /* specifier-qualifier-list:
429 type-specifier specifier-qualifier-list-opt
430 type-qualifier specifier-qualifier-list-opt
433 Implementation note: Because of the non-linearities in array or
434 function declarations, this routine prints not just the
435 specifier-qualifier-list of such entities or types of such entities,
436 but also the 'pointer' production part of their declarators. The
437 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
440 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
442 const enum tree_code code
= TREE_CODE (t
);
444 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
445 pp_c_type_qualifier_list (pp
, t
);
451 /* Get the types-specifier of this type. */
452 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
453 pp_c_specifier_qualifier_list (pp
, pointee
);
454 if (TREE_CODE (pointee
) == ARRAY_TYPE
455 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
457 pp_c_whitespace (pp
);
458 pp_c_left_paren (pp
);
459 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
461 else if (!c_dialect_cxx ())
462 pp_c_whitespace (pp
);
463 pp_ptr_operator (pp
, t
);
469 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
474 if (code
== COMPLEX_TYPE
)
475 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
476 ? "_Complex" : "__complex__"));
477 else if (code
== VECTOR_TYPE
)
479 pp_c_ws_string (pp
, "__vector");
480 pp_c_left_paren (pp
);
481 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
482 pp_c_right_paren (pp
);
483 pp_c_whitespace (pp
);
485 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
489 pp_simple_type_specifier (pp
, t
);
492 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
493 pp_c_type_qualifier_list (pp
, t
);
496 /* parameter-type-list:
501 parameter-declaration
502 parameter-list , parameter-declaration
504 parameter-declaration:
505 declaration-specifiers declarator
506 declaration-specifiers abstract-declarator(opt) */
509 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
511 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
512 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
513 pp_c_left_paren (pp
);
514 if (parms
== void_list_node
)
515 pp_c_ws_string (pp
, "void");
519 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
522 pp_separate_with (pp
, ',');
524 pp_declaration_specifiers
525 (pp
, want_parm_decl
? parms
: TREE_VALUE (parms
));
527 pp_declarator (pp
, parms
);
529 pp_abstract_declarator (pp
, TREE_VALUE (parms
));
532 pp_c_right_paren (pp
);
535 /* abstract-declarator:
537 pointer(opt) direct-abstract-declarator */
540 pp_c_abstract_declarator (c_pretty_printer
*pp
, tree t
)
542 if (TREE_CODE (t
) == POINTER_TYPE
)
544 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
545 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
546 pp_c_right_paren (pp
);
550 pp_direct_abstract_declarator (pp
, t
);
553 /* direct-abstract-declarator:
554 ( abstract-declarator )
555 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
556 direct-abstract-declarator(opt) [ * ]
557 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
560 pp_c_direct_abstract_declarator (c_pretty_printer
*pp
, tree t
)
562 switch (TREE_CODE (t
))
565 pp_abstract_declarator (pp
, t
);
569 pp_c_parameter_type_list (pp
, t
);
570 pp_direct_abstract_declarator (pp
, TREE_TYPE (t
));
574 pp_c_left_bracket (pp
);
575 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
577 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
578 tree type
= TREE_TYPE (maxval
);
580 if (host_integerp (maxval
, 0))
581 pp_wide_integer (pp
, tree_low_cst (maxval
, 0) + 1);
583 pp_expression (pp
, fold_build2 (PLUS_EXPR
, type
, maxval
,
584 build_int_cst (type
, 1)));
586 pp_c_right_bracket (pp
);
587 pp_direct_abstract_declarator (pp
, TREE_TYPE (t
));
590 case IDENTIFIER_NODE
:
595 case FIXED_POINT_TYPE
:
605 pp_unsupported_tree (pp
, t
);
611 specifier-qualifier-list abstract-declarator(opt) */
614 pp_c_type_id (c_pretty_printer
*pp
, tree t
)
616 pp_c_specifier_qualifier_list (pp
, t
);
617 pp_abstract_declarator (pp
, t
);
620 /* storage-class-specifier:
628 pp_c_storage_class_specifier (c_pretty_printer
*pp
, tree t
)
630 if (TREE_CODE (t
) == TYPE_DECL
)
631 pp_c_ws_string (pp
, "typedef");
634 if (DECL_REGISTER (t
))
635 pp_c_ws_string (pp
, "register");
636 else if (TREE_STATIC (t
) && TREE_CODE (t
) == VAR_DECL
)
637 pp_c_ws_string (pp
, "static");
641 /* function-specifier:
645 pp_c_function_specifier (c_pretty_printer
*pp
, tree t
)
647 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
648 pp_c_ws_string (pp
, "inline");
651 /* declaration-specifiers:
652 storage-class-specifier declaration-specifiers(opt)
653 type-specifier declaration-specifiers(opt)
654 type-qualifier declaration-specifiers(opt)
655 function-specifier declaration-specifiers(opt) */
658 pp_c_declaration_specifiers (c_pretty_printer
*pp
, tree t
)
660 pp_storage_class_specifier (pp
, t
);
661 pp_function_specifier (pp
, t
);
662 pp_c_specifier_qualifier_list (pp
, DECL_P (t
) ? TREE_TYPE (t
) : t
);
668 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
669 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
670 direct-declarator [ type-qualifier-list static assignment-expression ]
671 direct-declarator [ type-qualifier-list * ]
672 direct-declarator ( parameter-type-list )
673 direct-declarator ( identifier-list(opt) ) */
676 pp_c_direct_declarator (c_pretty_printer
*pp
, tree t
)
678 switch (TREE_CODE (t
))
685 pp_c_space_for_pointer_operator (pp
, TREE_TYPE (t
));
686 pp_c_tree_decl_identifier (pp
, t
);
691 pp_abstract_declarator (pp
, TREE_TYPE (t
));
695 pp_parameter_list (pp
, t
);
696 pp_abstract_declarator (pp
, TREE_TYPE (t
));
700 pp_c_space_for_pointer_operator (pp
, TREE_TYPE (TREE_TYPE (t
)));
701 pp_c_tree_decl_identifier (pp
, t
);
702 if (pp
->flags
& pp_c_flag_abstract
)
703 pp_abstract_declarator (pp
, TREE_TYPE (t
));
706 pp_parameter_list (pp
, t
);
707 pp_abstract_declarator (pp
, TREE_TYPE (TREE_TYPE (t
)));
713 case FIXED_POINT_TYPE
:
720 pp_unsupported_tree (pp
, t
);
727 pointer(opt) direct-declarator */
730 pp_c_declarator (c_pretty_printer
*pp
, tree t
)
732 switch (TREE_CODE (t
))
736 case FIXED_POINT_TYPE
:
749 pp_direct_declarator (pp
, t
);
754 pp_unsupported_tree (pp
, t
);
760 declaration-specifiers init-declarator-list(opt) ; */
763 pp_c_declaration (c_pretty_printer
*pp
, tree t
)
765 pp_declaration_specifiers (pp
, t
);
766 pp_c_init_declarator (pp
, t
);
769 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
772 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
774 if (attributes
== NULL_TREE
)
777 pp_c_ws_string (pp
, "__attribute__");
778 pp_c_left_paren (pp
);
779 pp_c_left_paren (pp
);
780 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
782 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
783 if (TREE_VALUE (attributes
))
784 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
786 if (TREE_CHAIN (attributes
))
787 pp_separate_with (pp
, ',');
789 pp_c_right_paren (pp
);
790 pp_c_right_paren (pp
);
793 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
794 marked to be displayed on disgnostic. */
797 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
799 bool is_first
= true;
804 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
806 const struct attribute_spec
*as
;
807 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
808 if (!as
|| as
->affects_type_identity
== false)
812 pp_c_ws_string (pp
, "__attribute__");
813 pp_c_left_paren (pp
);
814 pp_c_left_paren (pp
);
819 pp_separate_with (pp
, ',');
821 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
823 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
828 pp_c_right_paren (pp
);
829 pp_c_right_paren (pp
);
830 pp_c_whitespace (pp
);
834 /* function-definition:
835 declaration-specifiers declarator compound-statement */
838 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
840 pp_declaration_specifiers (pp
, t
);
841 pp_declarator (pp
, t
);
842 pp_needs_newline (pp
) = true;
843 pp_statement (pp
, DECL_SAVED_TREE (t
));
844 pp_newline_and_flush (pp
);
850 /* Print out a c-char. This is called solely for characters which are
851 in the *target* execution character set. We ought to convert them
852 back to the *host* execution character set before printing, but we
853 have no way to do this at present. A decent compromise is to print
854 all characters as if they were in the host execution character set,
855 and not attempt to recover any named escape characters, but render
856 all unprintables as octal escapes. If the host and target character
857 sets are the same, this produces relatively readable output. If they
858 are not the same, strings may appear as gibberish, but that's okay
859 (in fact, it may well be what the reader wants, e.g. if they are looking
860 to see if conversion to the target character set happened correctly).
862 A special case: we need to prefix \, ", and ' with backslashes. It is
863 correct to do so for the *host*'s \, ", and ', because the rest of the
864 file appears in the host character set. */
867 pp_c_char (c_pretty_printer
*pp
, int c
)
873 case '\\': pp_string (pp
, "\\\\"); break;
874 case '\'': pp_string (pp
, "\\\'"); break;
875 case '\"': pp_string (pp
, "\\\""); break;
876 default: pp_character (pp
, c
);
880 pp_scalar (pp
, "\\%03o", (unsigned) c
);
883 /* Print out a STRING literal. */
886 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
888 const char *p
= TREE_STRING_POINTER (s
);
889 int n
= TREE_STRING_LENGTH (s
) - 1;
892 for (i
= 0; i
< n
; ++i
)
893 pp_c_char (pp
, p
[i
]);
897 /* Pretty-print an INTEGER literal. */
900 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
902 /* We are going to compare the type of I to other types using
903 pointer comparison so we need to use its canonical type. */
905 TYPE_CANONICAL (TREE_TYPE (i
))
906 ? TYPE_CANONICAL (TREE_TYPE (i
))
909 if (host_integerp (i
, 0))
910 pp_wide_integer (pp
, TREE_INT_CST_LOW (i
));
911 else if (host_integerp (i
, 1))
912 pp_unsigned_wide_integer (pp
, TREE_INT_CST_LOW (i
));
915 unsigned HOST_WIDE_INT low
= TREE_INT_CST_LOW (i
);
916 HOST_WIDE_INT high
= TREE_INT_CST_HIGH (i
);
917 if (tree_int_cst_sgn (i
) < 0)
923 sprintf (pp_buffer (pp
)->digit_buffer
, HOST_WIDE_INT_PRINT_DOUBLE_HEX
,
924 (unsigned HOST_WIDE_INT
) high
, (unsigned HOST_WIDE_INT
) low
);
925 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
927 if (TYPE_UNSIGNED (type
))
928 pp_character (pp
, 'u');
929 if (type
== long_integer_type_node
|| type
== long_unsigned_type_node
)
930 pp_character (pp
, 'l');
931 else if (type
== long_long_integer_type_node
932 || type
== long_long_unsigned_type_node
)
933 pp_string (pp
, "ll");
934 else if (type
== int128_integer_type_node
935 || type
== int128_unsigned_type_node
)
936 pp_string (pp
, "I128");
939 /* Print out a CHARACTER literal. */
942 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
944 tree type
= TREE_TYPE (c
);
945 if (type
== wchar_type_node
)
946 pp_character (pp
, 'L');
948 if (host_integerp (c
, TYPE_UNSIGNED (type
)))
949 pp_c_char (pp
, tree_low_cst (c
, TYPE_UNSIGNED (type
)));
951 pp_scalar (pp
, "\\x%x", (unsigned) TREE_INT_CST_LOW (c
));
955 /* Print out a BOOLEAN literal. */
958 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
960 if (b
== boolean_false_node
)
962 if (c_dialect_cxx ())
963 pp_c_ws_string (pp
, "false");
964 else if (flag_isoc99
)
965 pp_c_ws_string (pp
, "_False");
967 pp_unsupported_tree (pp
, b
);
969 else if (b
== boolean_true_node
)
971 if (c_dialect_cxx ())
972 pp_c_ws_string (pp
, "true");
973 else if (flag_isoc99
)
974 pp_c_ws_string (pp
, "_True");
976 pp_unsupported_tree (pp
, b
);
978 else if (TREE_CODE (b
) == INTEGER_CST
)
979 pp_c_integer_constant (pp
, b
);
981 pp_unsupported_tree (pp
, b
);
984 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
985 false; that means the value was obtained by a cast, in which case
986 print out the type-id part of the cast-expression -- the casted value
987 is then printed by pp_c_integer_literal. */
990 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
992 bool value_is_named
= true;
993 tree type
= TREE_TYPE (e
);
996 /* Find the name of this constant. */
997 for (value
= TYPE_VALUES (type
);
998 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
999 value
= TREE_CHAIN (value
))
1002 if (value
!= NULL_TREE
)
1003 pp_id_expression (pp
, TREE_PURPOSE (value
));
1006 /* Value must have been cast. */
1007 pp_c_type_cast (pp
, type
);
1008 value_is_named
= false;
1011 return value_is_named
;
1014 /* Print out a REAL value as a decimal-floating-constant. */
1017 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1019 const struct real_format
*fmt
1020 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1022 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1023 bool is_decimal
= floating_cst
.decimal
;
1025 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1026 log10(2) to 7 significant digits. */
1027 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1029 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1030 sizeof (pp_buffer (pp
)->digit_buffer
),
1033 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1034 if (TREE_TYPE (r
) == float_type_node
)
1035 pp_character (pp
, 'f');
1036 else if (TREE_TYPE (r
) == long_double_type_node
)
1037 pp_character (pp
, 'l');
1038 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1039 pp_string (pp
, "dl");
1040 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1041 pp_string (pp
, "dd");
1042 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1043 pp_string (pp
, "df");
1046 /* Print out a FIXED value as a decimal-floating-constant. */
1049 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1051 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1052 sizeof (pp_buffer (pp
)->digit_buffer
));
1053 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1056 /* Pretty-print a compound literal expression. GNU extensions include
1057 vector constants. */
1060 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1062 tree type
= TREE_TYPE (e
);
1063 pp_c_type_cast (pp
, type
);
1065 switch (TREE_CODE (type
))
1072 pp_c_brace_enclosed_initializer_list (pp
, e
);
1076 pp_unsupported_tree (pp
, e
);
1081 /* Pretty-print a COMPLEX_EXPR expression. */
1084 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1086 /* Handle a few common special cases, otherwise fallback
1087 to printing it as compound literal. */
1088 tree type
= TREE_TYPE (e
);
1089 tree realexpr
= TREE_OPERAND (e
, 0);
1090 tree imagexpr
= TREE_OPERAND (e
, 1);
1092 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1093 if (TREE_CODE (realexpr
) == NOP_EXPR
1094 && TREE_CODE (imagexpr
) == NOP_EXPR
1095 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1096 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1097 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1098 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1099 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1100 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1102 pp_c_type_cast (pp
, type
);
1103 pp_expression (pp
, TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1107 /* Cast of an scalar expression to COMPLEX_TYPE. */
1108 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1109 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1111 pp_c_type_cast (pp
, type
);
1112 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1113 realexpr
= TREE_OPERAND (realexpr
, 0);
1114 pp_expression (pp
, realexpr
);
1118 pp_c_compound_literal (pp
, e
);
1124 fixed-point-constant
1125 enumeration-constant
1126 character-constant */
1129 c_pretty_printer::constant (tree e
)
1131 const enum tree_code code
= TREE_CODE (e
);
1137 tree type
= TREE_TYPE (e
);
1138 if (type
== boolean_type_node
)
1139 pp_c_bool_constant (this, e
);
1140 else if (type
== char_type_node
)
1141 pp_c_character_constant (this, e
);
1142 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1143 && pp_c_enumeration_constant (this, e
))
1146 pp_c_integer_constant (this, e
);
1151 pp_c_floating_constant (this, e
);
1155 pp_c_fixed_constant (this, e
);
1159 pp_c_string_literal (this, e
);
1163 /* Sometimes, we are confused and we think a complex literal
1164 is a constant. Such thing is a compound literal which
1165 grammatically belongs to postfix-expr production. */
1166 pp_c_compound_literal (this, e
);
1170 pp_unsupported_tree (this, e
);
1175 /* Pretty-print a string such as an identifier, without changing its
1176 encoding, preceded by whitespace is necessary. */
1179 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1181 pp_c_maybe_whitespace (pp
);
1182 pp_string (pp
, str
);
1183 pp
->padding
= pp_before
;
1187 c_pretty_printer::translate_string (const char *gmsgid
)
1189 if (pp_translate_identifiers (this))
1190 pp_c_ws_string (this, _(gmsgid
));
1192 pp_c_ws_string (this, gmsgid
);
1195 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1196 that need converting to the locale encoding, preceded by whitespace
1200 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1202 pp_c_maybe_whitespace (pp
);
1203 pp_identifier (pp
, id
);
1204 pp
->padding
= pp_before
;
1207 /* Pretty-print a C primary-expression.
1215 c_pretty_printer::primary_expression (tree e
)
1217 switch (TREE_CODE (e
))
1225 pp_c_tree_decl_identifier (this, e
);
1228 case IDENTIFIER_NODE
:
1229 pp_c_tree_identifier (this, e
);
1233 translate_string ("<erroneous-expression>");
1237 translate_string ("<return-value>");
1248 pp_c_ws_string (this, "__builtin_memcpy");
1249 pp_c_left_paren (this);
1250 pp_ampersand (this);
1251 primary_expression (TREE_OPERAND (e
, 0));
1252 pp_separate_with (this, ',');
1253 pp_ampersand (this);
1254 pp_initializer (this, TREE_OPERAND (e
, 1));
1255 if (TREE_OPERAND (e
, 2))
1257 pp_separate_with (this, ',');
1258 pp_c_expression (this, TREE_OPERAND (e
, 2));
1260 pp_c_right_paren (this);
1264 /* FIXME: Make sure we won't get into an infinite loop. */
1265 pp_c_left_paren (this);
1266 pp_expression (this, e
);
1267 pp_c_right_paren (this);
1272 /* Print out a C initializer -- also support C compound-literals.
1274 assignment-expression:
1275 { initializer-list }
1276 { initializer-list , } */
1279 pp_c_initializer (c_pretty_printer
*pp
, tree e
)
1281 if (TREE_CODE (e
) == CONSTRUCTOR
)
1282 pp_c_brace_enclosed_initializer_list (pp
, e
);
1284 pp_expression (pp
, e
);
1289 declarator = initializer */
1292 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1294 pp_declarator (pp
, t
);
1295 /* We don't want to output function definitions here. There are handled
1296 elsewhere (and the syntactic form is bogus anyway). */
1297 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1299 tree init
= DECL_INITIAL (t
);
1300 /* This C++ bit is handled here because it is easier to do so.
1301 In templates, the C++ parser builds a TREE_LIST for a
1302 direct-initialization; the TREE_PURPOSE is the variable to
1303 initialize and the TREE_VALUE is the initializer. */
1304 if (TREE_CODE (init
) == TREE_LIST
)
1306 pp_c_left_paren (pp
);
1307 pp_expression (pp
, TREE_VALUE (init
));
1308 pp_right_paren (pp
);
1315 pp_c_initializer (pp
, init
);
1320 /* initializer-list:
1321 designation(opt) initializer
1322 initializer-list , designation(opt) initializer
1329 designator-list designator
1332 [ constant-expression ]
1336 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1338 tree type
= TREE_TYPE (e
);
1339 const enum tree_code code
= TREE_CODE (type
);
1341 if (TREE_CODE (e
) == CONSTRUCTOR
)
1343 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1353 tree init
= TREE_OPERAND (e
, 0);
1354 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1356 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1359 pp_primary_expression (pp
, TREE_PURPOSE (init
));
1363 pp_c_left_bracket (pp
);
1364 if (TREE_PURPOSE (init
))
1365 pp_constant (pp
, TREE_PURPOSE (init
));
1366 pp_c_right_bracket (pp
);
1368 pp_c_whitespace (pp
);
1370 pp_c_whitespace (pp
);
1371 pp_initializer (pp
, TREE_VALUE (init
));
1372 if (TREE_CHAIN (init
))
1373 pp_separate_with (pp
, ',');
1379 if (TREE_CODE (e
) == VECTOR_CST
)
1382 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1385 pp_separate_with (pp
, ',');
1386 pp_expression (pp
, VECTOR_CST_ELT (e
, i
));
1394 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1396 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1397 pp_expression (pp
, cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1398 pp_separate_with (pp
, ',');
1399 pp_expression (pp
, cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1409 pp_unsupported_tree (pp
, type
);
1412 /* Pretty-print a brace-enclosed initializer-list. */
1415 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1417 pp_c_left_brace (pp
);
1418 pp_c_initializer_list (pp
, l
);
1419 pp_c_right_brace (pp
);
1423 /* This is a convenient function, used to bridge gap between C and C++
1430 c_pretty_printer::id_expression (tree t
)
1432 switch (TREE_CODE (t
))
1441 pp_c_tree_decl_identifier (this, t
);
1444 case IDENTIFIER_NODE
:
1445 pp_c_tree_identifier (this, t
);
1449 pp_unsupported_tree (this, t
);
1454 /* postfix-expression:
1456 postfix-expression [ expression ]
1457 postfix-expression ( argument-expression-list(opt) )
1458 postfix-expression . identifier
1459 postfix-expression -> identifier
1460 postfix-expression ++
1461 postfix-expression --
1462 ( type-name ) { initializer-list }
1463 ( type-name ) { initializer-list , } */
1466 c_pretty_printer::postfix_expression (tree e
)
1468 enum tree_code code
= TREE_CODE (e
);
1471 case POSTINCREMENT_EXPR
:
1472 case POSTDECREMENT_EXPR
:
1473 postfix_expression (TREE_OPERAND (e
, 0));
1474 pp_string (this, code
== POSTINCREMENT_EXPR
? "++" : "--");
1478 postfix_expression (TREE_OPERAND (e
, 0));
1479 pp_c_left_bracket (this);
1480 pp_expression (this, TREE_OPERAND (e
, 1));
1481 pp_c_right_bracket (this);
1484 case ARRAY_NOTATION_REF
:
1485 postfix_expression (ARRAY_NOTATION_ARRAY (e
));
1486 pp_c_left_bracket (this);
1487 pp_expression (this, ARRAY_NOTATION_START (e
));
1489 pp_expression (this, ARRAY_NOTATION_LENGTH (e
));
1491 pp_expression (this, ARRAY_NOTATION_STRIDE (e
));
1492 pp_c_right_bracket (this);
1497 call_expr_arg_iterator iter
;
1499 postfix_expression (CALL_EXPR_FN (e
));
1500 pp_c_left_paren (this);
1501 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1503 pp_expression (this, arg
);
1504 if (more_call_expr_args_p (&iter
))
1505 pp_separate_with (this, ',');
1507 pp_c_right_paren (this);
1511 case UNORDERED_EXPR
:
1512 pp_c_ws_string (this, flag_isoc99
1514 : "__builtin_isunordered");
1518 pp_c_ws_string (this, flag_isoc99
1520 : "!__builtin_isunordered");
1524 pp_c_ws_string (this, flag_isoc99
1526 : "!__builtin_isgreaterequal");
1530 pp_c_ws_string (this, flag_isoc99
1532 : "!__builtin_isgreater");
1536 pp_c_ws_string (this, flag_isoc99
1538 : "!__builtin_islessequal");
1542 pp_c_ws_string (this, flag_isoc99
1544 : "!__builtin_isless");
1548 pp_c_ws_string (this, flag_isoc99
1550 : "!__builtin_islessgreater");
1554 pp_c_ws_string (this, flag_isoc99
1556 : "__builtin_islessgreater");
1560 pp_c_left_paren (this);
1561 pp_expression (this, TREE_OPERAND (e
, 0));
1562 pp_separate_with (this, ',');
1563 pp_expression (this, TREE_OPERAND (e
, 1));
1564 pp_c_right_paren (this);
1568 pp_c_ws_string (this, "__builtin_abs");
1569 pp_c_left_paren (this);
1570 pp_expression (this, TREE_OPERAND (e
, 0));
1571 pp_c_right_paren (this);
1576 tree object
= TREE_OPERAND (e
, 0);
1577 if (TREE_CODE (object
) == INDIRECT_REF
)
1579 postfix_expression (TREE_OPERAND (object
, 0));
1584 postfix_expression (object
);
1587 pp_expression (this, TREE_OPERAND (e
, 1));
1593 tree type
= TREE_TYPE (e
);
1595 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1597 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1599 HOST_WIDE_INT bitpos
= tree_low_cst (TREE_OPERAND (e
, 2), 0);
1600 HOST_WIDE_INT size
= tree_low_cst (TYPE_SIZE (type
), 0);
1601 if ((bitpos
% size
) == 0)
1603 pp_c_left_paren (this);
1604 pp_c_left_paren (this);
1605 pp_type_id (this, type
);
1607 pp_c_right_paren (this);
1608 pp_c_ampersand (this);
1609 pp_expression (this, TREE_OPERAND (e
, 0));
1610 pp_c_right_paren (this);
1611 pp_c_left_bracket (this);
1612 pp_wide_integer (this, bitpos
/ size
);
1613 pp_c_right_bracket (this);
1617 pp_unsupported_tree (this, e
);
1622 pp_c_expression (this, e
);
1627 pp_c_compound_literal (this, e
);
1631 pp_c_complex_expr (this, e
);
1634 case COMPOUND_LITERAL_EXPR
:
1635 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1638 pp_initializer (this, e
);
1642 pp_c_ws_string (this, "__builtin_va_arg");
1643 pp_c_left_paren (this);
1644 pp_assignment_expression (this, TREE_OPERAND (e
, 0));
1645 pp_separate_with (this, ',');
1646 pp_type_id (this, TREE_TYPE (e
));
1647 pp_c_right_paren (this);
1651 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1653 id_expression (TREE_OPERAND (e
, 0));
1656 /* else fall through. */
1659 primary_expression (e
);
1664 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1667 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1669 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1671 pp_expression (pp
, TREE_VALUE (e
));
1673 pp_separate_with (pp
, ',');
1677 /* Print out V, which contains the elements of a constructor. */
1680 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1682 unsigned HOST_WIDE_INT ix
;
1685 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1687 pp_expression (pp
, value
);
1688 if (ix
!= vec_safe_length (v
) - 1)
1689 pp_separate_with (pp
, ',');
1693 /* Print out an expression-list in parens, as if it were the argument
1694 list to a function. */
1697 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1699 pp_c_left_paren (pp
);
1700 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1701 pp_c_expression_list (pp
, t
);
1702 pp_c_right_paren (pp
);
1705 /* unary-expression:
1709 unary-operator cast-expression
1710 sizeof unary-expression
1713 unary-operator: one of
1718 __alignof__ unary-expression
1719 __alignof__ ( type-id )
1720 __real__ unary-expression
1721 __imag__ unary-expression */
1724 pp_c_unary_expression (c_pretty_printer
*pp
, tree e
)
1726 enum tree_code code
= TREE_CODE (e
);
1729 case PREINCREMENT_EXPR
:
1730 case PREDECREMENT_EXPR
:
1731 pp_string (pp
, code
== PREINCREMENT_EXPR
? "++" : "--");
1732 pp_c_unary_expression (pp
, TREE_OPERAND (e
, 0));
1739 case TRUTH_NOT_EXPR
:
1741 /* String literal are used by address. */
1742 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1744 else if (code
== INDIRECT_REF
)
1746 else if (code
== NEGATE_EXPR
)
1748 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1750 else if (code
== TRUTH_NOT_EXPR
)
1751 pp_exclamation (pp
);
1752 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1756 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1757 && integer_zerop (TREE_OPERAND (e
, 1)))
1758 pp_c_expression (pp
, TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1762 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1764 pp_c_left_paren (pp
);
1765 if (!integer_onep (TYPE_SIZE_UNIT
1766 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1767 pp_c_type_cast (pp
, ptr_type_node
);
1769 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1770 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1773 pp_c_integer_constant (pp
,
1774 fold_convert (ssizetype
,
1775 TREE_OPERAND (e
, 1)));
1776 pp_c_right_paren (pp
);
1783 pp_c_ws_string (pp
, code
== REALPART_EXPR
? "__real__" : "__imag__");
1784 pp_c_whitespace (pp
);
1785 pp_unary_expression (pp
, TREE_OPERAND (e
, 0));
1789 pp_postfix_expression (pp
, e
);
1796 ( type-name ) cast-expression */
1799 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1801 switch (TREE_CODE (e
))
1804 case FIX_TRUNC_EXPR
:
1806 case VIEW_CONVERT_EXPR
:
1807 pp_c_type_cast (pp
, TREE_TYPE (e
));
1808 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1812 pp_unary_expression (pp
, e
);
1816 /* multiplicative-expression:
1818 multiplicative-expression * cast-expression
1819 multiplicative-expression / cast-expression
1820 multiplicative-expression % cast-expression */
1823 pp_c_multiplicative_expression (c_pretty_printer
*pp
, tree e
)
1825 enum tree_code code
= TREE_CODE (e
);
1829 case TRUNC_DIV_EXPR
:
1830 case TRUNC_MOD_EXPR
:
1831 pp_multiplicative_expression (pp
, TREE_OPERAND (e
, 0));
1832 pp_c_whitespace (pp
);
1833 if (code
== MULT_EXPR
)
1835 else if (code
== TRUNC_DIV_EXPR
)
1839 pp_c_whitespace (pp
);
1840 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 1));
1844 pp_c_cast_expression (pp
, e
);
1849 /* additive-expression:
1850 multiplicative-expression
1851 additive-expression + multiplicative-expression
1852 additive-expression - multiplicative-expression */
1855 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1857 enum tree_code code
= TREE_CODE (e
);
1860 case POINTER_PLUS_EXPR
:
1863 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1864 pp_c_whitespace (pp
);
1865 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1869 pp_c_whitespace (pp
);
1870 pp_multiplicative_expression (pp
, TREE_OPERAND (e
, 1));
1874 pp_multiplicative_expression (pp
, e
);
1879 /* additive-expression:
1881 shift-expression << additive-expression
1882 shift-expression >> additive-expression */
1885 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1887 enum tree_code code
= TREE_CODE (e
);
1892 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1893 pp_c_whitespace (pp
);
1894 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" : ">>");
1895 pp_c_whitespace (pp
);
1896 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1900 pp_c_additive_expression (pp
, e
);
1904 /* relational-expression:
1906 relational-expression < shift-expression
1907 relational-expression > shift-expression
1908 relational-expression <= shift-expression
1909 relational-expression >= shift-expression */
1912 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1914 enum tree_code code
= TREE_CODE (e
);
1921 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1922 pp_c_whitespace (pp
);
1923 if (code
== LT_EXPR
)
1925 else if (code
== GT_EXPR
)
1927 else if (code
== LE_EXPR
)
1929 else if (code
== GE_EXPR
)
1930 pp_greater_equal (pp
);
1931 pp_c_whitespace (pp
);
1932 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1936 pp_c_shift_expression (pp
, e
);
1941 /* equality-expression:
1942 relational-expression
1943 equality-expression == relational-expression
1944 equality-equality != relational-expression */
1947 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1949 enum tree_code code
= TREE_CODE (e
);
1954 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1955 pp_c_whitespace (pp
);
1956 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1957 pp_c_whitespace (pp
);
1958 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1962 pp_c_relational_expression (pp
, e
);
1969 AND-expression & equality-equality */
1972 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1974 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1976 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1977 pp_c_whitespace (pp
);
1979 pp_c_whitespace (pp
);
1980 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1983 pp_c_equality_expression (pp
, e
);
1986 /* exclusive-OR-expression:
1988 exclusive-OR-expression ^ AND-expression */
1991 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
1993 if (TREE_CODE (e
) == BIT_XOR_EXPR
1994 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
1996 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
1997 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
1998 pp_c_maybe_whitespace (pp
);
2000 pp_c_whitespace (pp
);
2002 pp_c_whitespace (pp
);
2003 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2006 pp_c_and_expression (pp
, e
);
2009 /* inclusive-OR-expression:
2010 exclusive-OR-expression
2011 inclusive-OR-expression | exclusive-OR-expression */
2014 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2016 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2018 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2019 pp_c_whitespace (pp
);
2021 pp_c_whitespace (pp
);
2022 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2025 pp_c_exclusive_or_expression (pp
, e
);
2028 /* logical-AND-expression:
2029 inclusive-OR-expression
2030 logical-AND-expression && inclusive-OR-expression */
2033 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2035 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2036 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2038 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2039 pp_c_whitespace (pp
);
2040 pp_ampersand_ampersand (pp
);
2041 pp_c_whitespace (pp
);
2042 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2045 pp_c_inclusive_or_expression (pp
, e
);
2048 /* logical-OR-expression:
2049 logical-AND-expression
2050 logical-OR-expression || logical-AND-expression */
2053 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2055 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2056 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2058 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2059 pp_c_whitespace (pp
);
2061 pp_c_whitespace (pp
);
2062 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2065 pp_c_logical_and_expression (pp
, e
);
2068 /* conditional-expression:
2069 logical-OR-expression
2070 logical-OR-expression ? expression : conditional-expression */
2073 pp_c_conditional_expression (c_pretty_printer
*pp
, tree e
)
2075 if (TREE_CODE (e
) == COND_EXPR
)
2077 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2078 pp_c_whitespace (pp
);
2080 pp_c_whitespace (pp
);
2081 pp_expression (pp
, TREE_OPERAND (e
, 1));
2082 pp_c_whitespace (pp
);
2084 pp_c_whitespace (pp
);
2085 pp_c_conditional_expression (pp
, TREE_OPERAND (e
, 2));
2088 pp_c_logical_or_expression (pp
, e
);
2092 /* assignment-expression:
2093 conditional-expression
2094 unary-expression assignment-operator assignment-expression
2096 assignment-expression: one of
2097 = *= /= %= += -= >>= <<= &= ^= |= */
2100 pp_c_assignment_expression (c_pretty_printer
*pp
, tree e
)
2102 if (TREE_CODE (e
) == MODIFY_EXPR
2103 || TREE_CODE (e
) == INIT_EXPR
)
2105 pp_c_unary_expression (pp
, TREE_OPERAND (e
, 0));
2106 pp_c_whitespace (pp
);
2109 pp_c_expression (pp
, TREE_OPERAND (e
, 1));
2112 pp_c_conditional_expression (pp
, e
);
2116 assignment-expression
2117 expression , assignment-expression
2119 Implementation note: instead of going through the usual recursion
2120 chain, I take the liberty of dispatching nodes to the appropriate
2121 functions. This makes some redundancy, but it worths it. That also
2122 prevents a possible infinite recursion between pp_primary_expression ()
2123 and pp_c_expression (). */
2126 pp_c_expression (c_pretty_printer
*pp
, tree e
)
2128 switch (TREE_CODE (e
))
2131 pp_c_integer_constant (pp
, e
);
2135 pp_c_floating_constant (pp
, e
);
2139 pp_c_fixed_constant (pp
, e
);
2143 pp_c_string_literal (pp
, e
);
2146 case IDENTIFIER_NODE
:
2155 pp_primary_expression (pp
, e
);
2159 if (SSA_NAME_VAR (e
)
2160 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2161 pp_c_expression (pp
, SSA_NAME_VAR (e
));
2163 pp
->translate_string ("<unknown>");
2166 case POSTINCREMENT_EXPR
:
2167 case POSTDECREMENT_EXPR
:
2169 case ARRAY_NOTATION_REF
:
2177 case UNORDERED_EXPR
:
2186 case COMPOUND_LITERAL_EXPR
:
2188 pp_postfix_expression (pp
, e
);
2197 case TRUTH_NOT_EXPR
:
2198 case PREINCREMENT_EXPR
:
2199 case PREDECREMENT_EXPR
:
2202 pp_c_unary_expression (pp
, e
);
2206 case FIX_TRUNC_EXPR
:
2208 case VIEW_CONVERT_EXPR
:
2209 pp_c_cast_expression (pp
, e
);
2213 case TRUNC_MOD_EXPR
:
2214 case TRUNC_DIV_EXPR
:
2215 pp_multiplicative_expression (pp
, e
);
2220 pp_c_shift_expression (pp
, e
);
2227 pp_c_relational_expression (pp
, e
);
2231 pp_c_and_expression (pp
, e
);
2235 case TRUTH_XOR_EXPR
:
2236 pp_c_exclusive_or_expression (pp
, e
);
2240 pp_c_inclusive_or_expression (pp
, e
);
2243 case TRUTH_ANDIF_EXPR
:
2244 case TRUTH_AND_EXPR
:
2245 pp_c_logical_and_expression (pp
, e
);
2248 case TRUTH_ORIF_EXPR
:
2250 pp_c_logical_or_expression (pp
, e
);
2255 pp_c_equality_expression (pp
, e
);
2259 pp_conditional_expression (pp
, e
);
2262 case POINTER_PLUS_EXPR
:
2265 pp_c_additive_expression (pp
, e
);
2270 pp_assignment_expression (pp
, e
);
2274 pp_c_left_paren (pp
);
2275 pp_expression (pp
, TREE_OPERAND (e
, 0));
2276 pp_separate_with (pp
, ',');
2277 pp_assignment_expression (pp
, TREE_OPERAND (e
, 1));
2278 pp_c_right_paren (pp
);
2281 case NON_LVALUE_EXPR
:
2283 pp_expression (pp
, TREE_OPERAND (e
, 0));
2287 pp_postfix_expression (pp
, TREE_OPERAND (e
, 1));
2292 /* We don't yet have a way of dumping statements in a
2293 human-readable format. */
2294 pp_string (pp
, "({...})");
2297 case C_MAYBE_CONST_EXPR
:
2298 pp_c_expression (pp
, C_MAYBE_CONST_EXPR_EXPR (e
));
2302 pp_unsupported_tree (pp
, e
);
2312 pp_c_statement (c_pretty_printer
*pp
, tree stmt
)
2317 if (pp_needs_newline (pp
))
2318 pp_newline_and_indent (pp
, 0);
2320 dump_generic_node (pp
, stmt
, pp_indentation (pp
), 0, true);
2324 /* Initialize the PRETTY-PRINTER for handling C codes. */
2326 c_pretty_printer::c_pretty_printer ()
2331 declaration
= pp_c_declaration
;
2332 declaration_specifiers
= pp_c_declaration_specifiers
;
2333 declarator
= pp_c_declarator
;
2334 direct_declarator
= pp_c_direct_declarator
;
2335 type_specifier_seq
= pp_c_specifier_qualifier_list
;
2336 abstract_declarator
= pp_c_abstract_declarator
;
2337 direct_abstract_declarator
= pp_c_direct_abstract_declarator
;
2338 ptr_operator
= pp_c_pointer
;
2339 parameter_list
= pp_c_parameter_type_list
;
2340 type_id
= pp_c_type_id
;
2341 simple_type_specifier
= pp_c_type_specifier
;
2342 function_specifier
= pp_c_function_specifier
;
2343 storage_class_specifier
= pp_c_storage_class_specifier
;
2345 statement
= pp_c_statement
;
2347 unary_expression
= pp_c_unary_expression
;
2348 initializer
= pp_c_initializer
;
2349 multiplicative_expression
= pp_c_multiplicative_expression
;
2350 conditional_expression
= pp_c_conditional_expression
;
2351 assignment_expression
= pp_c_assignment_expression
;
2352 expression
= pp_c_expression
;
2356 /* Print the tree T in full, on file FILE. */
2359 print_c_tree (FILE *file
, tree t
)
2361 c_pretty_printer pp
;
2363 pp_needs_newline (&pp
) = true;
2364 pp
.buffer
->stream
= file
;
2365 pp_statement (&pp
, t
);
2366 pp_newline_and_flush (&pp
);
2369 /* Print the tree T in full, on stderr. */
2372 debug_c_tree (tree t
)
2374 print_c_tree (stderr
, t
);
2375 fputc ('\n', stderr
);
2378 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2379 up of T's memory address. */
2382 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2386 gcc_assert (DECL_P (t
));
2389 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2392 static char xname
[8];
2393 sprintf (xname
, "<U%4x>", ((unsigned)((uintptr_t)(t
) & 0xffff)));
2397 pp_c_identifier (pp
, name
);