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 ? "restrict" : "__restrict__");
212 /* Pretty-print T using the type-cast notation '( type-name )'. */
215 pp_c_type_cast (c_pretty_printer *pp, tree t)
217 pp_c_left_paren (pp);
219 pp_c_right_paren (pp);
222 /* We're about to pretty-print a pointer type as indicated by T.
223 Output a whitespace, if needed, preparing for subsequent output. */
226 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
228 if (POINTER_TYPE_P (t))
230 tree pointee = strip_pointer_operator (TREE_TYPE (t));
231 if (TREE_CODE (pointee) != ARRAY_TYPE
232 && TREE_CODE (pointee) != FUNCTION_TYPE)
233 pp_c_whitespace (pp);
240 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
241 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
242 of its type. Take care of possible extensions.
246 type-qualifier-list type-qualifier
251 __restrict__ -- GNU C
252 address-space-qualifier -- GNU C
255 address-space-qualifier:
256 identifier -- GNU C */
259 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
263 if (!t || t == error_mark_node)
269 qualifiers = TYPE_QUALS (t);
270 pp_c_cv_qualifiers (pp, qualifiers,
271 TREE_CODE (t) == FUNCTION_TYPE);
273 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
275 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
276 pp_c_identifier (pp, as);
281 * type-qualifier-list(opt)
282 * type-qualifier-list(opt) pointer */
285 pp_c_pointer (c_pretty_printer *pp, tree t)
287 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
289 switch (TREE_CODE (t))
292 /* It is easier to handle C++ reference types here. */
294 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
295 pp_c_pointer (pp, TREE_TYPE (t));
296 if (TREE_CODE (t) == POINTER_TYPE)
300 pp_c_type_qualifier_list (pp, t);
303 /* ??? This node is now in GENERIC and so shouldn't be here. But
304 we'll fix that later. */
306 pp_declaration (pp, DECL_EXPR_DECL (t));
307 pp_needs_newline (pp) = true;
311 pp_unsupported_tree (pp, t);
328 struct-or-union-specifier
333 simple-type-specifier:
338 pp_c_type_specifier (c_pretty_printer *pp, tree t)
340 const enum tree_code code = TREE_CODE (t);
344 pp_c_ws_string (pp, M_("<type-error>"));
347 case IDENTIFIER_NODE:
348 pp_c_tree_decl_identifier (pp, t);
355 case FIXED_POINT_TYPE:
359 pp_c_type_specifier (pp, t);
363 int prec = TYPE_PRECISION (t);
364 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
365 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
367 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
370 pp_c_type_specifier (pp, t);
371 if (TYPE_PRECISION (t) != prec)
374 pp_decimal_int (pp, prec);
382 pp_string (pp, (TYPE_UNSIGNED (t)
383 ? M_("<unnamed-unsigned:")
384 : M_("<unnamed-signed:")));
387 pp_string (pp, M_("<unnamed-float:"));
389 case FIXED_POINT_TYPE:
390 pp_string (pp, M_("<unnamed-fixed:"));
395 pp_decimal_int (pp, prec);
403 pp_id_expression (pp, t);
405 pp_c_ws_string (pp, M_("<typedef-error>"));
411 if (code == UNION_TYPE)
412 pp_c_ws_string (pp, "union");
413 else if (code == RECORD_TYPE)
414 pp_c_ws_string (pp, "struct");
415 else if (code == ENUMERAL_TYPE)
416 pp_c_ws_string (pp, "enum");
418 pp_c_ws_string (pp, M_("<tag-error>"));
421 pp_id_expression (pp, TYPE_NAME (t));
423 pp_c_ws_string (pp, M_("<anonymous>"));
427 pp_unsupported_tree (pp, t);
432 /* specifier-qualifier-list:
433 type-specifier specifier-qualifier-list-opt
434 type-qualifier specifier-qualifier-list-opt
437 Implementation note: Because of the non-linearities in array or
438 function declarations, this routine prints not just the
439 specifier-qualifier-list of such entities or types of such entities,
440 but also the 'pointer' production part of their declarators. The
441 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
444 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
446 const enum tree_code code = TREE_CODE (t);
448 if (TREE_CODE (t) != POINTER_TYPE)
449 pp_c_type_qualifier_list (pp, t);
455 /* Get the types-specifier of this type. */
456 tree pointee = strip_pointer_operator (TREE_TYPE (t));
457 pp_c_specifier_qualifier_list (pp, pointee);
458 if (TREE_CODE (pointee) == ARRAY_TYPE
459 || TREE_CODE (pointee) == FUNCTION_TYPE)
461 pp_c_whitespace (pp);
462 pp_c_left_paren (pp);
463 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
465 else if (!c_dialect_cxx ())
466 pp_c_whitespace (pp);
467 pp_ptr_operator (pp, t);
473 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
478 if (code == COMPLEX_TYPE)
479 pp_c_ws_string (pp, flag_isoc99 ? "_Complex" : "__complex__");
480 else if (code == VECTOR_TYPE)
482 pp_c_ws_string (pp, "__vector");
483 pp_c_left_paren (pp);
484 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
485 pp_c_right_paren (pp);
486 pp_c_whitespace (pp);
488 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
492 pp_simple_type_specifier (pp, t);
497 /* parameter-type-list:
502 parameter-declaration
503 parameter-list , parameter-declaration
505 parameter-declaration:
506 declaration-specifiers declarator
507 declaration-specifiers abstract-declarator(opt) */
510 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
512 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
513 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
514 pp_c_left_paren (pp);
515 if (parms == void_list_node)
516 pp_c_ws_string (pp, "void");
520 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
523 pp_separate_with (pp, ',');
525 pp_declaration_specifiers
526 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
528 pp_declarator (pp, parms);
530 pp_abstract_declarator (pp, TREE_VALUE (parms));
533 pp_c_right_paren (pp);
536 /* abstract-declarator:
538 pointer(opt) direct-abstract-declarator */
541 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
543 if (TREE_CODE (t) == POINTER_TYPE)
545 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
546 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
547 pp_c_right_paren (pp);
551 pp_direct_abstract_declarator (pp, t);
554 /* direct-abstract-declarator:
555 ( abstract-declarator )
556 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
557 direct-abstract-declarator(opt) [ * ]
558 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
561 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
563 switch (TREE_CODE (t))
566 pp_abstract_declarator (pp, t);
570 pp_c_parameter_type_list (pp, t);
571 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
575 pp_c_left_bracket (pp);
576 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
578 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
579 tree type = TREE_TYPE (maxval);
581 if (host_integerp (maxval, 0))
582 pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
584 pp_expression (pp, fold_build2 (PLUS_EXPR, type, maxval,
585 build_int_cst (type, 1)));
587 pp_c_right_bracket (pp);
588 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
591 case IDENTIFIER_NODE:
596 case FIXED_POINT_TYPE:
606 pp_unsupported_tree (pp, t);
612 specifier-qualifier-list abstract-declarator(opt) */
615 pp_c_type_id (c_pretty_printer *pp, tree t)
617 pp_c_specifier_qualifier_list (pp, t);
618 pp_abstract_declarator (pp, t);
621 /* storage-class-specifier:
629 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
631 if (TREE_CODE (t) == TYPE_DECL)
632 pp_c_ws_string (pp, "typedef");
635 if (DECL_REGISTER (t))
636 pp_c_ws_string (pp, "register");
637 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
638 pp_c_ws_string (pp, "static");
642 /* function-specifier:
646 pp_c_function_specifier (c_pretty_printer *pp, tree t)
648 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
649 pp_c_ws_string (pp, "inline");
652 /* declaration-specifiers:
653 storage-class-specifier declaration-specifiers(opt)
654 type-specifier declaration-specifiers(opt)
655 type-qualifier declaration-specifiers(opt)
656 function-specifier declaration-specifiers(opt) */
659 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
661 pp_storage_class_specifier (pp, t);
662 pp_function_specifier (pp, t);
663 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
669 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
670 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
671 direct-declarator [ type-qualifier-list static assignment-expression ]
672 direct-declarator [ type-qualifier-list * ]
673 direct-declarator ( parameter-type-list )
674 direct-declarator ( identifier-list(opt) ) */
677 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
679 switch (TREE_CODE (t))
686 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
687 pp_c_tree_decl_identifier (pp, t);
692 pp_abstract_declarator (pp, TREE_TYPE (t));
696 pp_parameter_list (pp, t);
697 pp_abstract_declarator (pp, TREE_TYPE (t));
701 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
702 pp_c_tree_decl_identifier (pp, t);
703 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
704 pp_abstract_declarator (pp, TREE_TYPE (t));
707 pp_parameter_list (pp, t);
708 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
714 case FIXED_POINT_TYPE:
721 pp_unsupported_tree (pp, t);
728 pointer(opt) direct-declarator */
731 pp_c_declarator (c_pretty_printer *pp, tree t)
733 switch (TREE_CODE (t))
737 case FIXED_POINT_TYPE:
750 pp_direct_declarator (pp, t);
755 pp_unsupported_tree (pp, t);
761 declaration-specifiers init-declarator-list(opt) ; */
764 pp_c_declaration (c_pretty_printer *pp, tree t)
766 pp_declaration_specifiers (pp, t);
767 pp_c_init_declarator (pp, t);
770 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
773 pp_c_attributes (c_pretty_printer *pp, tree attributes)
775 if (attributes == NULL_TREE)
778 pp_c_ws_string (pp, "__attribute__");
779 pp_c_left_paren (pp);
780 pp_c_left_paren (pp);
781 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
783 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
784 if (TREE_VALUE (attributes))
785 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
787 if (TREE_CHAIN (attributes))
788 pp_separate_with (pp, ',');
790 pp_c_right_paren (pp);
791 pp_c_right_paren (pp);
794 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
795 marked to be displayed on disgnostic. */
798 pp_c_attributes_display (c_pretty_printer *pp, tree a)
800 bool is_first = true;
805 for (; a != NULL_TREE; a = TREE_CHAIN (a))
807 const struct attribute_spec *as;
808 as = lookup_attribute_spec (TREE_PURPOSE (a));
809 if (!as || as->affects_type_identity == false)
813 pp_c_ws_string (pp, "__attribute__");
814 pp_c_left_paren (pp);
815 pp_c_left_paren (pp);
820 pp_separate_with (pp, ',');
822 pp_tree_identifier (pp, TREE_PURPOSE (a));
824 pp_c_call_argument_list (pp, TREE_VALUE (a));
829 pp_c_right_paren (pp);
830 pp_c_right_paren (pp);
831 pp_c_whitespace (pp);
835 /* function-definition:
836 declaration-specifiers declarator compound-statement */
839 pp_c_function_definition (c_pretty_printer *pp, tree t)
841 pp_declaration_specifiers (pp, t);
842 pp_declarator (pp, t);
843 pp_needs_newline (pp) = true;
844 pp_statement (pp, DECL_SAVED_TREE (t));
852 /* Print out a c-char. This is called solely for characters which are
853 in the *target* execution character set. We ought to convert them
854 back to the *host* execution character set before printing, but we
855 have no way to do this at present. A decent compromise is to print
856 all characters as if they were in the host execution character set,
857 and not attempt to recover any named escape characters, but render
858 all unprintables as octal escapes. If the host and target character
859 sets are the same, this produces relatively readable output. If they
860 are not the same, strings may appear as gibberish, but that's okay
861 (in fact, it may well be what the reader wants, e.g. if they are looking
862 to see if conversion to the target character set happened correctly).
864 A special case: we need to prefix \, ", and ' with backslashes. It is
865 correct to do so for the *host*'s \, ", and ', because the rest of the
866 file appears in the host character set. */
869 pp_c_char (c_pretty_printer *pp, int c)
875 case '\\': pp_string (pp, "\\\\"); break;
876 case '\'': pp_string (pp, "\\\'"); break;
877 case '\"': pp_string (pp, "\\\""); break;
878 default: pp_character (pp, c);
882 pp_scalar (pp, "\\%03o", (unsigned) c);
885 /* Print out a STRING literal. */
888 pp_c_string_literal (c_pretty_printer *pp, tree s)
890 const char *p = TREE_STRING_POINTER (s);
891 int n = TREE_STRING_LENGTH (s) - 1;
894 for (i = 0; i < n; ++i)
895 pp_c_char (pp, p[i]);
899 /* Pretty-print an INTEGER literal. */
902 pp_c_integer_constant (c_pretty_printer *pp, tree i)
904 /* We are going to compare the type of I to other types using
905 pointer comparison so we need to use its canonical type. */
907 TYPE_CANONICAL (TREE_TYPE (i))
908 ? TYPE_CANONICAL (TREE_TYPE (i))
911 if (TREE_INT_CST_HIGH (i) == 0)
912 pp_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)
919 pp_character (pp, '-');
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 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1020 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
1021 pp_string (pp, pp_buffer(pp)->digit_buffer);
1022 if (TREE_TYPE (r) == float_type_node)
1023 pp_character (pp, 'f');
1024 else if (TREE_TYPE (r) == long_double_type_node)
1025 pp_character (pp, 'l');
1026 else if (TREE_TYPE (r) == dfloat128_type_node)
1027 pp_string (pp, "dl");
1028 else if (TREE_TYPE (r) == dfloat64_type_node)
1029 pp_string (pp, "dd");
1030 else if (TREE_TYPE (r) == dfloat32_type_node)
1031 pp_string (pp, "df");
1034 /* Print out a FIXED value as a decimal-floating-constant. */
1037 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1039 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1040 sizeof (pp_buffer (pp)->digit_buffer));
1041 pp_string (pp, pp_buffer(pp)->digit_buffer);
1044 /* Pretty-print a compound literal expression. GNU extensions include
1045 vector constants. */
1048 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1050 tree type = TREE_TYPE (e);
1051 pp_c_type_cast (pp, type);
1053 switch (TREE_CODE (type))
1060 pp_c_brace_enclosed_initializer_list (pp, e);
1064 pp_unsupported_tree (pp, e);
1069 /* Pretty-print a COMPLEX_EXPR expression. */
1072 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1074 /* Handle a few common special cases, otherwise fallback
1075 to printing it as compound literal. */
1076 tree type = TREE_TYPE (e);
1077 tree realexpr = TREE_OPERAND (e, 0);
1078 tree imagexpr = TREE_OPERAND (e, 1);
1080 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1081 if (TREE_CODE (realexpr) == NOP_EXPR
1082 && TREE_CODE (imagexpr) == NOP_EXPR
1083 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1084 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1085 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1086 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1087 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1088 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1090 pp_c_type_cast (pp, type);
1091 pp_expression (pp, TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1095 /* Cast of an scalar expression to COMPLEX_TYPE. */
1096 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1097 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1099 pp_c_type_cast (pp, type);
1100 if (TREE_CODE (realexpr) == NOP_EXPR)
1101 realexpr = TREE_OPERAND (realexpr, 0);
1102 pp_expression (pp, realexpr);
1106 pp_c_compound_literal (pp, e);
1112 fixed-point-constant
1113 enumeration-constant
1114 character-constant */
1117 pp_c_constant (c_pretty_printer *pp, tree e)
1119 const enum tree_code code = TREE_CODE (e);
1125 tree type = TREE_TYPE (e);
1126 if (type == boolean_type_node)
1127 pp_c_bool_constant (pp, e);
1128 else if (type == char_type_node)
1129 pp_c_character_constant (pp, e);
1130 else if (TREE_CODE (type) == ENUMERAL_TYPE
1131 && pp_c_enumeration_constant (pp, e))
1134 pp_c_integer_constant (pp, e);
1139 pp_c_floating_constant (pp, e);
1143 pp_c_fixed_constant (pp, e);
1147 pp_c_string_literal (pp, e);
1151 /* Sometimes, we are confused and we think a complex literal
1152 is a constant. Such thing is a compound literal which
1153 grammatically belongs to postfix-expr production. */
1154 pp_c_compound_literal (pp, e);
1158 pp_unsupported_tree (pp, e);
1163 /* Pretty-print a string such as an identifier, without changing its
1164 encoding, preceded by whitespace is necessary. */
1167 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1169 pp_c_maybe_whitespace (pp);
1170 pp_string (pp, str);
1171 pp_base (pp)->padding = pp_before;
1174 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1175 that need converting to the locale encoding, preceded by whitespace
1179 pp_c_identifier (c_pretty_printer *pp, const char *id)
1181 pp_c_maybe_whitespace (pp);
1182 pp_identifier (pp, id);
1183 pp_base (pp)->padding = pp_before;
1186 /* Pretty-print a C primary-expression.
1194 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1196 switch (TREE_CODE (e))
1204 pp_c_tree_decl_identifier (pp, e);
1207 case IDENTIFIER_NODE:
1208 pp_c_tree_identifier (pp, e);
1212 pp_c_ws_string (pp, M_("<erroneous-expression>"));
1216 pp_c_ws_string (pp, M_("<return-value>"));
1223 pp_c_constant (pp, e);
1227 pp_c_ws_string (pp, "__builtin_memcpy");
1228 pp_c_left_paren (pp);
1230 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1231 pp_separate_with (pp, ',');
1233 pp_initializer (pp, TREE_OPERAND (e, 1));
1234 if (TREE_OPERAND (e, 2))
1236 pp_separate_with (pp, ',');
1237 pp_c_expression (pp, TREE_OPERAND (e, 2));
1239 pp_c_right_paren (pp);
1243 /* FIXME: Make sure we won't get into an infinite loop. */
1244 pp_c_left_paren (pp);
1245 pp_expression (pp, e);
1246 pp_c_right_paren (pp);
1251 /* Print out a C initializer -- also support C compound-literals.
1253 assignment-expression:
1254 { initializer-list }
1255 { initializer-list , } */
1258 pp_c_initializer (c_pretty_printer *pp, tree e)
1260 if (TREE_CODE (e) == CONSTRUCTOR)
1261 pp_c_brace_enclosed_initializer_list (pp, e);
1263 pp_expression (pp, e);
1268 declarator = initializer */
1271 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1273 pp_declarator (pp, t);
1274 /* We don't want to output function definitions here. There are handled
1275 elsewhere (and the syntactic form is bogus anyway). */
1276 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1278 tree init = DECL_INITIAL (t);
1279 /* This C++ bit is handled here because it is easier to do so.
1280 In templates, the C++ parser builds a TREE_LIST for a
1281 direct-initialization; the TREE_PURPOSE is the variable to
1282 initialize and the TREE_VALUE is the initializer. */
1283 if (TREE_CODE (init) == TREE_LIST)
1285 pp_c_left_paren (pp);
1286 pp_expression (pp, TREE_VALUE (init));
1287 pp_right_paren (pp);
1294 pp_c_initializer (pp, init);
1299 /* initializer-list:
1300 designation(opt) initializer
1301 initializer-list , designation(opt) initializer
1308 designator-list designator
1311 [ constant-expression ]
1315 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1317 tree type = TREE_TYPE (e);
1318 const enum tree_code code = TREE_CODE (type);
1320 if (TREE_CODE (e) == CONSTRUCTOR)
1322 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1332 tree init = TREE_OPERAND (e, 0);
1333 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1335 if (code == RECORD_TYPE || code == UNION_TYPE)
1338 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1342 pp_c_left_bracket (pp);
1343 if (TREE_PURPOSE (init))
1344 pp_c_constant (pp, TREE_PURPOSE (init));
1345 pp_c_right_bracket (pp);
1347 pp_c_whitespace (pp);
1349 pp_c_whitespace (pp);
1350 pp_initializer (pp, TREE_VALUE (init));
1351 if (TREE_CHAIN (init))
1352 pp_separate_with (pp, ',');
1358 if (TREE_CODE (e) == VECTOR_CST)
1359 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1365 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1367 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1368 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1369 pp_separate_with (pp, ',');
1370 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1380 pp_unsupported_tree (pp, type);
1383 /* Pretty-print a brace-enclosed initializer-list. */
1386 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1388 pp_c_left_brace (pp);
1389 pp_c_initializer_list (pp, l);
1390 pp_c_right_brace (pp);
1394 /* This is a convenient function, used to bridge gap between C and C++
1401 pp_c_id_expression (c_pretty_printer *pp, tree t)
1403 switch (TREE_CODE (t))
1412 pp_c_tree_decl_identifier (pp, t);
1415 case IDENTIFIER_NODE:
1416 pp_c_tree_identifier (pp, t);
1420 pp_unsupported_tree (pp, t);
1425 /* postfix-expression:
1427 postfix-expression [ expression ]
1428 postfix-expression ( argument-expression-list(opt) )
1429 postfix-expression . identifier
1430 postfix-expression -> identifier
1431 postfix-expression ++
1432 postfix-expression --
1433 ( type-name ) { initializer-list }
1434 ( type-name ) { initializer-list , } */
1437 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1439 enum tree_code code = TREE_CODE (e);
1442 case POSTINCREMENT_EXPR:
1443 case POSTDECREMENT_EXPR:
1444 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1445 pp_string (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1449 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1450 pp_c_left_bracket (pp);
1451 pp_expression (pp, TREE_OPERAND (e, 1));
1452 pp_c_right_bracket (pp);
1457 call_expr_arg_iterator iter;
1459 pp_postfix_expression (pp, CALL_EXPR_FN (e));
1460 pp_c_left_paren (pp);
1461 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1463 pp_expression (pp, arg);
1464 if (more_call_expr_args_p (&iter))
1465 pp_separate_with (pp, ',');
1467 pp_c_right_paren (pp);
1471 case UNORDERED_EXPR:
1472 pp_c_ws_string (pp, flag_isoc99
1474 : "__builtin_isunordered");
1478 pp_c_ws_string (pp, flag_isoc99
1480 : "!__builtin_isunordered");
1484 pp_c_ws_string (pp, flag_isoc99
1486 : "!__builtin_isgreaterequal");
1490 pp_c_ws_string (pp, flag_isoc99
1492 : "!__builtin_isgreater");
1496 pp_c_ws_string (pp, flag_isoc99
1498 : "!__builtin_islessequal");
1502 pp_c_ws_string (pp, flag_isoc99
1504 : "!__builtin_isless");
1508 pp_c_ws_string (pp, flag_isoc99
1510 : "!__builtin_islessgreater");
1514 pp_c_ws_string (pp, flag_isoc99
1516 : "__builtin_islessgreater");
1520 pp_c_left_paren (pp);
1521 pp_expression (pp, TREE_OPERAND (e, 0));
1522 pp_separate_with (pp, ',');
1523 pp_expression (pp, TREE_OPERAND (e, 1));
1524 pp_c_right_paren (pp);
1528 pp_c_ws_string (pp, "__builtin_abs");
1529 pp_c_left_paren (pp);
1530 pp_expression (pp, TREE_OPERAND (e, 0));
1531 pp_c_right_paren (pp);
1536 tree object = TREE_OPERAND (e, 0);
1537 if (TREE_CODE (object) == INDIRECT_REF)
1539 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1544 pp_postfix_expression (pp, object);
1547 pp_expression (pp, TREE_OPERAND (e, 1));
1553 tree type = TREE_TYPE (e);
1555 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1557 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1559 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1560 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1561 if ((bitpos % size) == 0)
1563 pp_c_left_paren (pp);
1564 pp_c_left_paren (pp);
1565 pp_type_id (pp, type);
1567 pp_c_right_paren (pp);
1568 pp_c_ampersand (pp);
1569 pp_expression (pp, TREE_OPERAND (e, 0));
1570 pp_c_right_paren (pp);
1571 pp_c_left_bracket (pp);
1572 pp_wide_integer (pp, bitpos / size);
1573 pp_c_right_bracket (pp);
1577 pp_unsupported_tree (pp, e);
1582 pp_c_expression (pp, e);
1587 pp_c_compound_literal (pp, e);
1591 pp_c_complex_expr (pp, e);
1594 case COMPOUND_LITERAL_EXPR:
1595 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1598 pp_initializer (pp, e);
1602 pp_c_ws_string (pp, "__builtin_va_arg");
1603 pp_c_left_paren (pp);
1604 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1605 pp_separate_with (pp, ',');
1606 pp_type_id (pp, TREE_TYPE (e));
1607 pp_c_right_paren (pp);
1611 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1613 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1616 /* else fall through. */
1619 pp_primary_expression (pp, e);
1624 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1627 pp_c_expression_list (c_pretty_printer *pp, tree e)
1629 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1631 pp_expression (pp, TREE_VALUE (e));
1633 pp_separate_with (pp, ',');
1637 /* Print out V, which contains the elements of a constructor. */
1640 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1642 unsigned HOST_WIDE_INT ix;
1645 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1647 pp_expression (pp, value);
1648 if (ix != VEC_length (constructor_elt, v) - 1)
1649 pp_separate_with (pp, ',');
1653 /* Print out an expression-list in parens, as if it were the argument
1654 list to a function. */
1657 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1659 pp_c_left_paren (pp);
1660 if (t && TREE_CODE (t) == TREE_LIST)
1661 pp_c_expression_list (pp, t);
1662 pp_c_right_paren (pp);
1665 /* unary-expression:
1669 unary-operator cast-expression
1670 sizeof unary-expression
1673 unary-operator: one of
1678 __alignof__ unary-expression
1679 __alignof__ ( type-id )
1680 __real__ unary-expression
1681 __imag__ unary-expression */
1684 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1686 enum tree_code code = TREE_CODE (e);
1689 case PREINCREMENT_EXPR:
1690 case PREDECREMENT_EXPR:
1691 pp_string (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1692 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1699 case TRUTH_NOT_EXPR:
1701 /* String literal are used by address. */
1702 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1704 else if (code == INDIRECT_REF)
1706 else if (code == NEGATE_EXPR)
1708 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1710 else if (code == TRUTH_NOT_EXPR)
1711 pp_exclamation (pp);
1712 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1716 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1717 && integer_zerop (TREE_OPERAND (e, 1)))
1718 pp_c_expression (pp, TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1722 if (!integer_zerop (TREE_OPERAND (e, 1)))
1724 pp_c_left_paren (pp);
1725 if (!integer_onep (TYPE_SIZE_UNIT
1726 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1727 pp_c_type_cast (pp, ptr_type_node);
1729 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1730 if (!integer_zerop (TREE_OPERAND (e, 1)))
1733 pp_c_integer_constant (pp,
1734 fold_convert (ssizetype,
1735 TREE_OPERAND (e, 1)));
1736 pp_c_right_paren (pp);
1743 pp_c_ws_string (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1744 pp_c_whitespace (pp);
1745 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1749 pp_postfix_expression (pp, e);
1756 ( type-name ) cast-expression */
1759 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1761 switch (TREE_CODE (e))
1764 case FIX_TRUNC_EXPR:
1766 case VIEW_CONVERT_EXPR:
1767 pp_c_type_cast (pp, TREE_TYPE (e));
1768 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1772 pp_unary_expression (pp, e);
1776 /* multiplicative-expression:
1778 multiplicative-expression * cast-expression
1779 multiplicative-expression / cast-expression
1780 multiplicative-expression % cast-expression */
1783 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1785 enum tree_code code = TREE_CODE (e);
1789 case TRUNC_DIV_EXPR:
1790 case TRUNC_MOD_EXPR:
1791 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1792 pp_c_whitespace (pp);
1793 if (code == MULT_EXPR)
1795 else if (code == TRUNC_DIV_EXPR)
1799 pp_c_whitespace (pp);
1800 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1804 pp_c_cast_expression (pp, e);
1809 /* additive-expression:
1810 multiplicative-expression
1811 additive-expression + multiplicative-expression
1812 additive-expression - multiplicative-expression */
1815 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1817 enum tree_code code = TREE_CODE (e);
1820 case POINTER_PLUS_EXPR:
1823 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1824 pp_c_whitespace (pp);
1825 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1829 pp_c_whitespace (pp);
1830 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1834 pp_multiplicative_expression (pp, e);
1839 /* additive-expression:
1841 shift-expression << additive-expression
1842 shift-expression >> additive-expression */
1845 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1847 enum tree_code code = TREE_CODE (e);
1852 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1853 pp_c_whitespace (pp);
1854 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1855 pp_c_whitespace (pp);
1856 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1860 pp_c_additive_expression (pp, e);
1864 /* relational-expression:
1866 relational-expression < shift-expression
1867 relational-expression > shift-expression
1868 relational-expression <= shift-expression
1869 relational-expression >= shift-expression */
1872 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1874 enum tree_code code = TREE_CODE (e);
1881 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1882 pp_c_whitespace (pp);
1883 if (code == LT_EXPR)
1885 else if (code == GT_EXPR)
1887 else if (code == LE_EXPR)
1888 pp_string (pp, "<=");
1889 else if (code == GE_EXPR)
1890 pp_string (pp, ">=");
1891 pp_c_whitespace (pp);
1892 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1896 pp_c_shift_expression (pp, e);
1901 /* equality-expression:
1902 relational-expression
1903 equality-expression == relational-expression
1904 equality-equality != relational-expression */
1907 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1909 enum tree_code code = TREE_CODE (e);
1914 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1915 pp_c_whitespace (pp);
1916 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1917 pp_c_whitespace (pp);
1918 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1922 pp_c_relational_expression (pp, e);
1929 AND-expression & equality-equality */
1932 pp_c_and_expression (c_pretty_printer *pp, tree e)
1934 if (TREE_CODE (e) == BIT_AND_EXPR)
1936 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1937 pp_c_whitespace (pp);
1939 pp_c_whitespace (pp);
1940 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1943 pp_c_equality_expression (pp, e);
1946 /* exclusive-OR-expression:
1948 exclusive-OR-expression ^ AND-expression */
1951 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1953 if (TREE_CODE (e) == BIT_XOR_EXPR
1954 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1956 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1957 if (TREE_CODE (e) == BIT_XOR_EXPR)
1958 pp_c_maybe_whitespace (pp);
1960 pp_c_whitespace (pp);
1962 pp_c_whitespace (pp);
1963 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1966 pp_c_and_expression (pp, e);
1969 /* inclusive-OR-expression:
1970 exclusive-OR-expression
1971 inclusive-OR-expression | exclusive-OR-expression */
1974 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1976 if (TREE_CODE (e) == BIT_IOR_EXPR)
1978 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1979 pp_c_whitespace (pp);
1981 pp_c_whitespace (pp);
1982 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1985 pp_c_exclusive_or_expression (pp, e);
1988 /* logical-AND-expression:
1989 inclusive-OR-expression
1990 logical-AND-expression && inclusive-OR-expression */
1993 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1995 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
1996 || TREE_CODE (e) == TRUTH_AND_EXPR)
1998 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1999 pp_c_whitespace (pp);
2000 pp_string (pp, "&&");
2001 pp_c_whitespace (pp);
2002 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2005 pp_c_inclusive_or_expression (pp, e);
2008 /* logical-OR-expression:
2009 logical-AND-expression
2010 logical-OR-expression || logical-AND-expression */
2013 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2015 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2016 || TREE_CODE (e) == TRUTH_OR_EXPR)
2018 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2019 pp_c_whitespace (pp);
2020 pp_string (pp, "||");
2021 pp_c_whitespace (pp);
2022 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2025 pp_c_logical_and_expression (pp, e);
2028 /* conditional-expression:
2029 logical-OR-expression
2030 logical-OR-expression ? expression : conditional-expression */
2033 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
2035 if (TREE_CODE (e) == COND_EXPR)
2037 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2038 pp_c_whitespace (pp);
2040 pp_c_whitespace (pp);
2041 pp_expression (pp, TREE_OPERAND (e, 1));
2042 pp_c_whitespace (pp);
2044 pp_c_whitespace (pp);
2045 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
2048 pp_c_logical_or_expression (pp, e);
2052 /* assignment-expression:
2053 conditional-expression
2054 unary-expression assignment-operator assignment-expression
2056 assignment-expression: one of
2057 = *= /= %= += -= >>= <<= &= ^= |= */
2060 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
2062 if (TREE_CODE (e) == MODIFY_EXPR
2063 || TREE_CODE (e) == INIT_EXPR)
2065 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
2066 pp_c_whitespace (pp);
2069 pp_c_expression (pp, TREE_OPERAND (e, 1));
2072 pp_c_conditional_expression (pp, e);
2076 assignment-expression
2077 expression , assignment-expression
2079 Implementation note: instead of going through the usual recursion
2080 chain, I take the liberty of dispatching nodes to the appropriate
2081 functions. This makes some redundancy, but it worths it. That also
2082 prevents a possible infinite recursion between pp_c_primary_expression ()
2083 and pp_c_expression (). */
2086 pp_c_expression (c_pretty_printer *pp, tree e)
2088 switch (TREE_CODE (e))
2091 pp_c_integer_constant (pp, e);
2095 pp_c_floating_constant (pp, e);
2099 pp_c_fixed_constant (pp, e);
2103 pp_c_string_literal (pp, e);
2106 case IDENTIFIER_NODE:
2115 pp_primary_expression (pp, e);
2118 case POSTINCREMENT_EXPR:
2119 case POSTDECREMENT_EXPR:
2128 case UNORDERED_EXPR:
2137 case COMPOUND_LITERAL_EXPR:
2139 pp_postfix_expression (pp, e);
2148 case TRUTH_NOT_EXPR:
2149 case PREINCREMENT_EXPR:
2150 case PREDECREMENT_EXPR:
2153 pp_c_unary_expression (pp, e);
2157 case FIX_TRUNC_EXPR:
2159 case VIEW_CONVERT_EXPR:
2160 pp_c_cast_expression (pp, e);
2164 case TRUNC_MOD_EXPR:
2165 case TRUNC_DIV_EXPR:
2166 pp_multiplicative_expression (pp, e);
2171 pp_c_shift_expression (pp, e);
2178 pp_c_relational_expression (pp, e);
2182 pp_c_and_expression (pp, e);
2186 case TRUTH_XOR_EXPR:
2187 pp_c_exclusive_or_expression (pp, e);
2191 pp_c_inclusive_or_expression (pp, e);
2194 case TRUTH_ANDIF_EXPR:
2195 case TRUTH_AND_EXPR:
2196 pp_c_logical_and_expression (pp, e);
2199 case TRUTH_ORIF_EXPR:
2201 pp_c_logical_or_expression (pp, e);
2206 pp_c_equality_expression (pp, e);
2210 pp_conditional_expression (pp, e);
2213 case POINTER_PLUS_EXPR:
2216 pp_c_additive_expression (pp, e);
2221 pp_assignment_expression (pp, e);
2225 pp_c_left_paren (pp);
2226 pp_expression (pp, TREE_OPERAND (e, 0));
2227 pp_separate_with (pp, ',');
2228 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
2229 pp_c_right_paren (pp);
2232 case NON_LVALUE_EXPR:
2234 pp_expression (pp, TREE_OPERAND (e, 0));
2238 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
2243 /* We don't yet have a way of dumping statements in a
2244 human-readable format. */
2245 pp_string (pp, "({...})");
2248 case C_MAYBE_CONST_EXPR:
2249 pp_c_expression (pp, C_MAYBE_CONST_EXPR_EXPR (e));
2253 pp_unsupported_tree (pp, e);
2263 pp_c_statement (c_pretty_printer *pp, tree stmt)
2268 if (pp_needs_newline (pp))
2269 pp_newline_and_indent (pp, 0);
2271 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2275 /* Initialize the PRETTY-PRINTER for handling C codes. */
2278 pp_c_pretty_printer_init (c_pretty_printer *pp)
2280 pp->offset_list = 0;
2282 pp->declaration = pp_c_declaration;
2283 pp->declaration_specifiers = pp_c_declaration_specifiers;
2284 pp->declarator = pp_c_declarator;
2285 pp->direct_declarator = pp_c_direct_declarator;
2286 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2287 pp->abstract_declarator = pp_c_abstract_declarator;
2288 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2289 pp->ptr_operator = pp_c_pointer;
2290 pp->parameter_list = pp_c_parameter_type_list;
2291 pp->type_id = pp_c_type_id;
2292 pp->simple_type_specifier = pp_c_type_specifier;
2293 pp->function_specifier = pp_c_function_specifier;
2294 pp->storage_class_specifier = pp_c_storage_class_specifier;
2296 pp->statement = pp_c_statement;
2298 pp->constant = pp_c_constant;
2299 pp->id_expression = pp_c_id_expression;
2300 pp->primary_expression = pp_c_primary_expression;
2301 pp->postfix_expression = pp_c_postfix_expression;
2302 pp->unary_expression = pp_c_unary_expression;
2303 pp->initializer = pp_c_initializer;
2304 pp->multiplicative_expression = pp_c_multiplicative_expression;
2305 pp->conditional_expression = pp_c_conditional_expression;
2306 pp->assignment_expression = pp_c_assignment_expression;
2307 pp->expression = pp_c_expression;
2311 /* Print the tree T in full, on file FILE. */
2314 print_c_tree (FILE *file, tree t)
2316 static c_pretty_printer pp_rec;
2317 static bool initialized = 0;
2318 c_pretty_printer *pp = &pp_rec;
2323 pp_construct (pp_base (pp), NULL, 0);
2324 pp_c_pretty_printer_init (pp);
2325 pp_needs_newline (pp) = true;
2327 pp_base (pp)->buffer->stream = file;
2329 pp_statement (pp, t);
2335 /* Print the tree T in full, on stderr. */
2338 debug_c_tree (tree t)
2340 print_c_tree (stderr, t);
2341 fputc ('\n', stderr);
2344 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2345 up of T's memory address. */
2348 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2352 gcc_assert (DECL_P (t));
2355 name = IDENTIFIER_POINTER (DECL_NAME (t));
2358 static char xname[8];
2359 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2363 pp_c_identifier (pp, name);