1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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);
464 else if (!c_dialect_cxx ())
465 pp_c_whitespace (pp);
466 pp_ptr_operator (pp, t);
472 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
477 if (code == COMPLEX_TYPE)
478 pp_c_ws_string (pp, flag_isoc99 ? "_Complex" : "__complex__");
479 else if (code == VECTOR_TYPE)
481 pp_c_ws_string (pp, "__vector");
482 pp_c_left_paren (pp);
483 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
484 pp_c_right_paren (pp);
485 pp_c_whitespace (pp);
487 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
491 pp_simple_type_specifier (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_c_base (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 /* function-definition:
794 declaration-specifiers declarator compound-statement */
797 pp_c_function_definition (c_pretty_printer *pp, tree t)
799 pp_declaration_specifiers (pp, t);
800 pp_declarator (pp, t);
801 pp_needs_newline (pp) = true;
802 pp_statement (pp, DECL_SAVED_TREE (t));
810 /* Print out a c-char. This is called solely for characters which are
811 in the *target* execution character set. We ought to convert them
812 back to the *host* execution character set before printing, but we
813 have no way to do this at present. A decent compromise is to print
814 all characters as if they were in the host execution character set,
815 and not attempt to recover any named escape characters, but render
816 all unprintables as octal escapes. If the host and target character
817 sets are the same, this produces relatively readable output. If they
818 are not the same, strings may appear as gibberish, but that's okay
819 (in fact, it may well be what the reader wants, e.g. if they are looking
820 to see if conversion to the target character set happened correctly).
822 A special case: we need to prefix \, ", and ' with backslashes. It is
823 correct to do so for the *host*'s \, ", and ', because the rest of the
824 file appears in the host character set. */
827 pp_c_char (c_pretty_printer *pp, int c)
833 case '\\': pp_string (pp, "\\\\"); break;
834 case '\'': pp_string (pp, "\\\'"); break;
835 case '\"': pp_string (pp, "\\\""); break;
836 default: pp_character (pp, c);
840 pp_scalar (pp, "\\%03o", (unsigned) c);
843 /* Print out a STRING literal. */
846 pp_c_string_literal (c_pretty_printer *pp, tree s)
848 const char *p = TREE_STRING_POINTER (s);
849 int n = TREE_STRING_LENGTH (s) - 1;
852 for (i = 0; i < n; ++i)
853 pp_c_char (pp, p[i]);
857 /* Pretty-print an INTEGER literal. */
860 pp_c_integer_constant (c_pretty_printer *pp, tree i)
862 tree type = TREE_TYPE (i);
864 if (TREE_INT_CST_HIGH (i) == 0)
865 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
868 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
869 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
870 if (tree_int_cst_sgn (i) < 0)
872 pp_character (pp, '-');
876 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
877 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
878 pp_string (pp, pp_buffer (pp)->digit_buffer);
880 if (TYPE_UNSIGNED (type))
881 pp_character (pp, 'u');
882 if (type == long_integer_type_node || type == long_unsigned_type_node)
883 pp_character (pp, 'l');
884 else if (type == long_long_integer_type_node
885 || type == long_long_unsigned_type_node)
886 pp_string (pp, "ll");
887 else if (type == int128_integer_type_node
888 || type == int128_unsigned_type_node)
889 pp_string (pp, "I128");
892 /* Print out a CHARACTER literal. */
895 pp_c_character_constant (c_pretty_printer *pp, tree c)
897 tree type = TREE_TYPE (c);
898 if (type == wchar_type_node)
899 pp_character (pp, 'L');
901 if (host_integerp (c, TYPE_UNSIGNED (type)))
902 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
904 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
908 /* Print out a BOOLEAN literal. */
911 pp_c_bool_constant (c_pretty_printer *pp, tree b)
913 if (b == boolean_false_node)
915 if (c_dialect_cxx ())
916 pp_c_ws_string (pp, "false");
917 else if (flag_isoc99)
918 pp_c_ws_string (pp, "_False");
920 pp_unsupported_tree (pp, b);
922 else if (b == boolean_true_node)
924 if (c_dialect_cxx ())
925 pp_c_ws_string (pp, "true");
926 else if (flag_isoc99)
927 pp_c_ws_string (pp, "_True");
929 pp_unsupported_tree (pp, b);
931 else if (TREE_CODE (b) == INTEGER_CST)
932 pp_c_integer_constant (pp, b);
934 pp_unsupported_tree (pp, b);
937 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
938 false; that means the value was obtained by a cast, in which case
939 print out the type-id part of the cast-expression -- the casted value
940 is then printed by pp_c_integer_literal. */
943 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
945 bool value_is_named = true;
946 tree type = TREE_TYPE (e);
949 /* Find the name of this constant. */
950 for (value = TYPE_VALUES (type);
951 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
952 value = TREE_CHAIN (value))
955 if (value != NULL_TREE)
956 pp_id_expression (pp, TREE_PURPOSE (value));
959 /* Value must have been cast. */
960 pp_c_type_cast (pp, type);
961 value_is_named = false;
964 return value_is_named;
967 /* Print out a REAL value as a decimal-floating-constant. */
970 pp_c_floating_constant (c_pretty_printer *pp, tree r)
972 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
973 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
974 pp_string (pp, pp_buffer(pp)->digit_buffer);
975 if (TREE_TYPE (r) == float_type_node)
976 pp_character (pp, 'f');
977 else if (TREE_TYPE (r) == long_double_type_node)
978 pp_character (pp, 'l');
979 else if (TREE_TYPE (r) == dfloat128_type_node)
980 pp_string (pp, "dl");
981 else if (TREE_TYPE (r) == dfloat64_type_node)
982 pp_string (pp, "dd");
983 else if (TREE_TYPE (r) == dfloat32_type_node)
984 pp_string (pp, "df");
987 /* Print out a FIXED value as a decimal-floating-constant. */
990 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
992 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
993 sizeof (pp_buffer (pp)->digit_buffer));
994 pp_string (pp, pp_buffer(pp)->digit_buffer);
997 /* Pretty-print a compound literal expression. GNU extensions include
1001 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1003 tree type = TREE_TYPE (e);
1004 pp_c_type_cast (pp, type);
1006 switch (TREE_CODE (type))
1013 pp_c_brace_enclosed_initializer_list (pp, e);
1017 pp_unsupported_tree (pp, e);
1022 /* Pretty-print a COMPLEX_EXPR expression. */
1025 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1027 /* Handle a few common special cases, otherwise fallback
1028 to printing it as compound literal. */
1029 tree type = TREE_TYPE (e);
1030 tree realexpr = TREE_OPERAND (e, 0);
1031 tree imagexpr = TREE_OPERAND (e, 1);
1033 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1034 if (TREE_CODE (realexpr) == NOP_EXPR
1035 && TREE_CODE (imagexpr) == NOP_EXPR
1036 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1037 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1038 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1039 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1040 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1041 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1043 pp_c_type_cast (pp, type);
1044 pp_expression (pp, TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1048 /* Cast of an scalar expression to COMPLEX_TYPE. */
1049 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1050 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1052 pp_c_type_cast (pp, type);
1053 if (TREE_CODE (realexpr) == NOP_EXPR)
1054 realexpr = TREE_OPERAND (realexpr, 0);
1055 pp_expression (pp, realexpr);
1059 pp_c_compound_literal (pp, e);
1065 fixed-point-constant
1066 enumeration-constant
1067 character-constant */
1070 pp_c_constant (c_pretty_printer *pp, tree e)
1072 const enum tree_code code = TREE_CODE (e);
1078 tree type = TREE_TYPE (e);
1079 if (type == boolean_type_node)
1080 pp_c_bool_constant (pp, e);
1081 else if (type == char_type_node)
1082 pp_c_character_constant (pp, e);
1083 else if (TREE_CODE (type) == ENUMERAL_TYPE
1084 && pp_c_enumeration_constant (pp, e))
1087 pp_c_integer_constant (pp, e);
1092 pp_c_floating_constant (pp, e);
1096 pp_c_fixed_constant (pp, e);
1100 pp_c_string_literal (pp, e);
1104 /* Sometimes, we are confused and we think a complex literal
1105 is a constant. Such thing is a compound literal which
1106 grammatically belongs to postfix-expr production. */
1107 pp_c_compound_literal (pp, e);
1111 pp_unsupported_tree (pp, e);
1116 /* Pretty-print a string such as an identifier, without changing its
1117 encoding, preceded by whitespace is necessary. */
1120 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1122 pp_c_maybe_whitespace (pp);
1123 pp_string (pp, str);
1124 pp_base (pp)->padding = pp_before;
1127 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1128 that need converting to the locale encoding, preceded by whitespace
1132 pp_c_identifier (c_pretty_printer *pp, const char *id)
1134 pp_c_maybe_whitespace (pp);
1135 pp_identifier (pp, id);
1136 pp_base (pp)->padding = pp_before;
1139 /* Pretty-print a C primary-expression.
1147 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1149 switch (TREE_CODE (e))
1157 pp_c_tree_decl_identifier (pp, e);
1160 case IDENTIFIER_NODE:
1161 pp_c_tree_identifier (pp, e);
1165 pp_c_ws_string (pp, M_("<erroneous-expression>"));
1169 pp_c_ws_string (pp, M_("<return-value>"));
1176 pp_c_constant (pp, e);
1180 pp_c_ws_string (pp, "__builtin_memcpy");
1181 pp_c_left_paren (pp);
1183 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1184 pp_separate_with (pp, ',');
1186 pp_initializer (pp, TREE_OPERAND (e, 1));
1187 if (TREE_OPERAND (e, 2))
1189 pp_separate_with (pp, ',');
1190 pp_c_expression (pp, TREE_OPERAND (e, 2));
1192 pp_c_right_paren (pp);
1196 /* FIXME: Make sure we won't get into an infinite loop. */
1197 pp_c_left_paren (pp);
1198 pp_expression (pp, e);
1199 pp_c_right_paren (pp);
1204 /* Print out a C initializer -- also support C compound-literals.
1206 assignment-expression:
1207 { initializer-list }
1208 { initializer-list , } */
1211 pp_c_initializer (c_pretty_printer *pp, tree e)
1213 if (TREE_CODE (e) == CONSTRUCTOR)
1214 pp_c_brace_enclosed_initializer_list (pp, e);
1216 pp_expression (pp, e);
1221 declarator = initializer */
1224 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1226 pp_declarator (pp, t);
1227 /* We don't want to output function definitions here. There are handled
1228 elsewhere (and the syntactic form is bogus anyway). */
1229 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1231 tree init = DECL_INITIAL (t);
1232 /* This C++ bit is handled here because it is easier to do so.
1233 In templates, the C++ parser builds a TREE_LIST for a
1234 direct-initialization; the TREE_PURPOSE is the variable to
1235 initialize and the TREE_VALUE is the initializer. */
1236 if (TREE_CODE (init) == TREE_LIST)
1238 pp_c_left_paren (pp);
1239 pp_expression (pp, TREE_VALUE (init));
1240 pp_right_paren (pp);
1247 pp_c_initializer (pp, init);
1252 /* initializer-list:
1253 designation(opt) initializer
1254 initializer-list , designation(opt) initializer
1261 designator-list designator
1264 [ constant-expression ]
1268 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1270 tree type = TREE_TYPE (e);
1271 const enum tree_code code = TREE_CODE (type);
1273 if (TREE_CODE (e) == CONSTRUCTOR)
1275 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1285 tree init = TREE_OPERAND (e, 0);
1286 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1288 if (code == RECORD_TYPE || code == UNION_TYPE)
1291 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1295 pp_c_left_bracket (pp);
1296 if (TREE_PURPOSE (init))
1297 pp_c_constant (pp, TREE_PURPOSE (init));
1298 pp_c_right_bracket (pp);
1300 pp_c_whitespace (pp);
1302 pp_c_whitespace (pp);
1303 pp_initializer (pp, TREE_VALUE (init));
1304 if (TREE_CHAIN (init))
1305 pp_separate_with (pp, ',');
1311 if (TREE_CODE (e) == VECTOR_CST)
1312 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1318 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1320 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1321 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1322 pp_separate_with (pp, ',');
1323 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1333 pp_unsupported_tree (pp, type);
1336 /* Pretty-print a brace-enclosed initializer-list. */
1339 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1341 pp_c_left_brace (pp);
1342 pp_c_initializer_list (pp, l);
1343 pp_c_right_brace (pp);
1347 /* This is a convenient function, used to bridge gap between C and C++
1354 pp_c_id_expression (c_pretty_printer *pp, tree t)
1356 switch (TREE_CODE (t))
1365 pp_c_tree_decl_identifier (pp, t);
1368 case IDENTIFIER_NODE:
1369 pp_c_tree_identifier (pp, t);
1373 pp_unsupported_tree (pp, t);
1378 /* postfix-expression:
1380 postfix-expression [ expression ]
1381 postfix-expression ( argument-expression-list(opt) )
1382 postfix-expression . identifier
1383 postfix-expression -> identifier
1384 postfix-expression ++
1385 postfix-expression --
1386 ( type-name ) { initializer-list }
1387 ( type-name ) { initializer-list , } */
1390 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1392 enum tree_code code = TREE_CODE (e);
1395 case POSTINCREMENT_EXPR:
1396 case POSTDECREMENT_EXPR:
1397 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1398 pp_string (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1402 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1403 pp_c_left_bracket (pp);
1404 pp_expression (pp, TREE_OPERAND (e, 1));
1405 pp_c_right_bracket (pp);
1410 call_expr_arg_iterator iter;
1412 pp_postfix_expression (pp, CALL_EXPR_FN (e));
1413 pp_c_left_paren (pp);
1414 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1416 pp_expression (pp, arg);
1417 if (more_call_expr_args_p (&iter))
1418 pp_separate_with (pp, ',');
1420 pp_c_right_paren (pp);
1424 case UNORDERED_EXPR:
1425 pp_c_ws_string (pp, flag_isoc99
1427 : "__builtin_isunordered");
1431 pp_c_ws_string (pp, flag_isoc99
1433 : "!__builtin_isunordered");
1437 pp_c_ws_string (pp, flag_isoc99
1439 : "!__builtin_isgreaterequal");
1443 pp_c_ws_string (pp, flag_isoc99
1445 : "!__builtin_isgreater");
1449 pp_c_ws_string (pp, flag_isoc99
1451 : "!__builtin_islessequal");
1455 pp_c_ws_string (pp, flag_isoc99
1457 : "!__builtin_isless");
1461 pp_c_ws_string (pp, flag_isoc99
1463 : "!__builtin_islessgreater");
1467 pp_c_ws_string (pp, flag_isoc99
1469 : "__builtin_islessgreater");
1473 pp_c_left_paren (pp);
1474 pp_expression (pp, TREE_OPERAND (e, 0));
1475 pp_separate_with (pp, ',');
1476 pp_expression (pp, TREE_OPERAND (e, 1));
1477 pp_c_right_paren (pp);
1481 pp_c_ws_string (pp, "__builtin_abs");
1482 pp_c_left_paren (pp);
1483 pp_expression (pp, TREE_OPERAND (e, 0));
1484 pp_c_right_paren (pp);
1489 tree object = TREE_OPERAND (e, 0);
1490 if (TREE_CODE (object) == INDIRECT_REF)
1492 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1497 pp_postfix_expression (pp, object);
1500 pp_expression (pp, TREE_OPERAND (e, 1));
1506 tree type = TREE_TYPE (e);
1508 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1510 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1512 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1513 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1514 if ((bitpos % size) == 0)
1516 pp_c_left_paren (pp);
1517 pp_c_left_paren (pp);
1518 pp_type_id (pp, type);
1520 pp_c_right_paren (pp);
1521 pp_c_ampersand (pp);
1522 pp_expression (pp, TREE_OPERAND (e, 0));
1523 pp_c_right_paren (pp);
1524 pp_c_left_bracket (pp);
1525 pp_wide_integer (pp, bitpos / size);
1526 pp_c_right_bracket (pp);
1530 pp_unsupported_tree (pp, e);
1535 pp_c_expression (pp, e);
1540 pp_c_compound_literal (pp, e);
1544 pp_c_complex_expr (pp, e);
1547 case COMPOUND_LITERAL_EXPR:
1548 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1551 pp_initializer (pp, e);
1555 pp_c_ws_string (pp, "__builtin_va_arg");
1556 pp_c_left_paren (pp);
1557 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1558 pp_separate_with (pp, ',');
1559 pp_type_id (pp, TREE_TYPE (e));
1560 pp_c_right_paren (pp);
1564 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1566 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1569 /* else fall through. */
1572 pp_primary_expression (pp, e);
1577 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1580 pp_c_expression_list (c_pretty_printer *pp, tree e)
1582 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1584 pp_expression (pp, TREE_VALUE (e));
1586 pp_separate_with (pp, ',');
1590 /* Print out V, which contains the elements of a constructor. */
1593 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1595 unsigned HOST_WIDE_INT ix;
1598 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1600 pp_expression (pp, value);
1601 if (ix != VEC_length (constructor_elt, v) - 1)
1602 pp_separate_with (pp, ',');
1606 /* Print out an expression-list in parens, as if it were the argument
1607 list to a function. */
1610 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1612 pp_c_left_paren (pp);
1613 if (t && TREE_CODE (t) == TREE_LIST)
1614 pp_c_expression_list (pp, t);
1615 pp_c_right_paren (pp);
1618 /* unary-expression:
1622 unary-operator cast-expression
1623 sizeof unary-expression
1626 unary-operator: one of
1631 __alignof__ unary-expression
1632 __alignof__ ( type-id )
1633 __real__ unary-expression
1634 __imag__ unary-expression */
1637 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1639 enum tree_code code = TREE_CODE (e);
1642 case PREINCREMENT_EXPR:
1643 case PREDECREMENT_EXPR:
1644 pp_string (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1645 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1652 case TRUTH_NOT_EXPR:
1654 /* String literal are used by address. */
1655 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1657 else if (code == INDIRECT_REF)
1659 else if (code == NEGATE_EXPR)
1661 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1663 else if (code == TRUTH_NOT_EXPR)
1664 pp_exclamation (pp);
1665 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1669 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1670 && integer_zerop (TREE_OPERAND (e, 1)))
1671 pp_c_expression (pp, TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1675 if (!integer_zerop (TREE_OPERAND (e, 1)))
1677 pp_c_left_paren (pp);
1678 if (!integer_onep (TYPE_SIZE_UNIT
1679 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1680 pp_c_type_cast (pp, ptr_type_node);
1682 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1683 if (!integer_zerop (TREE_OPERAND (e, 1)))
1686 pp_c_integer_constant (pp,
1687 fold_convert (ssizetype,
1688 TREE_OPERAND (e, 1)));
1689 pp_c_right_paren (pp);
1696 pp_c_ws_string (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1697 pp_c_whitespace (pp);
1698 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1702 pp_postfix_expression (pp, e);
1709 ( type-name ) cast-expression */
1712 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1714 switch (TREE_CODE (e))
1717 case FIX_TRUNC_EXPR:
1719 case VIEW_CONVERT_EXPR:
1720 pp_c_type_cast (pp, TREE_TYPE (e));
1721 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1725 pp_unary_expression (pp, e);
1729 /* multiplicative-expression:
1731 multiplicative-expression * cast-expression
1732 multiplicative-expression / cast-expression
1733 multiplicative-expression % cast-expression */
1736 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1738 enum tree_code code = TREE_CODE (e);
1742 case TRUNC_DIV_EXPR:
1743 case TRUNC_MOD_EXPR:
1744 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1745 pp_c_whitespace (pp);
1746 if (code == MULT_EXPR)
1748 else if (code == TRUNC_DIV_EXPR)
1752 pp_c_whitespace (pp);
1753 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1757 pp_c_cast_expression (pp, e);
1762 /* additive-expression:
1763 multiplicative-expression
1764 additive-expression + multiplicative-expression
1765 additive-expression - multiplicative-expression */
1768 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1770 enum tree_code code = TREE_CODE (e);
1773 case POINTER_PLUS_EXPR:
1776 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1777 pp_c_whitespace (pp);
1778 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1782 pp_c_whitespace (pp);
1783 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1787 pp_multiplicative_expression (pp, e);
1792 /* additive-expression:
1794 shift-expression << additive-expression
1795 shift-expression >> additive-expression */
1798 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1800 enum tree_code code = TREE_CODE (e);
1805 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1806 pp_c_whitespace (pp);
1807 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1808 pp_c_whitespace (pp);
1809 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1813 pp_c_additive_expression (pp, e);
1817 /* relational-expression:
1819 relational-expression < shift-expression
1820 relational-expression > shift-expression
1821 relational-expression <= shift-expression
1822 relational-expression >= shift-expression */
1825 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1827 enum tree_code code = TREE_CODE (e);
1834 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1835 pp_c_whitespace (pp);
1836 if (code == LT_EXPR)
1838 else if (code == GT_EXPR)
1840 else if (code == LE_EXPR)
1841 pp_string (pp, "<=");
1842 else if (code == GE_EXPR)
1843 pp_string (pp, ">=");
1844 pp_c_whitespace (pp);
1845 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1849 pp_c_shift_expression (pp, e);
1854 /* equality-expression:
1855 relational-expression
1856 equality-expression == relational-expression
1857 equality-equality != relational-expression */
1860 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1862 enum tree_code code = TREE_CODE (e);
1867 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1868 pp_c_whitespace (pp);
1869 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1870 pp_c_whitespace (pp);
1871 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1875 pp_c_relational_expression (pp, e);
1882 AND-expression & equality-equality */
1885 pp_c_and_expression (c_pretty_printer *pp, tree e)
1887 if (TREE_CODE (e) == BIT_AND_EXPR)
1889 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1890 pp_c_whitespace (pp);
1892 pp_c_whitespace (pp);
1893 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1896 pp_c_equality_expression (pp, e);
1899 /* exclusive-OR-expression:
1901 exclusive-OR-expression ^ AND-expression */
1904 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1906 if (TREE_CODE (e) == BIT_XOR_EXPR
1907 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1909 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1910 if (TREE_CODE (e) == BIT_XOR_EXPR)
1911 pp_c_maybe_whitespace (pp);
1913 pp_c_whitespace (pp);
1915 pp_c_whitespace (pp);
1916 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1919 pp_c_and_expression (pp, e);
1922 /* inclusive-OR-expression:
1923 exclusive-OR-expression
1924 inclusive-OR-expression | exclusive-OR-expression */
1927 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1929 if (TREE_CODE (e) == BIT_IOR_EXPR)
1931 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1932 pp_c_whitespace (pp);
1934 pp_c_whitespace (pp);
1935 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1938 pp_c_exclusive_or_expression (pp, e);
1941 /* logical-AND-expression:
1942 inclusive-OR-expression
1943 logical-AND-expression && inclusive-OR-expression */
1946 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1948 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
1949 || TREE_CODE (e) == TRUTH_AND_EXPR)
1951 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1952 pp_c_whitespace (pp);
1953 pp_string (pp, "&&");
1954 pp_c_whitespace (pp);
1955 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1958 pp_c_inclusive_or_expression (pp, e);
1961 /* logical-OR-expression:
1962 logical-AND-expression
1963 logical-OR-expression || logical-AND-expression */
1966 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1968 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
1969 || TREE_CODE (e) == TRUTH_OR_EXPR)
1971 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1972 pp_c_whitespace (pp);
1973 pp_string (pp, "||");
1974 pp_c_whitespace (pp);
1975 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1978 pp_c_logical_and_expression (pp, e);
1981 /* conditional-expression:
1982 logical-OR-expression
1983 logical-OR-expression ? expression : conditional-expression */
1986 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1988 if (TREE_CODE (e) == COND_EXPR)
1990 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1991 pp_c_whitespace (pp);
1993 pp_c_whitespace (pp);
1994 pp_expression (pp, TREE_OPERAND (e, 1));
1995 pp_c_whitespace (pp);
1997 pp_c_whitespace (pp);
1998 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
2001 pp_c_logical_or_expression (pp, e);
2005 /* assignment-expression:
2006 conditional-expression
2007 unary-expression assignment-operator assignment-expression
2009 assignment-expression: one of
2010 = *= /= %= += -= >>= <<= &= ^= |= */
2013 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
2015 if (TREE_CODE (e) == MODIFY_EXPR
2016 || TREE_CODE (e) == INIT_EXPR)
2018 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
2019 pp_c_whitespace (pp);
2022 pp_c_expression (pp, TREE_OPERAND (e, 1));
2025 pp_c_conditional_expression (pp, e);
2029 assignment-expression
2030 expression , assignment-expression
2032 Implementation note: instead of going through the usual recursion
2033 chain, I take the liberty of dispatching nodes to the appropriate
2034 functions. This makes some redundancy, but it worths it. That also
2035 prevents a possible infinite recursion between pp_c_primary_expression ()
2036 and pp_c_expression (). */
2039 pp_c_expression (c_pretty_printer *pp, tree e)
2041 switch (TREE_CODE (e))
2044 pp_c_integer_constant (pp, e);
2048 pp_c_floating_constant (pp, e);
2052 pp_c_fixed_constant (pp, e);
2056 pp_c_string_literal (pp, e);
2059 case IDENTIFIER_NODE:
2068 pp_primary_expression (pp, e);
2071 case POSTINCREMENT_EXPR:
2072 case POSTDECREMENT_EXPR:
2081 case UNORDERED_EXPR:
2090 case COMPOUND_LITERAL_EXPR:
2092 pp_postfix_expression (pp, e);
2101 case TRUTH_NOT_EXPR:
2102 case PREINCREMENT_EXPR:
2103 case PREDECREMENT_EXPR:
2106 pp_c_unary_expression (pp, e);
2110 case FIX_TRUNC_EXPR:
2112 case VIEW_CONVERT_EXPR:
2113 pp_c_cast_expression (pp, e);
2117 case TRUNC_MOD_EXPR:
2118 case TRUNC_DIV_EXPR:
2119 pp_multiplicative_expression (pp, e);
2124 pp_c_shift_expression (pp, e);
2131 pp_c_relational_expression (pp, e);
2135 pp_c_and_expression (pp, e);
2139 case TRUTH_XOR_EXPR:
2140 pp_c_exclusive_or_expression (pp, e);
2144 pp_c_inclusive_or_expression (pp, e);
2147 case TRUTH_ANDIF_EXPR:
2148 case TRUTH_AND_EXPR:
2149 pp_c_logical_and_expression (pp, e);
2152 case TRUTH_ORIF_EXPR:
2154 pp_c_logical_or_expression (pp, e);
2159 pp_c_equality_expression (pp, e);
2163 pp_conditional_expression (pp, e);
2166 case POINTER_PLUS_EXPR:
2169 pp_c_additive_expression (pp, e);
2174 pp_assignment_expression (pp, e);
2178 pp_c_left_paren (pp);
2179 pp_expression (pp, TREE_OPERAND (e, 0));
2180 pp_separate_with (pp, ',');
2181 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
2182 pp_c_right_paren (pp);
2185 case NON_LVALUE_EXPR:
2187 pp_expression (pp, TREE_OPERAND (e, 0));
2191 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
2196 /* We don't yet have a way of dumping statements in a
2197 human-readable format. */
2198 pp_string (pp, "({...})");
2201 case C_MAYBE_CONST_EXPR:
2202 pp_c_expression (pp, C_MAYBE_CONST_EXPR_EXPR (e));
2206 pp_unsupported_tree (pp, e);
2216 pp_c_statement (c_pretty_printer *pp, tree stmt)
2221 if (pp_needs_newline (pp))
2222 pp_newline_and_indent (pp, 0);
2224 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2228 /* Initialize the PRETTY-PRINTER for handling C codes. */
2231 pp_c_pretty_printer_init (c_pretty_printer *pp)
2233 pp->offset_list = 0;
2235 pp->declaration = pp_c_declaration;
2236 pp->declaration_specifiers = pp_c_declaration_specifiers;
2237 pp->declarator = pp_c_declarator;
2238 pp->direct_declarator = pp_c_direct_declarator;
2239 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2240 pp->abstract_declarator = pp_c_abstract_declarator;
2241 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2242 pp->ptr_operator = pp_c_pointer;
2243 pp->parameter_list = pp_c_parameter_type_list;
2244 pp->type_id = pp_c_type_id;
2245 pp->simple_type_specifier = pp_c_type_specifier;
2246 pp->function_specifier = pp_c_function_specifier;
2247 pp->storage_class_specifier = pp_c_storage_class_specifier;
2249 pp->statement = pp_c_statement;
2251 pp->constant = pp_c_constant;
2252 pp->id_expression = pp_c_id_expression;
2253 pp->primary_expression = pp_c_primary_expression;
2254 pp->postfix_expression = pp_c_postfix_expression;
2255 pp->unary_expression = pp_c_unary_expression;
2256 pp->initializer = pp_c_initializer;
2257 pp->multiplicative_expression = pp_c_multiplicative_expression;
2258 pp->conditional_expression = pp_c_conditional_expression;
2259 pp->assignment_expression = pp_c_assignment_expression;
2260 pp->expression = pp_c_expression;
2264 /* Print the tree T in full, on file FILE. */
2267 print_c_tree (FILE *file, tree t)
2269 static c_pretty_printer pp_rec;
2270 static bool initialized = 0;
2271 c_pretty_printer *pp = &pp_rec;
2276 pp_construct (pp_base (pp), NULL, 0);
2277 pp_c_pretty_printer_init (pp);
2278 pp_needs_newline (pp) = true;
2280 pp_base (pp)->buffer->stream = file;
2282 pp_statement (pp, t);
2288 /* Print the tree T in full, on stderr. */
2291 debug_c_tree (tree t)
2293 print_c_tree (stderr, t);
2294 fputc ('\n', stderr);
2297 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2298 up of T's memory address. */
2301 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2305 gcc_assert (DECL_P (t));
2308 name = IDENTIFIER_POINTER (DECL_NAME (t));
2311 static char xname[8];
2312 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2316 pp_c_identifier (pp, name);