1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "double-int.h"
36 #include "stor-layout.h"
39 #include "c-pretty-print.h"
40 #include "tree-pretty-print.h"
41 #include "tree-iterator.h"
42 #include "diagnostic.h"
43 #include "wide-int-print.h"
45 /* The pretty-printer code is primarily designed to closely follow
46 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
47 codes we used to have in the past. Following a structured
48 approach (preferably the official grammars) is believed to make it
49 much easier to add extensions and nifty pretty-printing effects that
50 takes expression or declaration contexts into account. */
53 #define pp_c_maybe_whitespace(PP) \
55 if ((PP)->padding == pp_before) \
56 pp_c_whitespace (PP); \
60 static void pp_c_char (c_pretty_printer *, int);
62 /* postfix-expression */
63 static void pp_c_initializer_list (c_pretty_printer *, tree);
64 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
66 static void pp_c_additive_expression (c_pretty_printer *, tree);
67 static void pp_c_shift_expression (c_pretty_printer *, tree);
68 static void pp_c_relational_expression (c_pretty_printer *, tree);
69 static void pp_c_equality_expression (c_pretty_printer *, tree);
70 static void pp_c_and_expression (c_pretty_printer *, tree);
71 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
72 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
73 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
78 /* Helper functions. */
81 pp_c_whitespace (c_pretty_printer *pp)
84 pp->padding = pp_none;
88 pp_c_left_paren (c_pretty_printer *pp)
91 pp->padding = pp_none;
95 pp_c_right_paren (c_pretty_printer *pp)
98 pp->padding = pp_none;
102 pp_c_left_brace (c_pretty_printer *pp)
105 pp->padding = pp_none;
109 pp_c_right_brace (c_pretty_printer *pp)
112 pp->padding = pp_none;
116 pp_c_left_bracket (c_pretty_printer *pp)
118 pp_left_bracket (pp);
119 pp->padding = pp_none;
123 pp_c_right_bracket (c_pretty_printer *pp)
125 pp_right_bracket (pp);
126 pp->padding = pp_none;
130 pp_c_dot (c_pretty_printer *pp)
133 pp->padding = pp_none;
137 pp_c_ampersand (c_pretty_printer *pp)
140 pp->padding = pp_none;
144 pp_c_star (c_pretty_printer *pp)
147 pp->padding = pp_none;
151 pp_c_arrow (c_pretty_printer *pp)
154 pp->padding = pp_none;
158 pp_c_semicolon (c_pretty_printer *pp)
161 pp->padding = pp_none;
165 pp_c_complement (c_pretty_printer *pp)
168 pp->padding = pp_none;
172 pp_c_exclamation (c_pretty_printer *pp)
175 pp->padding = pp_none;
178 /* Print out the external representation of QUALIFIERS. */
181 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
183 const char *p = pp_last_position_in_text (pp);
184 bool previous = false;
189 /* The C programming language does not have references, but it is much
190 simpler to handle those here rather than going through the same
191 logic in the C++ pretty-printer. */
192 if (p != NULL && (*p == '*' || *p == '&'))
193 pp_c_whitespace (pp);
195 if (qualifiers & TYPE_QUAL_ATOMIC)
197 pp_c_ws_string (pp, "_Atomic");
201 if (qualifiers & TYPE_QUAL_CONST)
204 pp_c_whitespace (pp);
205 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
209 if (qualifiers & TYPE_QUAL_VOLATILE)
212 pp_c_whitespace (pp);
213 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
217 if (qualifiers & TYPE_QUAL_RESTRICT)
220 pp_c_whitespace (pp);
221 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
222 ? "restrict" : "__restrict__"));
226 /* Pretty-print T using the type-cast notation '( type-name )'. */
229 pp_c_type_cast (c_pretty_printer *pp, tree t)
231 pp_c_left_paren (pp);
233 pp_c_right_paren (pp);
236 /* We're about to pretty-print a pointer type as indicated by T.
237 Output a whitespace, if needed, preparing for subsequent output. */
240 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
242 if (POINTER_TYPE_P (t))
244 tree pointee = strip_pointer_operator (TREE_TYPE (t));
245 if (TREE_CODE (pointee) != ARRAY_TYPE
246 && TREE_CODE (pointee) != FUNCTION_TYPE)
247 pp_c_whitespace (pp);
254 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
255 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
256 of its type. Take care of possible extensions.
260 type-qualifier-list type-qualifier
265 __restrict__ -- GNU C
266 address-space-qualifier -- GNU C
270 address-space-qualifier:
271 identifier -- GNU C */
274 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
278 if (!t || t == error_mark_node)
284 qualifiers = TYPE_QUALS (t);
285 pp_c_cv_qualifiers (pp, qualifiers,
286 TREE_CODE (t) == FUNCTION_TYPE);
288 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
290 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
291 pp_c_identifier (pp, as);
296 * type-qualifier-list(opt)
297 * type-qualifier-list(opt) pointer */
300 pp_c_pointer (c_pretty_printer *pp, tree t)
302 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
304 switch (TREE_CODE (t))
307 /* It is easier to handle C++ reference types here. */
309 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
310 pp_c_pointer (pp, TREE_TYPE (t));
311 if (TREE_CODE (t) == POINTER_TYPE)
315 pp_c_type_qualifier_list (pp, t);
318 /* ??? This node is now in GENERIC and so shouldn't be here. But
319 we'll fix that later. */
321 pp->declaration (DECL_EXPR_DECL (t));
322 pp_needs_newline (pp) = true;
326 pp_unsupported_tree (pp, t);
330 /* simple-type-specifier:
346 struct-or-union-specifier
351 simple-type-specifier:
356 c_pretty_printer::simple_type_specifier (tree t)
358 const enum tree_code code = TREE_CODE (t);
362 translate_string ("<type-error>");
365 case IDENTIFIER_NODE:
366 pp_c_identifier (this, IDENTIFIER_POINTER (t));
373 case FIXED_POINT_TYPE:
377 simple_type_specifier (t);
381 int prec = TYPE_PRECISION (t);
382 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
383 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
385 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
388 simple_type_specifier (t);
389 if (TYPE_PRECISION (t) != prec)
392 pp_decimal_int (this, prec);
400 translate_string (TYPE_UNSIGNED (t)
401 ? "<unnamed-unsigned:"
402 : "<unnamed-signed:");
405 translate_string ("<unnamed-float:");
407 case FIXED_POINT_TYPE:
408 translate_string ("<unnamed-fixed:");
413 pp_decimal_int (this, prec);
423 translate_string ("<typedef-error>");
429 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
430 /* Don't decorate the type if this is a typedef name. */;
431 else if (code == UNION_TYPE)
432 pp_c_ws_string (this, "union");
433 else if (code == RECORD_TYPE)
434 pp_c_ws_string (this, "struct");
435 else if (code == ENUMERAL_TYPE)
436 pp_c_ws_string (this, "enum");
438 translate_string ("<tag-error>");
441 id_expression (TYPE_NAME (t));
443 translate_string ("<anonymous>");
447 pp_unsupported_tree (this, t);
452 /* specifier-qualifier-list:
453 type-specifier specifier-qualifier-list-opt
454 type-qualifier specifier-qualifier-list-opt
457 Implementation note: Because of the non-linearities in array or
458 function declarations, this routine prints not just the
459 specifier-qualifier-list of such entities or types of such entities,
460 but also the 'pointer' production part of their declarators. The
461 remaining part is done by declarator() or abstract_declarator(). */
464 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
466 const enum tree_code code = TREE_CODE (t);
468 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
469 pp_c_type_qualifier_list (pp, t);
475 /* Get the types-specifier of this type. */
476 tree pointee = strip_pointer_operator (TREE_TYPE (t));
477 pp_c_specifier_qualifier_list (pp, pointee);
478 if (TREE_CODE (pointee) == ARRAY_TYPE
479 || TREE_CODE (pointee) == FUNCTION_TYPE)
481 pp_c_whitespace (pp);
482 pp_c_left_paren (pp);
483 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
485 else if (!c_dialect_cxx ())
486 pp_c_whitespace (pp);
487 pp_ptr_operator (pp, t);
493 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
498 if (code == COMPLEX_TYPE)
499 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
500 ? "_Complex" : "__complex__"));
501 else if (code == VECTOR_TYPE)
503 pp_c_ws_string (pp, "__vector");
504 pp_c_left_paren (pp);
505 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
506 pp_c_right_paren (pp);
507 pp_c_whitespace (pp);
509 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
513 pp->simple_type_specifier (t);
516 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
517 pp_c_type_qualifier_list (pp, t);
520 /* parameter-type-list:
525 parameter-declaration
526 parameter-list , parameter-declaration
528 parameter-declaration:
529 declaration-specifiers declarator
530 declaration-specifiers abstract-declarator(opt) */
533 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
535 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
536 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
537 pp_c_left_paren (pp);
538 if (parms == void_list_node)
539 pp_c_ws_string (pp, "void");
543 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
546 pp_separate_with (pp, ',');
548 pp->declaration_specifiers
549 (want_parm_decl ? parms : TREE_VALUE (parms));
551 pp->declarator (parms);
553 pp->abstract_declarator (TREE_VALUE (parms));
556 pp_c_right_paren (pp);
559 /* abstract-declarator:
561 pointer(opt) direct-abstract-declarator */
564 c_pretty_printer::abstract_declarator (tree t)
566 if (TREE_CODE (t) == POINTER_TYPE)
568 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
569 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
570 pp_c_right_paren (this);
574 direct_abstract_declarator (t);
577 /* direct-abstract-declarator:
578 ( abstract-declarator )
579 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
580 direct-abstract-declarator(opt) [ * ]
581 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
584 c_pretty_printer::direct_abstract_declarator (tree t)
586 switch (TREE_CODE (t))
589 abstract_declarator (t);
593 pp_c_parameter_type_list (this, t);
594 direct_abstract_declarator (TREE_TYPE (t));
598 pp_c_left_bracket (this);
599 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
601 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
602 tree type = TREE_TYPE (maxval);
604 if (tree_fits_shwi_p (maxval))
605 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
607 expression (fold_build2 (PLUS_EXPR, type, maxval,
608 build_int_cst (type, 1)));
610 pp_c_right_bracket (this);
611 direct_abstract_declarator (TREE_TYPE (t));
614 case IDENTIFIER_NODE:
619 case FIXED_POINT_TYPE:
629 pp_unsupported_tree (this, t);
635 specifier-qualifier-list abstract-declarator(opt) */
638 c_pretty_printer::type_id (tree t)
640 pp_c_specifier_qualifier_list (this, t);
641 abstract_declarator (t);
644 /* storage-class-specifier:
652 c_pretty_printer::storage_class_specifier (tree t)
654 if (TREE_CODE (t) == TYPE_DECL)
655 pp_c_ws_string (this, "typedef");
658 if (DECL_REGISTER (t))
659 pp_c_ws_string (this, "register");
660 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
661 pp_c_ws_string (this, "static");
665 /* function-specifier:
669 c_pretty_printer::function_specifier (tree t)
671 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
672 pp_c_ws_string (this, "inline");
675 /* declaration-specifiers:
676 storage-class-specifier declaration-specifiers(opt)
677 type-specifier declaration-specifiers(opt)
678 type-qualifier declaration-specifiers(opt)
679 function-specifier declaration-specifiers(opt) */
682 c_pretty_printer::declaration_specifiers (tree t)
684 storage_class_specifier (t);
685 function_specifier (t);
686 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
692 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
693 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
694 direct-declarator [ type-qualifier-list static assignment-expression ]
695 direct-declarator [ type-qualifier-list * ]
696 direct-declarator ( parameter-type-list )
697 direct-declarator ( identifier-list(opt) ) */
700 c_pretty_printer::direct_declarator (tree t)
702 switch (TREE_CODE (t))
709 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
710 pp_c_tree_decl_identifier (this, t);
715 abstract_declarator (TREE_TYPE (t));
719 pp_parameter_list (this, t);
720 abstract_declarator (TREE_TYPE (t));
724 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
725 pp_c_tree_decl_identifier (this, t);
726 if (flags & pp_c_flag_abstract)
727 abstract_declarator (TREE_TYPE (t));
730 pp_parameter_list (this, t);
731 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
737 case FIXED_POINT_TYPE:
744 pp_unsupported_tree (this, t);
751 pointer(opt) direct-declarator */
754 c_pretty_printer::declarator (tree t)
756 switch (TREE_CODE (t))
760 case FIXED_POINT_TYPE:
773 direct_declarator (t);
778 pp_unsupported_tree (this, t);
784 declaration-specifiers init-declarator-list(opt) ; */
787 c_pretty_printer::declaration (tree t)
789 declaration_specifiers (t);
790 pp_c_init_declarator (this, t);
793 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
796 pp_c_attributes (c_pretty_printer *pp, tree attributes)
798 if (attributes == NULL_TREE)
801 pp_c_ws_string (pp, "__attribute__");
802 pp_c_left_paren (pp);
803 pp_c_left_paren (pp);
804 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
806 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
807 if (TREE_VALUE (attributes))
808 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
810 if (TREE_CHAIN (attributes))
811 pp_separate_with (pp, ',');
813 pp_c_right_paren (pp);
814 pp_c_right_paren (pp);
817 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
818 marked to be displayed on disgnostic. */
821 pp_c_attributes_display (c_pretty_printer *pp, tree a)
823 bool is_first = true;
828 for (; a != NULL_TREE; a = TREE_CHAIN (a))
830 const struct attribute_spec *as;
831 as = lookup_attribute_spec (TREE_PURPOSE (a));
832 if (!as || as->affects_type_identity == false)
836 pp_c_ws_string (pp, "__attribute__");
837 pp_c_left_paren (pp);
838 pp_c_left_paren (pp);
843 pp_separate_with (pp, ',');
845 pp_tree_identifier (pp, TREE_PURPOSE (a));
847 pp_c_call_argument_list (pp, TREE_VALUE (a));
852 pp_c_right_paren (pp);
853 pp_c_right_paren (pp);
854 pp_c_whitespace (pp);
858 /* function-definition:
859 declaration-specifiers declarator compound-statement */
862 pp_c_function_definition (c_pretty_printer *pp, tree t)
864 pp->declaration_specifiers (t);
866 pp_needs_newline (pp) = true;
867 pp->statement (DECL_SAVED_TREE (t));
868 pp_newline_and_flush (pp);
874 /* Print out a c-char. This is called solely for characters which are
875 in the *target* execution character set. We ought to convert them
876 back to the *host* execution character set before printing, but we
877 have no way to do this at present. A decent compromise is to print
878 all characters as if they were in the host execution character set,
879 and not attempt to recover any named escape characters, but render
880 all unprintables as octal escapes. If the host and target character
881 sets are the same, this produces relatively readable output. If they
882 are not the same, strings may appear as gibberish, but that's okay
883 (in fact, it may well be what the reader wants, e.g. if they are looking
884 to see if conversion to the target character set happened correctly).
886 A special case: we need to prefix \, ", and ' with backslashes. It is
887 correct to do so for the *host*'s \, ", and ', because the rest of the
888 file appears in the host character set. */
891 pp_c_char (c_pretty_printer *pp, int c)
897 case '\\': pp_string (pp, "\\\\"); break;
898 case '\'': pp_string (pp, "\\\'"); break;
899 case '\"': pp_string (pp, "\\\""); break;
900 default: pp_character (pp, c);
904 pp_scalar (pp, "\\%03o", (unsigned) c);
907 /* Print out a STRING literal. */
910 pp_c_string_literal (c_pretty_printer *pp, tree s)
912 const char *p = TREE_STRING_POINTER (s);
913 int n = TREE_STRING_LENGTH (s) - 1;
916 for (i = 0; i < n; ++i)
917 pp_c_char (pp, p[i]);
921 /* Pretty-print a VOID_CST (void_node). */
924 pp_c_void_constant (c_pretty_printer *pp)
926 pp_c_type_cast (pp, void_type_node);
930 /* Pretty-print an INTEGER literal. */
933 pp_c_integer_constant (c_pretty_printer *pp, tree i)
937 /* We are going to compare the type of I to other types using
938 pointer comparison so we need to use its canonical type. */
940 TYPE_CANONICAL (TREE_TYPE (i))
941 ? TYPE_CANONICAL (TREE_TYPE (i))
944 if (tree_fits_shwi_p (i))
945 pp_wide_integer (pp, tree_to_shwi (i));
946 else if (tree_fits_uhwi_p (i))
947 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
952 if (wi::lt_p (i, 0, TYPE_SIGN (TREE_TYPE (i))))
957 print_hex (wi, pp_buffer (pp)->digit_buffer);
958 pp_string (pp, pp_buffer (pp)->digit_buffer);
960 if (TYPE_UNSIGNED (type))
961 pp_character (pp, 'u');
962 if (type == long_integer_type_node || type == long_unsigned_type_node)
963 pp_character (pp, 'l');
964 else if (type == long_long_integer_type_node
965 || type == long_long_unsigned_type_node)
966 pp_string (pp, "ll");
967 else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++)
968 if (int_n_enabled_p[idx])
971 if (type == int_n_trees[idx].signed_type
972 || type == int_n_trees[idx].unsigned_type)
974 sprintf (buf, "I%d", int_n_data[idx].bitsize);
980 /* Print out a CHARACTER literal. */
983 pp_c_character_constant (c_pretty_printer *pp, tree c)
986 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
990 /* Print out a BOOLEAN literal. */
993 pp_c_bool_constant (c_pretty_printer *pp, tree b)
995 if (b == boolean_false_node)
997 if (c_dialect_cxx ())
998 pp_c_ws_string (pp, "false");
999 else if (flag_isoc99)
1000 pp_c_ws_string (pp, "_False");
1002 pp_unsupported_tree (pp, b);
1004 else if (b == boolean_true_node)
1006 if (c_dialect_cxx ())
1007 pp_c_ws_string (pp, "true");
1008 else if (flag_isoc99)
1009 pp_c_ws_string (pp, "_True");
1011 pp_unsupported_tree (pp, b);
1013 else if (TREE_CODE (b) == INTEGER_CST)
1014 pp_c_integer_constant (pp, b);
1016 pp_unsupported_tree (pp, b);
1019 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
1020 false; that means the value was obtained by a cast, in which case
1021 print out the type-id part of the cast-expression -- the casted value
1022 is then printed by pp_c_integer_literal. */
1025 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1027 bool value_is_named = true;
1028 tree type = TREE_TYPE (e);
1031 /* Find the name of this constant. */
1032 for (value = TYPE_VALUES (type);
1033 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1034 value = TREE_CHAIN (value))
1037 if (value != NULL_TREE)
1038 pp->id_expression (TREE_PURPOSE (value));
1041 /* Value must have been cast. */
1042 pp_c_type_cast (pp, type);
1043 value_is_named = false;
1046 return value_is_named;
1049 /* Print out a REAL value as a decimal-floating-constant. */
1052 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1054 const struct real_format *fmt
1055 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1057 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1058 bool is_decimal = floating_cst.decimal;
1060 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1061 log10(2) to 7 significant digits. */
1062 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1064 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1065 sizeof (pp_buffer (pp)->digit_buffer),
1068 pp_string (pp, pp_buffer(pp)->digit_buffer);
1069 if (TREE_TYPE (r) == float_type_node)
1070 pp_character (pp, 'f');
1071 else if (TREE_TYPE (r) == long_double_type_node)
1072 pp_character (pp, 'l');
1073 else if (TREE_TYPE (r) == dfloat128_type_node)
1074 pp_string (pp, "dl");
1075 else if (TREE_TYPE (r) == dfloat64_type_node)
1076 pp_string (pp, "dd");
1077 else if (TREE_TYPE (r) == dfloat32_type_node)
1078 pp_string (pp, "df");
1081 /* Print out a FIXED value as a decimal-floating-constant. */
1084 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1086 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1087 sizeof (pp_buffer (pp)->digit_buffer));
1088 pp_string (pp, pp_buffer(pp)->digit_buffer);
1091 /* Pretty-print a compound literal expression. GNU extensions include
1092 vector constants. */
1095 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1097 tree type = TREE_TYPE (e);
1098 pp_c_type_cast (pp, type);
1100 switch (TREE_CODE (type))
1107 pp_c_brace_enclosed_initializer_list (pp, e);
1111 pp_unsupported_tree (pp, e);
1116 /* Pretty-print a COMPLEX_EXPR expression. */
1119 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1121 /* Handle a few common special cases, otherwise fallback
1122 to printing it as compound literal. */
1123 tree type = TREE_TYPE (e);
1124 tree realexpr = TREE_OPERAND (e, 0);
1125 tree imagexpr = TREE_OPERAND (e, 1);
1127 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1128 if (TREE_CODE (realexpr) == NOP_EXPR
1129 && TREE_CODE (imagexpr) == NOP_EXPR
1130 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1131 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1132 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1133 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1134 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1135 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1137 pp_c_type_cast (pp, type);
1138 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1142 /* Cast of an scalar expression to COMPLEX_TYPE. */
1143 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1144 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1146 pp_c_type_cast (pp, type);
1147 if (TREE_CODE (realexpr) == NOP_EXPR)
1148 realexpr = TREE_OPERAND (realexpr, 0);
1149 pp->expression (realexpr);
1153 pp_c_compound_literal (pp, e);
1159 fixed-point-constant
1160 enumeration-constant
1161 character-constant */
1164 c_pretty_printer::constant (tree e)
1166 const enum tree_code code = TREE_CODE (e);
1171 pp_c_void_constant (this);
1176 tree type = TREE_TYPE (e);
1177 if (type == boolean_type_node)
1178 pp_c_bool_constant (this, e);
1179 else if (type == char_type_node)
1180 pp_c_character_constant (this, e);
1181 else if (TREE_CODE (type) == ENUMERAL_TYPE
1182 && pp_c_enumeration_constant (this, e))
1185 pp_c_integer_constant (this, e);
1190 pp_c_floating_constant (this, e);
1194 pp_c_fixed_constant (this, e);
1198 pp_c_string_literal (this, e);
1202 /* Sometimes, we are confused and we think a complex literal
1203 is a constant. Such thing is a compound literal which
1204 grammatically belongs to postfix-expr production. */
1205 pp_c_compound_literal (this, e);
1209 pp_unsupported_tree (this, e);
1214 /* Pretty-print a string such as an identifier, without changing its
1215 encoding, preceded by whitespace is necessary. */
1218 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1220 pp_c_maybe_whitespace (pp);
1221 pp_string (pp, str);
1222 pp->padding = pp_before;
1226 c_pretty_printer::translate_string (const char *gmsgid)
1228 if (pp_translate_identifiers (this))
1229 pp_c_ws_string (this, _(gmsgid));
1231 pp_c_ws_string (this, gmsgid);
1234 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1235 that need converting to the locale encoding, preceded by whitespace
1239 pp_c_identifier (c_pretty_printer *pp, const char *id)
1241 pp_c_maybe_whitespace (pp);
1242 pp_identifier (pp, id);
1243 pp->padding = pp_before;
1246 /* Pretty-print a C primary-expression.
1254 c_pretty_printer::primary_expression (tree e)
1256 switch (TREE_CODE (e))
1264 pp_c_tree_decl_identifier (this, e);
1267 case IDENTIFIER_NODE:
1268 pp_c_tree_identifier (this, e);
1272 translate_string ("<erroneous-expression>");
1276 translate_string ("<return-value>");
1288 pp_c_ws_string (this, "__builtin_memcpy");
1289 pp_c_left_paren (this);
1290 pp_ampersand (this);
1291 primary_expression (TREE_OPERAND (e, 0));
1292 pp_separate_with (this, ',');
1293 pp_ampersand (this);
1294 initializer (TREE_OPERAND (e, 1));
1295 if (TREE_OPERAND (e, 2))
1297 pp_separate_with (this, ',');
1298 expression (TREE_OPERAND (e, 2));
1300 pp_c_right_paren (this);
1304 /* FIXME: Make sure we won't get into an infinite loop. */
1305 pp_c_left_paren (this);
1307 pp_c_right_paren (this);
1312 /* Print out a C initializer -- also support C compound-literals.
1314 assignment-expression:
1315 { initializer-list }
1316 { initializer-list , } */
1319 c_pretty_printer::initializer (tree e)
1321 if (TREE_CODE (e) == CONSTRUCTOR)
1322 pp_c_brace_enclosed_initializer_list (this, e);
1329 declarator = initializer */
1332 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1335 /* We don't want to output function definitions here. There are handled
1336 elsewhere (and the syntactic form is bogus anyway). */
1337 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1339 tree init = DECL_INITIAL (t);
1340 /* This C++ bit is handled here because it is easier to do so.
1341 In templates, the C++ parser builds a TREE_LIST for a
1342 direct-initialization; the TREE_PURPOSE is the variable to
1343 initialize and the TREE_VALUE is the initializer. */
1344 if (TREE_CODE (init) == TREE_LIST)
1346 pp_c_left_paren (pp);
1347 pp->expression (TREE_VALUE (init));
1348 pp_right_paren (pp);
1355 pp->initializer (init);
1360 /* initializer-list:
1361 designation(opt) initializer
1362 initializer-list , designation(opt) initializer
1369 designator-list designator
1372 [ constant-expression ]
1376 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1378 tree type = TREE_TYPE (e);
1379 const enum tree_code code = TREE_CODE (type);
1381 if (TREE_CODE (e) == CONSTRUCTOR)
1383 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1393 tree init = TREE_OPERAND (e, 0);
1394 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1396 if (code == RECORD_TYPE || code == UNION_TYPE)
1399 pp->primary_expression (TREE_PURPOSE (init));
1403 pp_c_left_bracket (pp);
1404 if (TREE_PURPOSE (init))
1405 pp->constant (TREE_PURPOSE (init));
1406 pp_c_right_bracket (pp);
1408 pp_c_whitespace (pp);
1410 pp_c_whitespace (pp);
1411 pp->initializer (TREE_VALUE (init));
1412 if (TREE_CHAIN (init))
1413 pp_separate_with (pp, ',');
1419 if (TREE_CODE (e) == VECTOR_CST)
1422 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1425 pp_separate_with (pp, ',');
1426 pp->expression (VECTOR_CST_ELT (e, i));
1434 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1436 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1437 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1438 pp_separate_with (pp, ',');
1439 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1449 pp_unsupported_tree (pp, type);
1452 /* Pretty-print a brace-enclosed initializer-list. */
1455 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1457 pp_c_left_brace (pp);
1458 pp_c_initializer_list (pp, l);
1459 pp_c_right_brace (pp);
1463 /* This is a convenient function, used to bridge gap between C and C++
1470 c_pretty_printer::id_expression (tree t)
1472 switch (TREE_CODE (t))
1481 pp_c_tree_decl_identifier (this, t);
1484 case IDENTIFIER_NODE:
1485 pp_c_tree_identifier (this, t);
1489 pp_unsupported_tree (this, t);
1494 /* postfix-expression:
1496 postfix-expression [ expression ]
1497 postfix-expression ( argument-expression-list(opt) )
1498 postfix-expression . identifier
1499 postfix-expression -> identifier
1500 postfix-expression ++
1501 postfix-expression --
1502 ( type-name ) { initializer-list }
1503 ( type-name ) { initializer-list , } */
1506 c_pretty_printer::postfix_expression (tree e)
1508 enum tree_code code = TREE_CODE (e);
1511 case POSTINCREMENT_EXPR:
1512 case POSTDECREMENT_EXPR:
1513 postfix_expression (TREE_OPERAND (e, 0));
1514 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1518 postfix_expression (TREE_OPERAND (e, 0));
1519 pp_c_left_bracket (this);
1520 expression (TREE_OPERAND (e, 1));
1521 pp_c_right_bracket (this);
1524 case ARRAY_NOTATION_REF:
1525 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1526 pp_c_left_bracket (this);
1527 expression (ARRAY_NOTATION_START (e));
1529 expression (ARRAY_NOTATION_LENGTH (e));
1531 expression (ARRAY_NOTATION_STRIDE (e));
1532 pp_c_right_bracket (this);
1537 call_expr_arg_iterator iter;
1539 postfix_expression (CALL_EXPR_FN (e));
1540 pp_c_left_paren (this);
1541 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1544 if (more_call_expr_args_p (&iter))
1545 pp_separate_with (this, ',');
1547 pp_c_right_paren (this);
1551 case UNORDERED_EXPR:
1552 pp_c_ws_string (this, flag_isoc99
1554 : "__builtin_isunordered");
1558 pp_c_ws_string (this, flag_isoc99
1560 : "!__builtin_isunordered");
1564 pp_c_ws_string (this, flag_isoc99
1566 : "!__builtin_isgreaterequal");
1570 pp_c_ws_string (this, flag_isoc99
1572 : "!__builtin_isgreater");
1576 pp_c_ws_string (this, flag_isoc99
1578 : "!__builtin_islessequal");
1582 pp_c_ws_string (this, flag_isoc99
1584 : "!__builtin_isless");
1588 pp_c_ws_string (this, flag_isoc99
1590 : "!__builtin_islessgreater");
1594 pp_c_ws_string (this, flag_isoc99
1596 : "__builtin_islessgreater");
1600 pp_c_left_paren (this);
1601 expression (TREE_OPERAND (e, 0));
1602 pp_separate_with (this, ',');
1603 expression (TREE_OPERAND (e, 1));
1604 pp_c_right_paren (this);
1608 pp_c_ws_string (this, "__builtin_abs");
1609 pp_c_left_paren (this);
1610 expression (TREE_OPERAND (e, 0));
1611 pp_c_right_paren (this);
1616 tree object = TREE_OPERAND (e, 0);
1617 if (TREE_CODE (object) == INDIRECT_REF)
1619 postfix_expression (TREE_OPERAND (object, 0));
1624 postfix_expression (object);
1627 expression (TREE_OPERAND (e, 1));
1633 tree type = TREE_TYPE (e);
1635 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1637 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1639 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1640 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1641 if ((bitpos % size) == 0)
1643 pp_c_left_paren (this);
1644 pp_c_left_paren (this);
1647 pp_c_right_paren (this);
1648 pp_c_ampersand (this);
1649 expression (TREE_OPERAND (e, 0));
1650 pp_c_right_paren (this);
1651 pp_c_left_bracket (this);
1652 pp_wide_integer (this, bitpos / size);
1653 pp_c_right_bracket (this);
1657 pp_unsupported_tree (this, e);
1667 pp_c_compound_literal (this, e);
1671 pp_c_complex_expr (this, e);
1674 case COMPOUND_LITERAL_EXPR:
1675 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1682 pp_c_ws_string (this, "__builtin_va_arg");
1683 pp_c_left_paren (this);
1684 assignment_expression (TREE_OPERAND (e, 0));
1685 pp_separate_with (this, ',');
1686 type_id (TREE_TYPE (e));
1687 pp_c_right_paren (this);
1691 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1693 id_expression (TREE_OPERAND (e, 0));
1696 /* else fall through. */
1699 primary_expression (e);
1704 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1707 pp_c_expression_list (c_pretty_printer *pp, tree e)
1709 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1711 pp->expression (TREE_VALUE (e));
1713 pp_separate_with (pp, ',');
1717 /* Print out V, which contains the elements of a constructor. */
1720 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1722 unsigned HOST_WIDE_INT ix;
1725 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1727 pp->expression (value);
1728 if (ix != vec_safe_length (v) - 1)
1729 pp_separate_with (pp, ',');
1733 /* Print out an expression-list in parens, as if it were the argument
1734 list to a function. */
1737 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1739 pp_c_left_paren (pp);
1740 if (t && TREE_CODE (t) == TREE_LIST)
1741 pp_c_expression_list (pp, t);
1742 pp_c_right_paren (pp);
1745 /* unary-expression:
1749 unary-operator cast-expression
1750 sizeof unary-expression
1753 unary-operator: one of
1758 __alignof__ unary-expression
1759 __alignof__ ( type-id )
1760 __real__ unary-expression
1761 __imag__ unary-expression */
1764 c_pretty_printer::unary_expression (tree e)
1766 enum tree_code code = TREE_CODE (e);
1769 case PREINCREMENT_EXPR:
1770 case PREDECREMENT_EXPR:
1771 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1772 unary_expression (TREE_OPERAND (e, 0));
1779 case TRUTH_NOT_EXPR:
1781 /* String literal are used by address. */
1782 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1783 pp_ampersand (this);
1784 else if (code == INDIRECT_REF)
1786 else if (code == NEGATE_EXPR)
1788 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1789 pp_complement (this);
1790 else if (code == TRUTH_NOT_EXPR)
1791 pp_exclamation (this);
1792 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1796 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1797 && integer_zerop (TREE_OPERAND (e, 1)))
1798 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1802 if (!integer_zerop (TREE_OPERAND (e, 1)))
1804 pp_c_left_paren (this);
1805 if (!integer_onep (TYPE_SIZE_UNIT
1806 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1807 pp_c_type_cast (this, ptr_type_node);
1809 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1810 if (!integer_zerop (TREE_OPERAND (e, 1)))
1813 pp_c_integer_constant (this,
1814 fold_convert (ssizetype,
1815 TREE_OPERAND (e, 1)));
1816 pp_c_right_paren (this);
1823 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1824 pp_c_whitespace (this);
1825 unary_expression (TREE_OPERAND (e, 0));
1829 postfix_expression (e);
1836 ( type-name ) cast-expression */
1839 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1841 switch (TREE_CODE (e))
1844 case FIX_TRUNC_EXPR:
1846 case VIEW_CONVERT_EXPR:
1847 pp_c_type_cast (pp, TREE_TYPE (e));
1848 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1852 pp->unary_expression (e);
1856 /* multiplicative-expression:
1858 multiplicative-expression * cast-expression
1859 multiplicative-expression / cast-expression
1860 multiplicative-expression % cast-expression */
1863 c_pretty_printer::multiplicative_expression (tree e)
1865 enum tree_code code = TREE_CODE (e);
1869 case TRUNC_DIV_EXPR:
1870 case TRUNC_MOD_EXPR:
1871 multiplicative_expression (TREE_OPERAND (e, 0));
1872 pp_c_whitespace (this);
1873 if (code == MULT_EXPR)
1875 else if (code == TRUNC_DIV_EXPR)
1879 pp_c_whitespace (this);
1880 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1884 pp_c_cast_expression (this, e);
1889 /* additive-expression:
1890 multiplicative-expression
1891 additive-expression + multiplicative-expression
1892 additive-expression - multiplicative-expression */
1895 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1897 enum tree_code code = TREE_CODE (e);
1900 case POINTER_PLUS_EXPR:
1903 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1904 pp_c_whitespace (pp);
1905 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1909 pp_c_whitespace (pp);
1910 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1914 pp->multiplicative_expression (e);
1919 /* additive-expression:
1921 shift-expression << additive-expression
1922 shift-expression >> additive-expression */
1925 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1927 enum tree_code code = TREE_CODE (e);
1932 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1933 pp_c_whitespace (pp);
1934 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1935 pp_c_whitespace (pp);
1936 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1940 pp_c_additive_expression (pp, e);
1944 /* relational-expression:
1946 relational-expression < shift-expression
1947 relational-expression > shift-expression
1948 relational-expression <= shift-expression
1949 relational-expression >= shift-expression */
1952 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1954 enum tree_code code = TREE_CODE (e);
1961 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1962 pp_c_whitespace (pp);
1963 if (code == LT_EXPR)
1965 else if (code == GT_EXPR)
1967 else if (code == LE_EXPR)
1969 else if (code == GE_EXPR)
1970 pp_greater_equal (pp);
1971 pp_c_whitespace (pp);
1972 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1976 pp_c_shift_expression (pp, e);
1981 /* equality-expression:
1982 relational-expression
1983 equality-expression == relational-expression
1984 equality-equality != relational-expression */
1987 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1989 enum tree_code code = TREE_CODE (e);
1994 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1995 pp_c_whitespace (pp);
1996 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1997 pp_c_whitespace (pp);
1998 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
2002 pp_c_relational_expression (pp, e);
2009 AND-expression & equality-equality */
2012 pp_c_and_expression (c_pretty_printer *pp, tree e)
2014 if (TREE_CODE (e) == BIT_AND_EXPR)
2016 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2017 pp_c_whitespace (pp);
2019 pp_c_whitespace (pp);
2020 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2023 pp_c_equality_expression (pp, e);
2026 /* exclusive-OR-expression:
2028 exclusive-OR-expression ^ AND-expression */
2031 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2033 if (TREE_CODE (e) == BIT_XOR_EXPR
2034 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2036 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2037 if (TREE_CODE (e) == BIT_XOR_EXPR)
2038 pp_c_maybe_whitespace (pp);
2040 pp_c_whitespace (pp);
2042 pp_c_whitespace (pp);
2043 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2046 pp_c_and_expression (pp, e);
2049 /* inclusive-OR-expression:
2050 exclusive-OR-expression
2051 inclusive-OR-expression | exclusive-OR-expression */
2054 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2056 if (TREE_CODE (e) == BIT_IOR_EXPR)
2058 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2059 pp_c_whitespace (pp);
2061 pp_c_whitespace (pp);
2062 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2065 pp_c_exclusive_or_expression (pp, e);
2068 /* logical-AND-expression:
2069 inclusive-OR-expression
2070 logical-AND-expression && inclusive-OR-expression */
2073 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2075 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2076 || TREE_CODE (e) == TRUTH_AND_EXPR)
2078 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2079 pp_c_whitespace (pp);
2080 pp_ampersand_ampersand (pp);
2081 pp_c_whitespace (pp);
2082 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2085 pp_c_inclusive_or_expression (pp, e);
2088 /* logical-OR-expression:
2089 logical-AND-expression
2090 logical-OR-expression || logical-AND-expression */
2093 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2095 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2096 || TREE_CODE (e) == TRUTH_OR_EXPR)
2098 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2099 pp_c_whitespace (pp);
2101 pp_c_whitespace (pp);
2102 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2105 pp_c_logical_and_expression (pp, e);
2108 /* conditional-expression:
2109 logical-OR-expression
2110 logical-OR-expression ? expression : conditional-expression */
2113 c_pretty_printer::conditional_expression (tree e)
2115 if (TREE_CODE (e) == COND_EXPR)
2117 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2118 pp_c_whitespace (this);
2120 pp_c_whitespace (this);
2121 expression (TREE_OPERAND (e, 1));
2122 pp_c_whitespace (this);
2124 pp_c_whitespace (this);
2125 conditional_expression (TREE_OPERAND (e, 2));
2128 pp_c_logical_or_expression (this, e);
2132 /* assignment-expression:
2133 conditional-expression
2134 unary-expression assignment-operator assignment-expression
2136 assignment-expression: one of
2137 = *= /= %= += -= >>= <<= &= ^= |= */
2140 c_pretty_printer::assignment_expression (tree e)
2142 if (TREE_CODE (e) == MODIFY_EXPR
2143 || TREE_CODE (e) == INIT_EXPR)
2145 unary_expression (TREE_OPERAND (e, 0));
2146 pp_c_whitespace (this);
2149 expression (TREE_OPERAND (e, 1));
2152 conditional_expression (e);
2156 assignment-expression
2157 expression , assignment-expression
2159 Implementation note: instead of going through the usual recursion
2160 chain, I take the liberty of dispatching nodes to the appropriate
2161 functions. This makes some redundancy, but it worths it. That also
2162 prevents a possible infinite recursion between primary_expression ()
2163 and expression (). */
2166 c_pretty_printer::expression (tree e)
2168 switch (TREE_CODE (e))
2171 pp_c_void_constant (this);
2175 pp_c_integer_constant (this, e);
2179 pp_c_floating_constant (this, e);
2183 pp_c_fixed_constant (this, e);
2187 pp_c_string_literal (this, e);
2190 case IDENTIFIER_NODE:
2199 primary_expression (e);
2203 if (SSA_NAME_VAR (e)
2204 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2205 expression (SSA_NAME_VAR (e));
2207 translate_string ("<unknown>");
2210 case POSTINCREMENT_EXPR:
2211 case POSTDECREMENT_EXPR:
2213 case ARRAY_NOTATION_REF:
2221 case UNORDERED_EXPR:
2230 case COMPOUND_LITERAL_EXPR:
2232 postfix_expression (e);
2241 case TRUTH_NOT_EXPR:
2242 case PREINCREMENT_EXPR:
2243 case PREDECREMENT_EXPR:
2246 unary_expression (e);
2250 case FIX_TRUNC_EXPR:
2252 case VIEW_CONVERT_EXPR:
2253 pp_c_cast_expression (this, e);
2257 case TRUNC_MOD_EXPR:
2258 case TRUNC_DIV_EXPR:
2259 multiplicative_expression (e);
2264 pp_c_shift_expression (this, e);
2271 pp_c_relational_expression (this, e);
2275 pp_c_and_expression (this, e);
2279 case TRUTH_XOR_EXPR:
2280 pp_c_exclusive_or_expression (this, e);
2284 pp_c_inclusive_or_expression (this, e);
2287 case TRUTH_ANDIF_EXPR:
2288 case TRUTH_AND_EXPR:
2289 pp_c_logical_and_expression (this, e);
2292 case TRUTH_ORIF_EXPR:
2294 pp_c_logical_or_expression (this, e);
2299 pp_c_equality_expression (this, e);
2303 conditional_expression (e);
2306 case POINTER_PLUS_EXPR:
2309 pp_c_additive_expression (this, e);
2314 assignment_expression (e);
2318 pp_c_left_paren (this);
2319 expression (TREE_OPERAND (e, 0));
2320 pp_separate_with (this, ',');
2321 assignment_expression (TREE_OPERAND (e, 1));
2322 pp_c_right_paren (this);
2325 case NON_LVALUE_EXPR:
2327 expression (TREE_OPERAND (e, 0));
2331 postfix_expression (TREE_OPERAND (e, 1));
2336 /* We don't yet have a way of dumping statements in a
2337 human-readable format. */
2338 pp_string (this, "({...})");
2341 case C_MAYBE_CONST_EXPR:
2342 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2346 pp_unsupported_tree (this, e);
2356 c_pretty_printer::statement (tree stmt)
2361 if (pp_needs_newline (this))
2362 pp_newline_and_indent (this, 0);
2364 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2368 /* Initialize the PRETTY-PRINTER for handling C codes. */
2370 c_pretty_printer::c_pretty_printer ()
2371 : pretty_printer (),
2375 type_specifier_seq = pp_c_specifier_qualifier_list;
2376 ptr_operator = pp_c_pointer;
2377 parameter_list = pp_c_parameter_type_list;
2381 /* Print the tree T in full, on file FILE. */
2384 print_c_tree (FILE *file, tree t)
2386 c_pretty_printer pp;
2388 pp_needs_newline (&pp) = true;
2389 pp.buffer->stream = file;
2391 pp_newline_and_flush (&pp);
2394 /* Print the tree T in full, on stderr. */
2397 debug_c_tree (tree t)
2399 print_c_tree (stderr, t);
2400 fputc ('\n', stderr);
2403 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2404 up of T's memory address. */
2407 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2411 gcc_assert (DECL_P (t));
2414 name = IDENTIFIER_POINTER (DECL_NAME (t));
2417 static char xname[8];
2418 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2422 pp_c_identifier (pp, name);