1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "c-pretty-print.h"
29 #include "tree-pretty-print.h"
30 #include "tree-iterator.h"
31 #include "diagnostic.h"
33 /* Translate if being used for diagnostics, but not for dump files or
35 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
37 /* The pretty-printer code is primarily designed to closely follow
38 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
39 codes we used to have in the past. Following a structured
40 approach (preferably the official grammars) is believed to make it
41 much easier to add extensions and nifty pretty-printing effects that
42 takes expression or declaration contexts into account. */
45 #define pp_c_maybe_whitespace(PP) \
47 if (pp_base (PP)->padding == pp_before) \
48 pp_c_whitespace (PP); \
52 static void pp_c_char (c_pretty_printer *, int);
54 /* postfix-expression */
55 static void pp_c_initializer_list (c_pretty_printer *, tree);
56 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
58 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
59 static void pp_c_additive_expression (c_pretty_printer *, tree);
60 static void pp_c_shift_expression (c_pretty_printer *, tree);
61 static void pp_c_relational_expression (c_pretty_printer *, tree);
62 static void pp_c_equality_expression (c_pretty_printer *, tree);
63 static void pp_c_and_expression (c_pretty_printer *, tree);
64 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
65 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
66 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
67 static void pp_c_conditional_expression (c_pretty_printer *, tree);
68 static void pp_c_assignment_expression (c_pretty_printer *, tree);
73 /* Helper functions. */
76 pp_c_whitespace (c_pretty_printer *pp)
79 pp_base (pp)->padding = pp_none;
83 pp_c_left_paren (c_pretty_printer *pp)
86 pp_base (pp)->padding = pp_none;
90 pp_c_right_paren (c_pretty_printer *pp)
93 pp_base (pp)->padding = pp_none;
97 pp_c_left_brace (c_pretty_printer *pp)
100 pp_base (pp)->padding = pp_none;
104 pp_c_right_brace (c_pretty_printer *pp)
107 pp_base (pp)->padding = pp_none;
111 pp_c_left_bracket (c_pretty_printer *pp)
113 pp_left_bracket (pp);
114 pp_base (pp)->padding = pp_none;
118 pp_c_right_bracket (c_pretty_printer *pp)
120 pp_right_bracket (pp);
121 pp_base (pp)->padding = pp_none;
125 pp_c_dot (c_pretty_printer *pp)
128 pp_base (pp)->padding = pp_none;
132 pp_c_ampersand (c_pretty_printer *pp)
135 pp_base (pp)->padding = pp_none;
139 pp_c_star (c_pretty_printer *pp)
142 pp_base (pp)->padding = pp_none;
146 pp_c_arrow (c_pretty_printer *pp)
149 pp_base (pp)->padding = pp_none;
153 pp_c_semicolon (c_pretty_printer *pp)
156 pp_base (pp)->padding = pp_none;
160 pp_c_complement (c_pretty_printer *pp)
163 pp_base (pp)->padding = pp_none;
167 pp_c_exclamation (c_pretty_printer *pp)
170 pp_base (pp)->padding = pp_none;
173 /* Print out the external representation of QUALIFIERS. */
176 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
178 const char *p = pp_last_position_in_text (pp);
179 bool previous = false;
184 /* The C programming language does not have references, but it is much
185 simpler to handle those here rather than going through the same
186 logic in the C++ pretty-printer. */
187 if (p != NULL && (*p == '*' || *p == '&'))
188 pp_c_whitespace (pp);
190 if (qualifiers & TYPE_QUAL_CONST)
192 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
196 if (qualifiers & TYPE_QUAL_VOLATILE)
199 pp_c_whitespace (pp);
200 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
204 if (qualifiers & TYPE_QUAL_RESTRICT)
207 pp_c_whitespace (pp);
208 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
209 ? "restrict" : "__restrict__"));
213 /* Pretty-print T using the type-cast notation '( type-name )'. */
216 pp_c_type_cast (c_pretty_printer *pp, tree t)
218 pp_c_left_paren (pp);
220 pp_c_right_paren (pp);
223 /* We're about to pretty-print a pointer type as indicated by T.
224 Output a whitespace, if needed, preparing for subsequent output. */
227 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
229 if (POINTER_TYPE_P (t))
231 tree pointee = strip_pointer_operator (TREE_TYPE (t));
232 if (TREE_CODE (pointee) != ARRAY_TYPE
233 && TREE_CODE (pointee) != FUNCTION_TYPE)
234 pp_c_whitespace (pp);
241 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
242 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
243 of its type. Take care of possible extensions.
247 type-qualifier-list type-qualifier
252 __restrict__ -- GNU C
253 address-space-qualifier -- GNU C
256 address-space-qualifier:
257 identifier -- GNU C */
260 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
264 if (!t || t == error_mark_node)
270 qualifiers = TYPE_QUALS (t);
271 pp_c_cv_qualifiers (pp, qualifiers,
272 TREE_CODE (t) == FUNCTION_TYPE);
274 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
276 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
277 pp_c_identifier (pp, as);
282 * type-qualifier-list(opt)
283 * type-qualifier-list(opt) pointer */
286 pp_c_pointer (c_pretty_printer *pp, tree t)
288 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
290 switch (TREE_CODE (t))
293 /* It is easier to handle C++ reference types here. */
295 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
296 pp_c_pointer (pp, TREE_TYPE (t));
297 if (TREE_CODE (t) == POINTER_TYPE)
301 pp_c_type_qualifier_list (pp, t);
304 /* ??? This node is now in GENERIC and so shouldn't be here. But
305 we'll fix that later. */
307 pp_declaration (pp, DECL_EXPR_DECL (t));
308 pp_needs_newline (pp) = true;
312 pp_unsupported_tree (pp, t);
329 struct-or-union-specifier
334 simple-type-specifier:
339 pp_c_type_specifier (c_pretty_printer *pp, tree t)
341 const enum tree_code code = TREE_CODE (t);
345 pp_c_ws_string (pp, M_("<type-error>"));
348 case IDENTIFIER_NODE:
349 pp_c_identifier (pp, IDENTIFIER_POINTER (t));
356 case FIXED_POINT_TYPE:
360 pp_c_type_specifier (pp, t);
364 int prec = TYPE_PRECISION (t);
365 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
366 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
368 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
371 pp_c_type_specifier (pp, t);
372 if (TYPE_PRECISION (t) != prec)
375 pp_decimal_int (pp, prec);
383 pp_string (pp, (TYPE_UNSIGNED (t)
384 ? M_("<unnamed-unsigned:")
385 : M_("<unnamed-signed:")));
388 pp_string (pp, M_("<unnamed-float:"));
390 case FIXED_POINT_TYPE:
391 pp_string (pp, M_("<unnamed-fixed:"));
396 pp_decimal_int (pp, prec);
404 pp_id_expression (pp, t);
406 pp_c_ws_string (pp, M_("<typedef-error>"));
412 if (code == UNION_TYPE)
413 pp_c_ws_string (pp, "union");
414 else if (code == RECORD_TYPE)
415 pp_c_ws_string (pp, "struct");
416 else if (code == ENUMERAL_TYPE)
417 pp_c_ws_string (pp, "enum");
419 pp_c_ws_string (pp, M_("<tag-error>"));
422 pp_id_expression (pp, TYPE_NAME (t));
424 pp_c_ws_string (pp, M_("<anonymous>"));
428 pp_unsupported_tree (pp, t);
433 /* specifier-qualifier-list:
434 type-specifier specifier-qualifier-list-opt
435 type-qualifier specifier-qualifier-list-opt
438 Implementation note: Because of the non-linearities in array or
439 function declarations, this routine prints not just the
440 specifier-qualifier-list of such entities or types of such entities,
441 but also the 'pointer' production part of their declarators. The
442 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
445 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
447 const enum tree_code code = TREE_CODE (t);
449 if (TREE_CODE (t) != POINTER_TYPE)
450 pp_c_type_qualifier_list (pp, t);
456 /* Get the types-specifier of this type. */
457 tree pointee = strip_pointer_operator (TREE_TYPE (t));
458 pp_c_specifier_qualifier_list (pp, pointee);
459 if (TREE_CODE (pointee) == ARRAY_TYPE
460 || TREE_CODE (pointee) == FUNCTION_TYPE)
462 pp_c_whitespace (pp);
463 pp_c_left_paren (pp);
464 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
466 else if (!c_dialect_cxx ())
467 pp_c_whitespace (pp);
468 pp_ptr_operator (pp, t);
474 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
479 if (code == COMPLEX_TYPE)
480 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
481 ? "_Complex" : "__complex__"));
482 else if (code == VECTOR_TYPE)
484 pp_c_ws_string (pp, "__vector");
485 pp_c_left_paren (pp);
486 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
487 pp_c_right_paren (pp);
488 pp_c_whitespace (pp);
490 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
494 pp_simple_type_specifier (pp, t);
499 /* parameter-type-list:
504 parameter-declaration
505 parameter-list , parameter-declaration
507 parameter-declaration:
508 declaration-specifiers declarator
509 declaration-specifiers abstract-declarator(opt) */
512 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
514 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
515 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
516 pp_c_left_paren (pp);
517 if (parms == void_list_node)
518 pp_c_ws_string (pp, "void");
522 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
525 pp_separate_with (pp, ',');
527 pp_declaration_specifiers
528 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
530 pp_declarator (pp, parms);
532 pp_abstract_declarator (pp, TREE_VALUE (parms));
535 pp_c_right_paren (pp);
538 /* abstract-declarator:
540 pointer(opt) direct-abstract-declarator */
543 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
545 if (TREE_CODE (t) == POINTER_TYPE)
547 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
548 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
549 pp_c_right_paren (pp);
553 pp_direct_abstract_declarator (pp, t);
556 /* direct-abstract-declarator:
557 ( abstract-declarator )
558 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
559 direct-abstract-declarator(opt) [ * ]
560 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
563 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
565 switch (TREE_CODE (t))
568 pp_abstract_declarator (pp, t);
572 pp_c_parameter_type_list (pp, t);
573 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
577 pp_c_left_bracket (pp);
578 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
580 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
581 tree type = TREE_TYPE (maxval);
583 if (host_integerp (maxval, 0))
584 pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
586 pp_expression (pp, fold_build2 (PLUS_EXPR, type, maxval,
587 build_int_cst (type, 1)));
589 pp_c_right_bracket (pp);
590 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
593 case IDENTIFIER_NODE:
598 case FIXED_POINT_TYPE:
608 pp_unsupported_tree (pp, t);
614 specifier-qualifier-list abstract-declarator(opt) */
617 pp_c_type_id (c_pretty_printer *pp, tree t)
619 pp_c_specifier_qualifier_list (pp, t);
620 pp_abstract_declarator (pp, t);
623 /* storage-class-specifier:
631 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
633 if (TREE_CODE (t) == TYPE_DECL)
634 pp_c_ws_string (pp, "typedef");
637 if (DECL_REGISTER (t))
638 pp_c_ws_string (pp, "register");
639 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
640 pp_c_ws_string (pp, "static");
644 /* function-specifier:
648 pp_c_function_specifier (c_pretty_printer *pp, tree t)
650 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
651 pp_c_ws_string (pp, "inline");
654 /* declaration-specifiers:
655 storage-class-specifier declaration-specifiers(opt)
656 type-specifier declaration-specifiers(opt)
657 type-qualifier declaration-specifiers(opt)
658 function-specifier declaration-specifiers(opt) */
661 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
663 pp_storage_class_specifier (pp, t);
664 pp_function_specifier (pp, t);
665 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
671 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
672 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
673 direct-declarator [ type-qualifier-list static assignment-expression ]
674 direct-declarator [ type-qualifier-list * ]
675 direct-declarator ( parameter-type-list )
676 direct-declarator ( identifier-list(opt) ) */
679 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
681 switch (TREE_CODE (t))
688 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
689 pp_c_tree_decl_identifier (pp, t);
694 pp_abstract_declarator (pp, TREE_TYPE (t));
698 pp_parameter_list (pp, t);
699 pp_abstract_declarator (pp, TREE_TYPE (t));
703 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
704 pp_c_tree_decl_identifier (pp, t);
705 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
706 pp_abstract_declarator (pp, TREE_TYPE (t));
709 pp_parameter_list (pp, t);
710 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
716 case FIXED_POINT_TYPE:
723 pp_unsupported_tree (pp, t);
730 pointer(opt) direct-declarator */
733 pp_c_declarator (c_pretty_printer *pp, tree t)
735 switch (TREE_CODE (t))
739 case FIXED_POINT_TYPE:
752 pp_direct_declarator (pp, t);
757 pp_unsupported_tree (pp, t);
763 declaration-specifiers init-declarator-list(opt) ; */
766 pp_c_declaration (c_pretty_printer *pp, tree t)
768 pp_declaration_specifiers (pp, t);
769 pp_c_init_declarator (pp, t);
772 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
775 pp_c_attributes (c_pretty_printer *pp, tree attributes)
777 if (attributes == NULL_TREE)
780 pp_c_ws_string (pp, "__attribute__");
781 pp_c_left_paren (pp);
782 pp_c_left_paren (pp);
783 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
785 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
786 if (TREE_VALUE (attributes))
787 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
789 if (TREE_CHAIN (attributes))
790 pp_separate_with (pp, ',');
792 pp_c_right_paren (pp);
793 pp_c_right_paren (pp);
796 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
797 marked to be displayed on disgnostic. */
800 pp_c_attributes_display (c_pretty_printer *pp, tree a)
802 bool is_first = true;
807 for (; a != NULL_TREE; a = TREE_CHAIN (a))
809 const struct attribute_spec *as;
810 as = lookup_attribute_spec (TREE_PURPOSE (a));
811 if (!as || as->affects_type_identity == false)
815 pp_c_ws_string (pp, "__attribute__");
816 pp_c_left_paren (pp);
817 pp_c_left_paren (pp);
822 pp_separate_with (pp, ',');
824 pp_tree_identifier (pp, TREE_PURPOSE (a));
826 pp_c_call_argument_list (pp, TREE_VALUE (a));
831 pp_c_right_paren (pp);
832 pp_c_right_paren (pp);
833 pp_c_whitespace (pp);
837 /* function-definition:
838 declaration-specifiers declarator compound-statement */
841 pp_c_function_definition (c_pretty_printer *pp, tree t)
843 pp_declaration_specifiers (pp, t);
844 pp_declarator (pp, t);
845 pp_needs_newline (pp) = true;
846 pp_statement (pp, DECL_SAVED_TREE (t));
854 /* Print out a c-char. This is called solely for characters which are
855 in the *target* execution character set. We ought to convert them
856 back to the *host* execution character set before printing, but we
857 have no way to do this at present. A decent compromise is to print
858 all characters as if they were in the host execution character set,
859 and not attempt to recover any named escape characters, but render
860 all unprintables as octal escapes. If the host and target character
861 sets are the same, this produces relatively readable output. If they
862 are not the same, strings may appear as gibberish, but that's okay
863 (in fact, it may well be what the reader wants, e.g. if they are looking
864 to see if conversion to the target character set happened correctly).
866 A special case: we need to prefix \, ", and ' with backslashes. It is
867 correct to do so for the *host*'s \, ", and ', because the rest of the
868 file appears in the host character set. */
871 pp_c_char (c_pretty_printer *pp, int c)
877 case '\\': pp_string (pp, "\\\\"); break;
878 case '\'': pp_string (pp, "\\\'"); break;
879 case '\"': pp_string (pp, "\\\""); break;
880 default: pp_character (pp, c);
884 pp_scalar (pp, "\\%03o", (unsigned) c);
887 /* Print out a STRING literal. */
890 pp_c_string_literal (c_pretty_printer *pp, tree s)
892 const char *p = TREE_STRING_POINTER (s);
893 int n = TREE_STRING_LENGTH (s) - 1;
896 for (i = 0; i < n; ++i)
897 pp_c_char (pp, p[i]);
901 /* Pretty-print an INTEGER literal. */
904 pp_c_integer_constant (c_pretty_printer *pp, tree i)
906 /* We are going to compare the type of I to other types using
907 pointer comparison so we need to use its canonical type. */
909 TYPE_CANONICAL (TREE_TYPE (i))
910 ? TYPE_CANONICAL (TREE_TYPE (i))
913 if (TREE_INT_CST_HIGH (i) == 0)
914 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
917 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
918 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
919 if (tree_int_cst_sgn (i) < 0)
921 pp_character (pp, '-');
925 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
926 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
927 pp_string (pp, pp_buffer (pp)->digit_buffer);
929 if (TYPE_UNSIGNED (type))
930 pp_character (pp, 'u');
931 if (type == long_integer_type_node || type == long_unsigned_type_node)
932 pp_character (pp, 'l');
933 else if (type == long_long_integer_type_node
934 || type == long_long_unsigned_type_node)
935 pp_string (pp, "ll");
936 else if (type == int128_integer_type_node
937 || type == int128_unsigned_type_node)
938 pp_string (pp, "I128");
941 /* Print out a CHARACTER literal. */
944 pp_c_character_constant (c_pretty_printer *pp, tree c)
946 tree type = TREE_TYPE (c);
947 if (type == wchar_type_node)
948 pp_character (pp, 'L');
950 if (host_integerp (c, TYPE_UNSIGNED (type)))
951 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
953 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
957 /* Print out a BOOLEAN literal. */
960 pp_c_bool_constant (c_pretty_printer *pp, tree b)
962 if (b == boolean_false_node)
964 if (c_dialect_cxx ())
965 pp_c_ws_string (pp, "false");
966 else if (flag_isoc99)
967 pp_c_ws_string (pp, "_False");
969 pp_unsupported_tree (pp, b);
971 else if (b == boolean_true_node)
973 if (c_dialect_cxx ())
974 pp_c_ws_string (pp, "true");
975 else if (flag_isoc99)
976 pp_c_ws_string (pp, "_True");
978 pp_unsupported_tree (pp, b);
980 else if (TREE_CODE (b) == INTEGER_CST)
981 pp_c_integer_constant (pp, b);
983 pp_unsupported_tree (pp, b);
986 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
987 false; that means the value was obtained by a cast, in which case
988 print out the type-id part of the cast-expression -- the casted value
989 is then printed by pp_c_integer_literal. */
992 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
994 bool value_is_named = true;
995 tree type = TREE_TYPE (e);
998 /* Find the name of this constant. */
999 for (value = TYPE_VALUES (type);
1000 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1001 value = TREE_CHAIN (value))
1004 if (value != NULL_TREE)
1005 pp_id_expression (pp, TREE_PURPOSE (value));
1008 /* Value must have been cast. */
1009 pp_c_type_cast (pp, type);
1010 value_is_named = false;
1013 return value_is_named;
1016 /* Print out a REAL value as a decimal-floating-constant. */
1019 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1021 const struct real_format *fmt
1022 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1024 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1025 bool is_decimal = floating_cst.decimal;
1027 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1028 log10(2) to 7 significant digits. */
1029 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1031 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1032 sizeof (pp_buffer (pp)->digit_buffer),
1035 pp_string (pp, pp_buffer(pp)->digit_buffer);
1036 if (TREE_TYPE (r) == float_type_node)
1037 pp_character (pp, 'f');
1038 else if (TREE_TYPE (r) == long_double_type_node)
1039 pp_character (pp, 'l');
1040 else if (TREE_TYPE (r) == dfloat128_type_node)
1041 pp_string (pp, "dl");
1042 else if (TREE_TYPE (r) == dfloat64_type_node)
1043 pp_string (pp, "dd");
1044 else if (TREE_TYPE (r) == dfloat32_type_node)
1045 pp_string (pp, "df");
1048 /* Print out a FIXED value as a decimal-floating-constant. */
1051 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1053 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1054 sizeof (pp_buffer (pp)->digit_buffer));
1055 pp_string (pp, pp_buffer(pp)->digit_buffer);
1058 /* Pretty-print a compound literal expression. GNU extensions include
1059 vector constants. */
1062 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1064 tree type = TREE_TYPE (e);
1065 pp_c_type_cast (pp, type);
1067 switch (TREE_CODE (type))
1074 pp_c_brace_enclosed_initializer_list (pp, e);
1078 pp_unsupported_tree (pp, e);
1083 /* Pretty-print a COMPLEX_EXPR expression. */
1086 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1088 /* Handle a few common special cases, otherwise fallback
1089 to printing it as compound literal. */
1090 tree type = TREE_TYPE (e);
1091 tree realexpr = TREE_OPERAND (e, 0);
1092 tree imagexpr = TREE_OPERAND (e, 1);
1094 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1095 if (TREE_CODE (realexpr) == NOP_EXPR
1096 && TREE_CODE (imagexpr) == NOP_EXPR
1097 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1098 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1099 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1100 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1101 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1102 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1104 pp_c_type_cast (pp, type);
1105 pp_expression (pp, TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1109 /* Cast of an scalar expression to COMPLEX_TYPE. */
1110 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1111 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1113 pp_c_type_cast (pp, type);
1114 if (TREE_CODE (realexpr) == NOP_EXPR)
1115 realexpr = TREE_OPERAND (realexpr, 0);
1116 pp_expression (pp, realexpr);
1120 pp_c_compound_literal (pp, e);
1126 fixed-point-constant
1127 enumeration-constant
1128 character-constant */
1131 pp_c_constant (c_pretty_printer *pp, tree e)
1133 const enum tree_code code = TREE_CODE (e);
1139 tree type = TREE_TYPE (e);
1140 if (type == boolean_type_node)
1141 pp_c_bool_constant (pp, e);
1142 else if (type == char_type_node)
1143 pp_c_character_constant (pp, e);
1144 else if (TREE_CODE (type) == ENUMERAL_TYPE
1145 && pp_c_enumeration_constant (pp, e))
1148 pp_c_integer_constant (pp, e);
1153 pp_c_floating_constant (pp, e);
1157 pp_c_fixed_constant (pp, e);
1161 pp_c_string_literal (pp, e);
1165 /* Sometimes, we are confused and we think a complex literal
1166 is a constant. Such thing is a compound literal which
1167 grammatically belongs to postfix-expr production. */
1168 pp_c_compound_literal (pp, e);
1172 pp_unsupported_tree (pp, e);
1177 /* Pretty-print a string such as an identifier, without changing its
1178 encoding, preceded by whitespace is necessary. */
1181 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1183 pp_c_maybe_whitespace (pp);
1184 pp_string (pp, str);
1185 pp_base (pp)->padding = pp_before;
1188 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1189 that need converting to the locale encoding, preceded by whitespace
1193 pp_c_identifier (c_pretty_printer *pp, const char *id)
1195 pp_c_maybe_whitespace (pp);
1196 pp_identifier (pp, id);
1197 pp_base (pp)->padding = pp_before;
1200 /* Pretty-print a C primary-expression.
1208 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1210 switch (TREE_CODE (e))
1218 pp_c_tree_decl_identifier (pp, e);
1221 case IDENTIFIER_NODE:
1222 pp_c_tree_identifier (pp, e);
1226 pp_c_ws_string (pp, M_("<erroneous-expression>"));
1230 pp_c_ws_string (pp, M_("<return-value>"));
1237 pp_c_constant (pp, e);
1241 pp_c_ws_string (pp, "__builtin_memcpy");
1242 pp_c_left_paren (pp);
1244 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1245 pp_separate_with (pp, ',');
1247 pp_initializer (pp, TREE_OPERAND (e, 1));
1248 if (TREE_OPERAND (e, 2))
1250 pp_separate_with (pp, ',');
1251 pp_c_expression (pp, TREE_OPERAND (e, 2));
1253 pp_c_right_paren (pp);
1257 /* FIXME: Make sure we won't get into an infinite loop. */
1258 pp_c_left_paren (pp);
1259 pp_expression (pp, e);
1260 pp_c_right_paren (pp);
1265 /* Print out a C initializer -- also support C compound-literals.
1267 assignment-expression:
1268 { initializer-list }
1269 { initializer-list , } */
1272 pp_c_initializer (c_pretty_printer *pp, tree e)
1274 if (TREE_CODE (e) == CONSTRUCTOR)
1275 pp_c_brace_enclosed_initializer_list (pp, e);
1277 pp_expression (pp, e);
1282 declarator = initializer */
1285 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1287 pp_declarator (pp, t);
1288 /* We don't want to output function definitions here. There are handled
1289 elsewhere (and the syntactic form is bogus anyway). */
1290 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1292 tree init = DECL_INITIAL (t);
1293 /* This C++ bit is handled here because it is easier to do so.
1294 In templates, the C++ parser builds a TREE_LIST for a
1295 direct-initialization; the TREE_PURPOSE is the variable to
1296 initialize and the TREE_VALUE is the initializer. */
1297 if (TREE_CODE (init) == TREE_LIST)
1299 pp_c_left_paren (pp);
1300 pp_expression (pp, TREE_VALUE (init));
1301 pp_right_paren (pp);
1308 pp_c_initializer (pp, init);
1313 /* initializer-list:
1314 designation(opt) initializer
1315 initializer-list , designation(opt) initializer
1322 designator-list designator
1325 [ constant-expression ]
1329 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1331 tree type = TREE_TYPE (e);
1332 const enum tree_code code = TREE_CODE (type);
1334 if (TREE_CODE (e) == CONSTRUCTOR)
1336 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1346 tree init = TREE_OPERAND (e, 0);
1347 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1349 if (code == RECORD_TYPE || code == UNION_TYPE)
1352 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1356 pp_c_left_bracket (pp);
1357 if (TREE_PURPOSE (init))
1358 pp_c_constant (pp, TREE_PURPOSE (init));
1359 pp_c_right_bracket (pp);
1361 pp_c_whitespace (pp);
1363 pp_c_whitespace (pp);
1364 pp_initializer (pp, TREE_VALUE (init));
1365 if (TREE_CHAIN (init))
1366 pp_separate_with (pp, ',');
1372 if (TREE_CODE (e) == VECTOR_CST)
1373 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1379 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1381 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1382 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1383 pp_separate_with (pp, ',');
1384 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1394 pp_unsupported_tree (pp, type);
1397 /* Pretty-print a brace-enclosed initializer-list. */
1400 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1402 pp_c_left_brace (pp);
1403 pp_c_initializer_list (pp, l);
1404 pp_c_right_brace (pp);
1408 /* This is a convenient function, used to bridge gap between C and C++
1415 pp_c_id_expression (c_pretty_printer *pp, tree t)
1417 switch (TREE_CODE (t))
1426 pp_c_tree_decl_identifier (pp, t);
1429 case IDENTIFIER_NODE:
1430 pp_c_tree_identifier (pp, t);
1434 pp_unsupported_tree (pp, t);
1439 /* postfix-expression:
1441 postfix-expression [ expression ]
1442 postfix-expression ( argument-expression-list(opt) )
1443 postfix-expression . identifier
1444 postfix-expression -> identifier
1445 postfix-expression ++
1446 postfix-expression --
1447 ( type-name ) { initializer-list }
1448 ( type-name ) { initializer-list , } */
1451 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1453 enum tree_code code = TREE_CODE (e);
1456 case POSTINCREMENT_EXPR:
1457 case POSTDECREMENT_EXPR:
1458 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1459 pp_string (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1463 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1464 pp_c_left_bracket (pp);
1465 pp_expression (pp, TREE_OPERAND (e, 1));
1466 pp_c_right_bracket (pp);
1471 call_expr_arg_iterator iter;
1473 pp_postfix_expression (pp, CALL_EXPR_FN (e));
1474 pp_c_left_paren (pp);
1475 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1477 pp_expression (pp, arg);
1478 if (more_call_expr_args_p (&iter))
1479 pp_separate_with (pp, ',');
1481 pp_c_right_paren (pp);
1485 case UNORDERED_EXPR:
1486 pp_c_ws_string (pp, flag_isoc99
1488 : "__builtin_isunordered");
1492 pp_c_ws_string (pp, flag_isoc99
1494 : "!__builtin_isunordered");
1498 pp_c_ws_string (pp, flag_isoc99
1500 : "!__builtin_isgreaterequal");
1504 pp_c_ws_string (pp, flag_isoc99
1506 : "!__builtin_isgreater");
1510 pp_c_ws_string (pp, flag_isoc99
1512 : "!__builtin_islessequal");
1516 pp_c_ws_string (pp, flag_isoc99
1518 : "!__builtin_isless");
1522 pp_c_ws_string (pp, flag_isoc99
1524 : "!__builtin_islessgreater");
1528 pp_c_ws_string (pp, flag_isoc99
1530 : "__builtin_islessgreater");
1534 pp_c_left_paren (pp);
1535 pp_expression (pp, TREE_OPERAND (e, 0));
1536 pp_separate_with (pp, ',');
1537 pp_expression (pp, TREE_OPERAND (e, 1));
1538 pp_c_right_paren (pp);
1542 pp_c_ws_string (pp, "__builtin_abs");
1543 pp_c_left_paren (pp);
1544 pp_expression (pp, TREE_OPERAND (e, 0));
1545 pp_c_right_paren (pp);
1550 tree object = TREE_OPERAND (e, 0);
1551 if (TREE_CODE (object) == INDIRECT_REF)
1553 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1558 pp_postfix_expression (pp, object);
1561 pp_expression (pp, TREE_OPERAND (e, 1));
1567 tree type = TREE_TYPE (e);
1569 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1571 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1573 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1574 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1575 if ((bitpos % size) == 0)
1577 pp_c_left_paren (pp);
1578 pp_c_left_paren (pp);
1579 pp_type_id (pp, type);
1581 pp_c_right_paren (pp);
1582 pp_c_ampersand (pp);
1583 pp_expression (pp, TREE_OPERAND (e, 0));
1584 pp_c_right_paren (pp);
1585 pp_c_left_bracket (pp);
1586 pp_wide_integer (pp, bitpos / size);
1587 pp_c_right_bracket (pp);
1591 pp_unsupported_tree (pp, e);
1596 pp_c_expression (pp, e);
1601 pp_c_compound_literal (pp, e);
1605 pp_c_complex_expr (pp, e);
1608 case COMPOUND_LITERAL_EXPR:
1609 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1612 pp_initializer (pp, e);
1616 pp_c_ws_string (pp, "__builtin_va_arg");
1617 pp_c_left_paren (pp);
1618 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1619 pp_separate_with (pp, ',');
1620 pp_type_id (pp, TREE_TYPE (e));
1621 pp_c_right_paren (pp);
1625 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1627 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1630 /* else fall through. */
1633 pp_primary_expression (pp, e);
1638 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1641 pp_c_expression_list (c_pretty_printer *pp, tree e)
1643 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1645 pp_expression (pp, TREE_VALUE (e));
1647 pp_separate_with (pp, ',');
1651 /* Print out V, which contains the elements of a constructor. */
1654 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1656 unsigned HOST_WIDE_INT ix;
1659 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1661 pp_expression (pp, value);
1662 if (ix != VEC_length (constructor_elt, v) - 1)
1663 pp_separate_with (pp, ',');
1667 /* Print out an expression-list in parens, as if it were the argument
1668 list to a function. */
1671 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1673 pp_c_left_paren (pp);
1674 if (t && TREE_CODE (t) == TREE_LIST)
1675 pp_c_expression_list (pp, t);
1676 pp_c_right_paren (pp);
1679 /* unary-expression:
1683 unary-operator cast-expression
1684 sizeof unary-expression
1687 unary-operator: one of
1692 __alignof__ unary-expression
1693 __alignof__ ( type-id )
1694 __real__ unary-expression
1695 __imag__ unary-expression */
1698 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1700 enum tree_code code = TREE_CODE (e);
1703 case PREINCREMENT_EXPR:
1704 case PREDECREMENT_EXPR:
1705 pp_string (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1706 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1713 case TRUTH_NOT_EXPR:
1715 /* String literal are used by address. */
1716 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1718 else if (code == INDIRECT_REF)
1720 else if (code == NEGATE_EXPR)
1722 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1724 else if (code == TRUTH_NOT_EXPR)
1725 pp_exclamation (pp);
1726 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1730 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1731 && integer_zerop (TREE_OPERAND (e, 1)))
1732 pp_c_expression (pp, TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1736 if (!integer_zerop (TREE_OPERAND (e, 1)))
1738 pp_c_left_paren (pp);
1739 if (!integer_onep (TYPE_SIZE_UNIT
1740 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1741 pp_c_type_cast (pp, ptr_type_node);
1743 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1744 if (!integer_zerop (TREE_OPERAND (e, 1)))
1747 pp_c_integer_constant (pp,
1748 fold_convert (ssizetype,
1749 TREE_OPERAND (e, 1)));
1750 pp_c_right_paren (pp);
1757 pp_c_ws_string (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1758 pp_c_whitespace (pp);
1759 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1763 pp_postfix_expression (pp, e);
1770 ( type-name ) cast-expression */
1773 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1775 switch (TREE_CODE (e))
1778 case FIX_TRUNC_EXPR:
1780 case VIEW_CONVERT_EXPR:
1781 pp_c_type_cast (pp, TREE_TYPE (e));
1782 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1786 pp_unary_expression (pp, e);
1790 /* multiplicative-expression:
1792 multiplicative-expression * cast-expression
1793 multiplicative-expression / cast-expression
1794 multiplicative-expression % cast-expression */
1797 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1799 enum tree_code code = TREE_CODE (e);
1803 case TRUNC_DIV_EXPR:
1804 case TRUNC_MOD_EXPR:
1805 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1806 pp_c_whitespace (pp);
1807 if (code == MULT_EXPR)
1809 else if (code == TRUNC_DIV_EXPR)
1813 pp_c_whitespace (pp);
1814 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1818 pp_c_cast_expression (pp, e);
1823 /* additive-expression:
1824 multiplicative-expression
1825 additive-expression + multiplicative-expression
1826 additive-expression - multiplicative-expression */
1829 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1831 enum tree_code code = TREE_CODE (e);
1834 case POINTER_PLUS_EXPR:
1837 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1838 pp_c_whitespace (pp);
1839 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1843 pp_c_whitespace (pp);
1844 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1848 pp_multiplicative_expression (pp, e);
1853 /* additive-expression:
1855 shift-expression << additive-expression
1856 shift-expression >> additive-expression */
1859 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1861 enum tree_code code = TREE_CODE (e);
1866 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1867 pp_c_whitespace (pp);
1868 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1869 pp_c_whitespace (pp);
1870 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1874 pp_c_additive_expression (pp, e);
1878 /* relational-expression:
1880 relational-expression < shift-expression
1881 relational-expression > shift-expression
1882 relational-expression <= shift-expression
1883 relational-expression >= shift-expression */
1886 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1888 enum tree_code code = TREE_CODE (e);
1895 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1896 pp_c_whitespace (pp);
1897 if (code == LT_EXPR)
1899 else if (code == GT_EXPR)
1901 else if (code == LE_EXPR)
1902 pp_string (pp, "<=");
1903 else if (code == GE_EXPR)
1904 pp_string (pp, ">=");
1905 pp_c_whitespace (pp);
1906 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1910 pp_c_shift_expression (pp, e);
1915 /* equality-expression:
1916 relational-expression
1917 equality-expression == relational-expression
1918 equality-equality != relational-expression */
1921 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1923 enum tree_code code = TREE_CODE (e);
1928 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1929 pp_c_whitespace (pp);
1930 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1931 pp_c_whitespace (pp);
1932 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1936 pp_c_relational_expression (pp, e);
1943 AND-expression & equality-equality */
1946 pp_c_and_expression (c_pretty_printer *pp, tree e)
1948 if (TREE_CODE (e) == BIT_AND_EXPR)
1950 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1951 pp_c_whitespace (pp);
1953 pp_c_whitespace (pp);
1954 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1957 pp_c_equality_expression (pp, e);
1960 /* exclusive-OR-expression:
1962 exclusive-OR-expression ^ AND-expression */
1965 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1967 if (TREE_CODE (e) == BIT_XOR_EXPR
1968 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1970 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1971 if (TREE_CODE (e) == BIT_XOR_EXPR)
1972 pp_c_maybe_whitespace (pp);
1974 pp_c_whitespace (pp);
1976 pp_c_whitespace (pp);
1977 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1980 pp_c_and_expression (pp, e);
1983 /* inclusive-OR-expression:
1984 exclusive-OR-expression
1985 inclusive-OR-expression | exclusive-OR-expression */
1988 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1990 if (TREE_CODE (e) == BIT_IOR_EXPR)
1992 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1993 pp_c_whitespace (pp);
1995 pp_c_whitespace (pp);
1996 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1999 pp_c_exclusive_or_expression (pp, e);
2002 /* logical-AND-expression:
2003 inclusive-OR-expression
2004 logical-AND-expression && inclusive-OR-expression */
2007 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2009 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2010 || TREE_CODE (e) == TRUTH_AND_EXPR)
2012 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2013 pp_c_whitespace (pp);
2014 pp_string (pp, "&&");
2015 pp_c_whitespace (pp);
2016 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2019 pp_c_inclusive_or_expression (pp, e);
2022 /* logical-OR-expression:
2023 logical-AND-expression
2024 logical-OR-expression || logical-AND-expression */
2027 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2029 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2030 || TREE_CODE (e) == TRUTH_OR_EXPR)
2032 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2033 pp_c_whitespace (pp);
2034 pp_string (pp, "||");
2035 pp_c_whitespace (pp);
2036 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2039 pp_c_logical_and_expression (pp, e);
2042 /* conditional-expression:
2043 logical-OR-expression
2044 logical-OR-expression ? expression : conditional-expression */
2047 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
2049 if (TREE_CODE (e) == COND_EXPR)
2051 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2052 pp_c_whitespace (pp);
2054 pp_c_whitespace (pp);
2055 pp_expression (pp, TREE_OPERAND (e, 1));
2056 pp_c_whitespace (pp);
2058 pp_c_whitespace (pp);
2059 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
2062 pp_c_logical_or_expression (pp, e);
2066 /* assignment-expression:
2067 conditional-expression
2068 unary-expression assignment-operator assignment-expression
2070 assignment-expression: one of
2071 = *= /= %= += -= >>= <<= &= ^= |= */
2074 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
2076 if (TREE_CODE (e) == MODIFY_EXPR
2077 || TREE_CODE (e) == INIT_EXPR)
2079 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
2080 pp_c_whitespace (pp);
2083 pp_c_expression (pp, TREE_OPERAND (e, 1));
2086 pp_c_conditional_expression (pp, e);
2090 assignment-expression
2091 expression , assignment-expression
2093 Implementation note: instead of going through the usual recursion
2094 chain, I take the liberty of dispatching nodes to the appropriate
2095 functions. This makes some redundancy, but it worths it. That also
2096 prevents a possible infinite recursion between pp_c_primary_expression ()
2097 and pp_c_expression (). */
2100 pp_c_expression (c_pretty_printer *pp, tree e)
2102 switch (TREE_CODE (e))
2105 pp_c_integer_constant (pp, e);
2109 pp_c_floating_constant (pp, e);
2113 pp_c_fixed_constant (pp, e);
2117 pp_c_string_literal (pp, e);
2120 case IDENTIFIER_NODE:
2129 pp_primary_expression (pp, e);
2133 if (!DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2134 pp_c_expression (pp, SSA_NAME_VAR (e));
2136 pp_c_ws_string (pp, M_("<unknown>"));
2139 case POSTINCREMENT_EXPR:
2140 case POSTDECREMENT_EXPR:
2149 case UNORDERED_EXPR:
2158 case COMPOUND_LITERAL_EXPR:
2160 pp_postfix_expression (pp, e);
2169 case TRUTH_NOT_EXPR:
2170 case PREINCREMENT_EXPR:
2171 case PREDECREMENT_EXPR:
2174 pp_c_unary_expression (pp, e);
2178 case FIX_TRUNC_EXPR:
2180 case VIEW_CONVERT_EXPR:
2181 pp_c_cast_expression (pp, e);
2185 case TRUNC_MOD_EXPR:
2186 case TRUNC_DIV_EXPR:
2187 pp_multiplicative_expression (pp, e);
2192 pp_c_shift_expression (pp, e);
2199 pp_c_relational_expression (pp, e);
2203 pp_c_and_expression (pp, e);
2207 case TRUTH_XOR_EXPR:
2208 pp_c_exclusive_or_expression (pp, e);
2212 pp_c_inclusive_or_expression (pp, e);
2215 case TRUTH_ANDIF_EXPR:
2216 case TRUTH_AND_EXPR:
2217 pp_c_logical_and_expression (pp, e);
2220 case TRUTH_ORIF_EXPR:
2222 pp_c_logical_or_expression (pp, e);
2227 pp_c_equality_expression (pp, e);
2231 pp_conditional_expression (pp, e);
2234 case POINTER_PLUS_EXPR:
2237 pp_c_additive_expression (pp, e);
2242 pp_assignment_expression (pp, e);
2246 pp_c_left_paren (pp);
2247 pp_expression (pp, TREE_OPERAND (e, 0));
2248 pp_separate_with (pp, ',');
2249 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
2250 pp_c_right_paren (pp);
2253 case NON_LVALUE_EXPR:
2255 pp_expression (pp, TREE_OPERAND (e, 0));
2259 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
2264 /* We don't yet have a way of dumping statements in a
2265 human-readable format. */
2266 pp_string (pp, "({...})");
2269 case C_MAYBE_CONST_EXPR:
2270 pp_c_expression (pp, C_MAYBE_CONST_EXPR_EXPR (e));
2274 pp_unsupported_tree (pp, e);
2284 pp_c_statement (c_pretty_printer *pp, tree stmt)
2289 if (pp_needs_newline (pp))
2290 pp_newline_and_indent (pp, 0);
2292 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2296 /* Initialize the PRETTY-PRINTER for handling C codes. */
2299 pp_c_pretty_printer_init (c_pretty_printer *pp)
2301 pp->offset_list = 0;
2303 pp->declaration = pp_c_declaration;
2304 pp->declaration_specifiers = pp_c_declaration_specifiers;
2305 pp->declarator = pp_c_declarator;
2306 pp->direct_declarator = pp_c_direct_declarator;
2307 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2308 pp->abstract_declarator = pp_c_abstract_declarator;
2309 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2310 pp->ptr_operator = pp_c_pointer;
2311 pp->parameter_list = pp_c_parameter_type_list;
2312 pp->type_id = pp_c_type_id;
2313 pp->simple_type_specifier = pp_c_type_specifier;
2314 pp->function_specifier = pp_c_function_specifier;
2315 pp->storage_class_specifier = pp_c_storage_class_specifier;
2317 pp->statement = pp_c_statement;
2319 pp->constant = pp_c_constant;
2320 pp->id_expression = pp_c_id_expression;
2321 pp->primary_expression = pp_c_primary_expression;
2322 pp->postfix_expression = pp_c_postfix_expression;
2323 pp->unary_expression = pp_c_unary_expression;
2324 pp->initializer = pp_c_initializer;
2325 pp->multiplicative_expression = pp_c_multiplicative_expression;
2326 pp->conditional_expression = pp_c_conditional_expression;
2327 pp->assignment_expression = pp_c_assignment_expression;
2328 pp->expression = pp_c_expression;
2332 /* Print the tree T in full, on file FILE. */
2335 print_c_tree (FILE *file, tree t)
2337 static c_pretty_printer pp_rec;
2338 static bool initialized = 0;
2339 c_pretty_printer *pp = &pp_rec;
2344 pp_construct (pp_base (pp), NULL, 0);
2345 pp_c_pretty_printer_init (pp);
2346 pp_needs_newline (pp) = true;
2348 pp_base (pp)->buffer->stream = file;
2350 pp_statement (pp, t);
2356 /* Print the tree T in full, on stderr. */
2359 debug_c_tree (tree t)
2361 print_c_tree (stderr, t);
2362 fputc ('\n', stderr);
2365 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2366 up of T's memory address. */
2369 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2373 gcc_assert (DECL_P (t));
2376 name = IDENTIFIER_POINTER (DECL_NAME (t));
2379 static char xname[8];
2380 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2384 pp_c_identifier (pp, name);