1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "c-pretty-print.h"
27 /* The pretty-printer code is primarily designed to closely follow
28 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
29 codes we used to have in the past. Following a structured
30 approach (preferably the official grammars) is believed to make it
31 much easier to add extensions and nifty pretty-printing effects that
32 takes expression or declaration contexts into account. */
35 #define pp_c_maybe_whitespace(PP) \
37 if (pp_base (PP)->padding == pp_before) \
38 pp_c_whitespace (PP); \
42 static void pp_c_char (c_pretty_printer *, int);
44 /* postfix-expression */
45 static void pp_c_initializer_list (c_pretty_printer *, tree);
46 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
48 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
49 static void pp_c_additive_expression (c_pretty_printer *, tree);
50 static void pp_c_shift_expression (c_pretty_printer *, tree);
51 static void pp_c_relational_expression (c_pretty_printer *, tree);
52 static void pp_c_equality_expression (c_pretty_printer *, tree);
53 static void pp_c_and_expression (c_pretty_printer *, tree);
54 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
55 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
56 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
57 static void pp_c_conditional_expression (c_pretty_printer *, tree);
58 static void pp_c_assignment_expression (c_pretty_printer *, tree);
63 /* Helper functions. */
66 pp_c_whitespace (c_pretty_printer *pp)
69 pp_base (pp)->padding = pp_none;
73 pp_c_left_paren (c_pretty_printer *pp)
76 pp_base (pp)->padding = pp_none;
80 pp_c_right_paren (c_pretty_printer *pp)
83 pp_base (pp)->padding = pp_none;
87 pp_c_left_brace (c_pretty_printer *pp)
90 pp_base (pp)->padding = pp_none;
94 pp_c_right_brace (c_pretty_printer *pp)
97 pp_base (pp)->padding = pp_none;
101 pp_c_left_bracket (c_pretty_printer *pp)
103 pp_left_bracket (pp);
104 pp_base (pp)->padding = pp_none;
108 pp_c_right_bracket (c_pretty_printer *pp)
110 pp_right_bracket (pp);
111 pp_base (pp)->padding = pp_none;
115 pp_c_dot (c_pretty_printer *pp)
118 pp_base (pp)->padding = pp_none;
122 pp_c_ampersand (c_pretty_printer *pp)
125 pp_base (pp)->padding = pp_none;
129 pp_c_star (c_pretty_printer *pp)
132 pp_base (pp)->padding = pp_none;
136 pp_c_arrow (c_pretty_printer *pp)
139 pp_base (pp)->padding = pp_none;
143 pp_c_semicolon (c_pretty_printer *pp)
146 pp_base (pp)->padding = pp_none;
150 pp_c_complement (c_pretty_printer *pp)
153 pp_base (pp)->padding = pp_none;
157 pp_c_exclamation (c_pretty_printer *pp)
160 pp_base (pp)->padding = pp_none;
163 /* Print out the external representation of CV-QUALIFIER. */
166 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
168 const char *p = pp_last_position_in_text (pp);
169 /* The C programming language does not have references, but it is much
170 simpler to handle those here rather than going through the same
171 logic in the C++ pretty-printer. */
172 if (p != NULL && (*p == '*' || *p == '&'))
173 pp_c_whitespace (pp);
174 pp_c_identifier (pp, cv);
177 /* Pretty-print T using the type-cast notation '( type-name )'. */
180 pp_c_type_cast (c_pretty_printer *pp, tree t)
182 pp_c_left_paren (pp);
184 pp_c_right_paren (pp);
187 /* We're about to pretty-print a pointer type as indicated by T.
188 Output a whitespace, if needed, preparing for subsequent output. */
191 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
193 if (POINTER_TYPE_P (t))
195 tree pointee = strip_pointer_operator (TREE_TYPE (t));
196 if (TREE_CODE (pointee) != ARRAY_TYPE
197 && TREE_CODE (pointee) != FUNCTION_TYPE)
198 pp_c_whitespace (pp);
205 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
206 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
207 of its type. Take care of possible extensions.
211 type-qualifier-list type-qualifier
216 __restrict__ -- GNU C
220 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
227 qualifiers = TYPE_QUALS (t);
228 if (qualifiers & TYPE_QUAL_CONST)
229 pp_c_cv_qualifier (pp, "const");
230 if (qualifiers & TYPE_QUAL_VOLATILE)
231 pp_c_cv_qualifier (pp, "volatile");
232 if (qualifiers & TYPE_QUAL_RESTRICT)
233 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
237 * type-qualifier-list(opt)
238 * type-qualifier-list(opt) pointer */
241 pp_c_pointer (c_pretty_printer *pp, tree t)
243 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
245 switch (TREE_CODE (t))
248 /* It is easier to handle C++ reference types here. */
250 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
251 pp_c_pointer (pp, TREE_TYPE (t));
252 if (TREE_CODE (t) == POINTER_TYPE)
256 pp_c_type_qualifier_list (pp, t);
259 /* ??? This node is now in GENERIC and so shouldn't be here. But
260 we'll fix that later. */
262 pp_declaration (pp, DECL_EXPR_DECL (t));
263 pp_needs_newline (pp) = true;
267 pp_unsupported_tree (pp, t);
284 struct-or-union-specifier
289 simple-type-specifier:
294 pp_c_type_specifier (c_pretty_printer *pp, tree t)
296 const enum tree_code code = TREE_CODE (t);
300 pp_c_identifier (pp, "<type-error>");
303 case IDENTIFIER_NODE:
304 pp_c_tree_decl_identifier (pp, t);
315 pp_c_type_specifier (pp, t);
319 int prec = TYPE_PRECISION (t);
320 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
323 pp_c_type_specifier (pp, t);
324 if (TYPE_PRECISION (t) != prec)
327 pp_decimal_int (pp, prec);
335 pp_string (pp, (TYPE_UNSIGNED (t)
336 ? "<unnamed-unsigned:"
337 : "<unnamed-signed:"));
340 pp_string (pp, "<unnamed-float:");
345 pp_decimal_int (pp, prec);
353 pp_id_expression (pp, t);
355 pp_c_identifier (pp, "<typedef-error>");
361 if (code == UNION_TYPE)
362 pp_c_identifier (pp, "union");
363 else if (code == RECORD_TYPE)
364 pp_c_identifier (pp, "struct");
365 else if (code == ENUMERAL_TYPE)
366 pp_c_identifier (pp, "enum");
368 pp_c_identifier (pp, "<tag-error>");
371 pp_id_expression (pp, TYPE_NAME (t));
373 pp_c_identifier (pp, "<anonymous>");
377 pp_unsupported_tree (pp, t);
382 /* specifier-qualifier-list:
383 type-specifier specifier-qualifier-list-opt
384 type-qualifier specifier-qualifier-list-opt
387 Implementation note: Because of the non-linearities in array or
388 function declarations, this routine prints not just the
389 specifier-qualifier-list of such entities or types of such entities,
390 but also the 'pointer' production part of their declarators. The
391 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
394 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
396 const enum tree_code code = TREE_CODE (t);
398 if (TREE_CODE (t) != POINTER_TYPE)
399 pp_c_type_qualifier_list (pp, t);
405 /* Get the types-specifier of this type. */
406 tree pointee = strip_pointer_operator (TREE_TYPE (t));
407 pp_c_specifier_qualifier_list (pp, pointee);
408 if (TREE_CODE (pointee) == ARRAY_TYPE
409 || TREE_CODE (pointee) == FUNCTION_TYPE)
411 pp_c_whitespace (pp);
412 pp_c_left_paren (pp);
414 else if (!c_dialect_cxx ())
415 pp_c_whitespace (pp);
416 pp_ptr_operator (pp, t);
422 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
427 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
428 if (code == COMPLEX_TYPE)
429 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
430 else if (code == VECTOR_TYPE)
431 pp_c_identifier (pp, "__vector__");
435 pp_simple_type_specifier (pp, t);
440 /* parameter-type-list:
445 parameter-declaration
446 parameter-list , parameter-declaration
448 parameter-declaration:
449 declaration-specifiers declarator
450 declaration-specifiers abstract-declarator(opt) */
453 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
455 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
456 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
457 pp_c_left_paren (pp);
458 if (parms == void_list_node)
459 pp_c_identifier (pp, "void");
463 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
466 pp_separate_with (pp, ',');
468 pp_declaration_specifiers
469 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
471 pp_declarator (pp, parms);
473 pp_abstract_declarator (pp, TREE_VALUE (parms));
476 pp_c_right_paren (pp);
479 /* abstract-declarator:
481 pointer(opt) direct-abstract-declarator */
484 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
486 if (TREE_CODE (t) == POINTER_TYPE)
488 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
489 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
490 pp_c_right_paren (pp);
494 pp_direct_abstract_declarator (pp, t);
497 /* direct-abstract-declarator:
498 ( abstract-declarator )
499 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
500 direct-abstract-declarator(opt) [ * ]
501 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
504 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
506 switch (TREE_CODE (t))
509 pp_abstract_declarator (pp, t);
513 pp_c_parameter_type_list (pp, t);
514 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
518 pp_c_left_bracket (pp);
519 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
520 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
521 pp_c_right_bracket (pp);
522 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
525 case IDENTIFIER_NODE:
539 pp_unsupported_tree (pp, t);
545 specifier-qualifier-list abstract-declarator(opt) */
548 pp_c_type_id (c_pretty_printer *pp, tree t)
550 pp_c_specifier_qualifier_list (pp, t);
551 pp_abstract_declarator (pp, t);
554 /* storage-class-specifier:
562 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
564 if (TREE_CODE (t) == TYPE_DECL)
565 pp_c_identifier (pp, "typedef");
568 if (DECL_REGISTER (t))
569 pp_c_identifier (pp, "register");
570 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
571 pp_c_identifier (pp, "static");
575 /* function-specifier:
579 pp_c_function_specifier (c_pretty_printer *pp, tree t)
581 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
582 pp_c_identifier (pp, "inline");
585 /* declaration-specifiers:
586 storage-class-specifier declaration-specifiers(opt)
587 type-specifier declaration-specifiers(opt)
588 type-qualifier declaration-specifiers(opt)
589 function-specifier declaration-specifiers(opt) */
592 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
594 pp_storage_class_specifier (pp, t);
595 pp_function_specifier (pp, t);
596 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
602 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
603 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
604 direct-declarator [ type-qualifier-list static assignment-expression ]
605 direct-declarator [ type-qualifier-list * ]
606 direct-declarator ( parameter-type-list )
607 direct-declarator ( identifier-list(opt) ) */
610 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
612 switch (TREE_CODE (t))
619 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
620 pp_c_tree_decl_identifier (pp, t);
625 pp_abstract_declarator (pp, TREE_TYPE (t));
629 pp_parameter_list (pp, t);
630 pp_abstract_declarator (pp, TREE_TYPE (t));
634 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
635 pp_c_tree_decl_identifier (pp, t);
636 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
637 pp_abstract_declarator (pp, TREE_TYPE (t));
640 pp_parameter_list (pp, t);
641 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
653 pp_unsupported_tree (pp, t);
660 pointer(opt) direct-declarator */
663 pp_c_declarator (c_pretty_printer *pp, tree t)
665 switch (TREE_CODE (t))
681 pp_direct_declarator (pp, t);
686 pp_unsupported_tree (pp, t);
692 declaration-specifiers init-declarator-list(opt) ; */
695 pp_c_declaration (c_pretty_printer *pp, tree t)
697 pp_declaration_specifiers (pp, t);
698 pp_c_init_declarator (pp, t);
701 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
704 pp_c_attributes (c_pretty_printer *pp, tree attributes)
706 if (attributes == NULL_TREE)
709 pp_c_identifier (pp, "__attribute__");
710 pp_c_left_paren (pp);
711 pp_c_left_paren (pp);
712 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
714 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
715 if (TREE_VALUE (attributes))
716 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
718 if (TREE_CHAIN (attributes))
719 pp_separate_with (pp, ',');
721 pp_c_right_paren (pp);
722 pp_c_right_paren (pp);
725 /* function-definition:
726 declaration-specifiers declarator compound-statement */
729 pp_c_function_definition (c_pretty_printer *pp, tree t)
731 pp_declaration_specifiers (pp, t);
732 pp_declarator (pp, t);
733 pp_needs_newline (pp) = true;
734 pp_statement (pp, DECL_SAVED_TREE (t));
742 /* Print out a c-char. This is called solely for characters which are
743 in the *target* execution character set. We ought to convert them
744 back to the *host* execution character set before printing, but we
745 have no way to do this at present. A decent compromise is to print
746 all characters as if they were in the host execution character set,
747 and not attempt to recover any named escape characters, but render
748 all unprintables as octal escapes. If the host and target character
749 sets are the same, this produces relatively readable output. If they
750 are not the same, strings may appear as gibberish, but that's okay
751 (in fact, it may well be what the reader wants, e.g. if they are looking
752 to see if conversion to the target character set happened correctly).
754 A special case: we need to prefix \, ", and ' with backslashes. It is
755 correct to do so for the *host*'s \, ", and ', because the rest of the
756 file appears in the host character set. */
759 pp_c_char (c_pretty_printer *pp, int c)
765 case '\\': pp_string (pp, "\\\\"); break;
766 case '\'': pp_string (pp, "\\\'"); break;
767 case '\"': pp_string (pp, "\\\""); break;
768 default: pp_character (pp, c);
772 pp_scalar (pp, "\\%03o", (unsigned) c);
775 /* Print out a STRING literal. */
778 pp_c_string_literal (c_pretty_printer *pp, tree s)
780 const char *p = TREE_STRING_POINTER (s);
781 int n = TREE_STRING_LENGTH (s) - 1;
784 for (i = 0; i < n; ++i)
785 pp_c_char (pp, p[i]);
789 /* Pretty-print an INTEGER literal. */
792 pp_c_integer_constant (c_pretty_printer *pp, tree i)
794 tree type = TREE_TYPE (i);
796 if (TREE_INT_CST_HIGH (i) == 0)
797 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
800 if (tree_int_cst_sgn (i) < 0)
802 pp_character (pp, '-');
803 i = build_int_cst_wide (NULL_TREE,
804 -TREE_INT_CST_LOW (i),
805 ~TREE_INT_CST_HIGH (i)
806 + !TREE_INT_CST_LOW (i));
808 sprintf (pp_buffer (pp)->digit_buffer,
809 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
810 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
811 pp_string (pp, pp_buffer (pp)->digit_buffer);
813 if (TYPE_UNSIGNED (type))
814 pp_character (pp, 'u');
815 if (type == long_integer_type_node || type == long_unsigned_type_node)
816 pp_character (pp, 'l');
817 else if (type == long_long_integer_type_node
818 || type == long_long_unsigned_type_node)
819 pp_string (pp, "ll");
822 /* Print out a CHARACTER literal. */
825 pp_c_character_constant (c_pretty_printer *pp, tree c)
827 tree type = TREE_TYPE (c);
828 if (type == wchar_type_node)
829 pp_character (pp, 'L');
831 if (host_integerp (c, TYPE_UNSIGNED (type)))
832 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
834 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
838 /* Print out a BOOLEAN literal. */
841 pp_c_bool_constant (c_pretty_printer *pp, tree b)
843 if (b == boolean_false_node)
845 if (c_dialect_cxx ())
846 pp_c_identifier (pp, "false");
847 else if (flag_isoc99)
848 pp_c_identifier (pp, "_False");
850 pp_unsupported_tree (pp, b);
852 else if (b == boolean_true_node)
854 if (c_dialect_cxx ())
855 pp_c_identifier (pp, "true");
856 else if (flag_isoc99)
857 pp_c_identifier (pp, "_True");
859 pp_unsupported_tree (pp, b);
861 else if (TREE_CODE (b) == INTEGER_CST)
862 pp_c_integer_constant (pp, b);
864 pp_unsupported_tree (pp, b);
867 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
868 false; that means the value was obtained by a cast, in which case
869 print out the type-id part of the cast-expression -- the casted value
870 is then printed by pp_c_integer_literal. */
873 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
875 bool value_is_named = true;
876 tree type = TREE_TYPE (e);
879 /* Find the name of this constant. */
880 for (value = TYPE_VALUES (type);
881 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
882 value = TREE_CHAIN (value))
885 if (value != NULL_TREE)
886 pp_id_expression (pp, TREE_PURPOSE (value));
889 /* Value must have been cast. */
890 pp_c_type_cast (pp, type);
891 value_is_named = false;
894 return value_is_named;
897 /* Print out a REAL value as a decimal-floating-constant. */
900 pp_c_floating_constant (c_pretty_printer *pp, tree r)
902 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
903 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
904 pp_string (pp, pp_buffer(pp)->digit_buffer);
905 if (TREE_TYPE (r) == float_type_node)
906 pp_character (pp, 'f');
907 else if (TREE_TYPE (r) == long_double_type_node)
908 pp_character (pp, 'l');
911 /* Pretty-print a compound literal expression. GNU extensions include
915 pp_c_compound_literal (c_pretty_printer *pp, tree e)
917 tree type = TREE_TYPE (e);
918 pp_c_type_cast (pp, type);
920 switch (TREE_CODE (type))
927 pp_c_brace_enclosed_initializer_list (pp, e);
931 pp_unsupported_tree (pp, e);
940 character-constant */
943 pp_c_constant (c_pretty_printer *pp, tree e)
945 const enum tree_code code = TREE_CODE (e);
951 tree type = TREE_TYPE (e);
952 if (type == boolean_type_node)
953 pp_c_bool_constant (pp, e);
954 else if (type == char_type_node)
955 pp_c_character_constant (pp, e);
956 else if (TREE_CODE (type) == ENUMERAL_TYPE
957 && pp_c_enumeration_constant (pp, e))
960 pp_c_integer_constant (pp, e);
965 pp_c_floating_constant (pp, e);
969 pp_c_string_literal (pp, e);
973 pp_unsupported_tree (pp, e);
978 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
981 pp_c_identifier (c_pretty_printer *pp, const char *id)
983 pp_c_maybe_whitespace (pp);
984 pp_identifier (pp, id);
985 pp_base (pp)->padding = pp_before;
988 /* Pretty-print a C primary-expression.
996 pp_c_primary_expression (c_pretty_printer *pp, tree e)
998 switch (TREE_CODE (e))
1006 pp_c_tree_decl_identifier (pp, e);
1009 case IDENTIFIER_NODE:
1010 pp_c_tree_identifier (pp, e);
1014 pp_c_identifier (pp, "<erroneous-expression>");
1018 pp_c_identifier (pp, "<return-value>");
1024 pp_c_constant (pp, e);
1028 pp_c_identifier (pp, "__builtin_memcpy");
1029 pp_c_left_paren (pp);
1031 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1032 pp_separate_with (pp, ',');
1034 pp_initializer (pp, TREE_OPERAND (e, 1));
1035 if (TREE_OPERAND (e, 2))
1037 pp_separate_with (pp, ',');
1038 pp_c_expression (pp, TREE_OPERAND (e, 2));
1040 pp_c_right_paren (pp);
1044 /* FIXME: Make sure we won't get into an infinie loop. */
1045 pp_c_left_paren (pp);
1046 pp_expression (pp, e);
1047 pp_c_right_paren (pp);
1052 /* Print out a C initializer -- also support C compound-literals.
1054 assignment-expression:
1055 { initializer-list }
1056 { initializer-list , } */
1059 pp_c_initializer (c_pretty_printer *pp, tree e)
1061 if (TREE_CODE (e) == CONSTRUCTOR)
1062 pp_c_brace_enclosed_initializer_list (pp, e);
1064 pp_expression (pp, e);
1069 declarator = initializer */
1072 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1074 pp_declarator (pp, t);
1075 /* We don't want to output function definitions here. There are handled
1076 elsewhere (and the syntactic form is bogus anyway). */
1077 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1079 tree init = DECL_INITIAL (t);
1080 /* This C++ bit is handled here because it is easier to do so.
1081 In templates, the C++ parser builds a TREE_LIST for a
1082 direct-initialization; the TREE_PURPOSE is the variable to
1083 initialize and the TREE_VALUE is the initializer. */
1084 if (TREE_CODE (init) == TREE_LIST)
1086 pp_c_left_paren (pp);
1087 pp_expression (pp, TREE_VALUE (init));
1088 pp_right_paren (pp);
1095 pp_c_initializer (pp, init);
1100 /* initializer-list:
1101 designation(opt) initializer
1102 initializer-list , designation(opt) initializer
1109 designator-list designator
1112 [ constant-expression ]
1116 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1118 tree type = TREE_TYPE (e);
1119 const enum tree_code code = TREE_CODE (type);
1127 tree init = TREE_OPERAND (e, 0);
1128 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1130 if (code == RECORD_TYPE || code == UNION_TYPE)
1133 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1137 pp_c_left_bracket (pp);
1138 if (TREE_PURPOSE (init))
1139 pp_c_constant (pp, TREE_PURPOSE (init));
1140 pp_c_right_bracket (pp);
1142 pp_c_whitespace (pp);
1144 pp_c_whitespace (pp);
1145 pp_initializer (pp, TREE_VALUE (init));
1146 if (TREE_CHAIN (init))
1147 pp_separate_with (pp, ',');
1153 if (TREE_CODE (e) == VECTOR_CST)
1154 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1155 else if (TREE_CODE (e) == CONSTRUCTOR)
1156 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1162 if (TREE_CODE (e) == CONSTRUCTOR)
1163 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1164 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1166 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1167 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1168 pp_separate_with (pp, ',');
1169 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1179 pp_unsupported_tree (pp, type);
1182 /* Pretty-print a brace-enclosed initializer-list. */
1185 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1187 pp_c_left_brace (pp);
1188 pp_c_initializer_list (pp, l);
1189 pp_c_right_brace (pp);
1193 /* This is a convenient function, used to bridge gap between C and C++
1200 pp_c_id_expression (c_pretty_printer *pp, tree t)
1202 switch (TREE_CODE (t))
1211 pp_c_tree_decl_identifier (pp, t);
1214 case IDENTIFIER_NODE:
1215 pp_c_tree_identifier (pp, t);
1219 pp_unsupported_tree (pp, t);
1224 /* postfix-expression:
1226 postfix-expression [ expression ]
1227 postfix-expression ( argument-expression-list(opt) )
1228 postfix-expression . identifier
1229 postfix-expression -> identifier
1230 postfix-expression ++
1231 postfix-expression --
1232 ( type-name ) { initializer-list }
1233 ( type-name ) { initializer-list , } */
1236 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1238 enum tree_code code = TREE_CODE (e);
1241 case POSTINCREMENT_EXPR:
1242 case POSTDECREMENT_EXPR:
1243 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1244 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1248 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1249 pp_c_left_bracket (pp);
1250 pp_expression (pp, TREE_OPERAND (e, 1));
1251 pp_c_right_bracket (pp);
1255 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1256 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1259 case UNORDERED_EXPR:
1260 pp_c_identifier (pp, flag_isoc99
1262 : "__builtin_isunordered");
1266 pp_c_identifier (pp, flag_isoc99
1268 : "!__builtin_isunordered");
1272 pp_c_identifier (pp, flag_isoc99
1274 : "!__builtin_isgreaterequal");
1278 pp_c_identifier (pp, flag_isoc99
1280 : "!__builtin_isgreater");
1284 pp_c_identifier (pp, flag_isoc99
1286 : "!__builtin_islessequal");
1290 pp_c_identifier (pp, flag_isoc99
1292 : "!__builtin_isless");
1296 pp_c_identifier (pp, flag_isoc99
1298 : "!__builtin_islessgreater");
1302 pp_c_identifier (pp, flag_isoc99
1304 : "__builtin_islessgreater");
1308 pp_c_left_paren (pp);
1309 pp_expression (pp, TREE_OPERAND (e, 0));
1310 pp_separate_with (pp, ',');
1311 pp_expression (pp, TREE_OPERAND (e, 1));
1312 pp_c_right_paren (pp);
1316 pp_c_identifier (pp, "__builtin_abs");
1317 pp_c_left_paren (pp);
1318 pp_expression (pp, TREE_OPERAND (e, 0));
1319 pp_c_right_paren (pp);
1324 tree object = TREE_OPERAND (e, 0);
1325 if (TREE_CODE (object) == INDIRECT_REF)
1327 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1332 pp_postfix_expression (pp, object);
1335 pp_expression (pp, TREE_OPERAND (e, 1));
1342 pp_c_compound_literal (pp, e);
1345 case COMPOUND_LITERAL_EXPR:
1346 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1349 pp_initializer (pp, e);
1353 pp_c_identifier (pp, "__builtin_va_arg");
1354 pp_c_left_paren (pp);
1355 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1356 pp_separate_with (pp, ',');
1357 pp_type_id (pp, TREE_TYPE (e));
1358 pp_c_right_paren (pp);
1362 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1364 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1367 /* else fall through. */
1370 pp_primary_expression (pp, e);
1375 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1378 pp_c_expression_list (c_pretty_printer *pp, tree e)
1380 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1382 pp_expression (pp, TREE_VALUE (e));
1384 pp_separate_with (pp, ',');
1388 /* Print out V, which contains the elements of a constructor. */
1391 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1393 unsigned HOST_WIDE_INT ix;
1396 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1398 pp_expression (pp, value);
1399 if (ix != VEC_length (constructor_elt, v) - 1)
1400 pp_separate_with (pp, ',');
1404 /* Print out an expression-list in parens, as in a function call. */
1407 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1409 pp_c_left_paren (pp);
1410 if (t && TREE_CODE (t) == TREE_LIST)
1411 pp_c_expression_list (pp, t);
1412 pp_c_right_paren (pp);
1415 /* unary-expression:
1419 unary-operator cast-expression
1420 sizeof unary-expression
1423 unary-operator: one of
1428 __alignof__ unary-expression
1429 __alignof__ ( type-id )
1430 __real__ unary-expression
1431 __imag__ unary-expression */
1434 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1436 enum tree_code code = TREE_CODE (e);
1439 case PREINCREMENT_EXPR:
1440 case PREDECREMENT_EXPR:
1441 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1442 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1449 case TRUTH_NOT_EXPR:
1451 /* String literal are used by address. */
1452 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1454 else if (code == INDIRECT_REF)
1456 else if (code == NEGATE_EXPR)
1458 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1460 else if (code == TRUTH_NOT_EXPR)
1461 pp_exclamation (pp);
1462 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1467 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1468 pp_c_whitespace (pp);
1469 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1473 pp_postfix_expression (pp, e);
1480 ( type-name ) cast-expression */
1483 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1485 switch (TREE_CODE (e))
1488 case FIX_TRUNC_EXPR:
1490 pp_c_type_cast (pp, TREE_TYPE (e));
1491 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1495 pp_unary_expression (pp, e);
1499 /* multiplicative-expression:
1501 multiplicative-expression * cast-expression
1502 multiplicative-expression / cast-expression
1503 multiplicative-expression % cast-expression */
1506 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1508 enum tree_code code = TREE_CODE (e);
1512 case TRUNC_DIV_EXPR:
1513 case TRUNC_MOD_EXPR:
1514 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1515 pp_c_whitespace (pp);
1516 if (code == MULT_EXPR)
1518 else if (code == TRUNC_DIV_EXPR)
1522 pp_c_whitespace (pp);
1523 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1527 pp_c_cast_expression (pp, e);
1532 /* additive-expression:
1533 multiplicative-expression
1534 additive-expression + multiplicative-expression
1535 additive-expression - multiplicative-expression */
1538 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1540 enum tree_code code = TREE_CODE (e);
1545 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1546 pp_c_whitespace (pp);
1547 if (code == PLUS_EXPR)
1551 pp_c_whitespace (pp);
1552 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1556 pp_multiplicative_expression (pp, e);
1561 /* additive-expression:
1563 shift-expression << additive-expression
1564 shift-expression >> additive-expression */
1567 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1569 enum tree_code code = TREE_CODE (e);
1574 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1575 pp_c_whitespace (pp);
1576 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1577 pp_c_whitespace (pp);
1578 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1582 pp_c_additive_expression (pp, e);
1586 /* relational-expression:
1588 relational-expression < shift-expression
1589 relational-expression > shift-expression
1590 relational-expression <= shift-expression
1591 relational-expression >= shift-expression */
1594 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1596 enum tree_code code = TREE_CODE (e);
1603 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1604 pp_c_whitespace (pp);
1605 if (code == LT_EXPR)
1607 else if (code == GT_EXPR)
1609 else if (code == LE_EXPR)
1610 pp_identifier (pp, "<=");
1611 else if (code == GE_EXPR)
1612 pp_identifier (pp, ">=");
1613 pp_c_whitespace (pp);
1614 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1618 pp_c_shift_expression (pp, e);
1623 /* equality-expression:
1624 relational-expression
1625 equality-expression == relational-expression
1626 equality-equality != relational-expression */
1629 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1631 enum tree_code code = TREE_CODE (e);
1636 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1637 pp_c_whitespace (pp);
1638 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1639 pp_c_whitespace (pp);
1640 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1644 pp_c_relational_expression (pp, e);
1651 AND-expression & equality-equality */
1654 pp_c_and_expression (c_pretty_printer *pp, tree e)
1656 if (TREE_CODE (e) == BIT_AND_EXPR)
1658 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1659 pp_c_whitespace (pp);
1661 pp_c_whitespace (pp);
1662 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1665 pp_c_equality_expression (pp, e);
1668 /* exclusive-OR-expression:
1670 exclusive-OR-expression ^ AND-expression */
1673 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1675 if (TREE_CODE (e) == BIT_XOR_EXPR)
1677 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1678 pp_c_maybe_whitespace (pp);
1680 pp_c_whitespace (pp);
1681 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1684 pp_c_and_expression (pp, e);
1687 /* inclusive-OR-expression:
1688 exclusive-OR-expression
1689 inclusive-OR-expression | exclusive-OR-expression */
1692 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1694 if (TREE_CODE (e) == BIT_IOR_EXPR)
1696 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1697 pp_c_whitespace (pp);
1699 pp_c_whitespace (pp);
1700 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1703 pp_c_exclusive_or_expression (pp, e);
1706 /* logical-AND-expression:
1707 inclusive-OR-expression
1708 logical-AND-expression && inclusive-OR-expression */
1711 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1713 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1715 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1716 pp_c_whitespace (pp);
1717 pp_identifier (pp, "&&");
1718 pp_c_whitespace (pp);
1719 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1722 pp_c_inclusive_or_expression (pp, e);
1725 /* logical-OR-expression:
1726 logical-AND-expression
1727 logical-OR-expression || logical-AND-expression */
1730 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1732 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1734 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1735 pp_c_whitespace (pp);
1736 pp_identifier (pp, "||");
1737 pp_c_whitespace (pp);
1738 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1741 pp_c_logical_and_expression (pp, e);
1744 /* conditional-expression:
1745 logical-OR-expression
1746 logical-OR-expression ? expression : conditional-expression */
1749 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1751 if (TREE_CODE (e) == COND_EXPR)
1753 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1754 pp_c_whitespace (pp);
1756 pp_c_whitespace (pp);
1757 pp_expression (pp, TREE_OPERAND (e, 1));
1758 pp_c_whitespace (pp);
1760 pp_c_whitespace (pp);
1761 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1764 pp_c_logical_or_expression (pp, e);
1768 /* assignment-expression:
1769 conditional-expression
1770 unary-expression assignment-operator assignment-expression
1772 assignment-expression: one of
1773 = *= /= %= += -= >>= <<= &= ^= |= */
1776 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1778 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1780 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1781 pp_c_whitespace (pp);
1784 pp_c_expression (pp, TREE_OPERAND (e, 1));
1787 pp_c_conditional_expression (pp, e);
1791 assignment-expression
1792 expression , assignment-expression
1794 Implementation note: instead of going through the usual recursion
1795 chain, I take the liberty of dispatching nodes to the appropriate
1796 functions. This makes some redundancy, but it worths it. That also
1797 prevents a possible infinite recursion between pp_c_primary_expression ()
1798 and pp_c_expression (). */
1801 pp_c_expression (c_pretty_printer *pp, tree e)
1803 switch (TREE_CODE (e))
1806 pp_c_integer_constant (pp, e);
1810 pp_c_floating_constant (pp, e);
1814 pp_c_string_literal (pp, e);
1817 case IDENTIFIER_NODE:
1826 pp_primary_expression (pp, e);
1829 case POSTINCREMENT_EXPR:
1830 case POSTDECREMENT_EXPR:
1838 case UNORDERED_EXPR:
1847 case COMPOUND_LITERAL_EXPR:
1849 pp_postfix_expression (pp, e);
1857 case TRUTH_NOT_EXPR:
1858 case PREINCREMENT_EXPR:
1859 case PREDECREMENT_EXPR:
1862 pp_c_unary_expression (pp, e);
1866 case FIX_TRUNC_EXPR:
1868 pp_c_cast_expression (pp, e);
1872 case TRUNC_MOD_EXPR:
1873 case TRUNC_DIV_EXPR:
1874 pp_multiplicative_expression (pp, e);
1879 pp_c_shift_expression (pp, e);
1886 pp_c_relational_expression (pp, e);
1890 pp_c_and_expression (pp, e);
1894 pp_c_exclusive_or_expression (pp, e);
1898 pp_c_inclusive_or_expression (pp, e);
1901 case TRUTH_ANDIF_EXPR:
1902 pp_c_logical_and_expression (pp, e);
1905 case TRUTH_ORIF_EXPR:
1906 pp_c_logical_or_expression (pp, e);
1911 pp_c_equality_expression (pp, e);
1915 pp_conditional_expression (pp, e);
1920 pp_c_additive_expression (pp, e);
1925 pp_assignment_expression (pp, e);
1929 pp_c_left_paren (pp);
1930 pp_expression (pp, TREE_OPERAND (e, 0));
1931 pp_separate_with (pp, ',');
1932 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1933 pp_c_right_paren (pp);
1937 case NON_LVALUE_EXPR:
1939 pp_expression (pp, TREE_OPERAND (e, 0));
1943 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1947 pp_unsupported_tree (pp, e);
1957 pp_c_statement (c_pretty_printer *pp, tree stmt)
1962 if (pp_needs_newline (pp))
1963 pp_newline_and_indent (pp, 0);
1965 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1969 /* Initialize the PRETTY-PRINTER for handling C codes. */
1972 pp_c_pretty_printer_init (c_pretty_printer *pp)
1974 pp->offset_list = 0;
1976 pp->declaration = pp_c_declaration;
1977 pp->declaration_specifiers = pp_c_declaration_specifiers;
1978 pp->declarator = pp_c_declarator;
1979 pp->direct_declarator = pp_c_direct_declarator;
1980 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
1981 pp->abstract_declarator = pp_c_abstract_declarator;
1982 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
1983 pp->ptr_operator = pp_c_pointer;
1984 pp->parameter_list = pp_c_parameter_type_list;
1985 pp->type_id = pp_c_type_id;
1986 pp->simple_type_specifier = pp_c_type_specifier;
1987 pp->function_specifier = pp_c_function_specifier;
1988 pp->storage_class_specifier = pp_c_storage_class_specifier;
1990 pp->statement = pp_c_statement;
1992 pp->id_expression = pp_c_id_expression;
1993 pp->primary_expression = pp_c_primary_expression;
1994 pp->postfix_expression = pp_c_postfix_expression;
1995 pp->unary_expression = pp_c_unary_expression;
1996 pp->initializer = pp_c_initializer;
1997 pp->multiplicative_expression = pp_c_multiplicative_expression;
1998 pp->conditional_expression = pp_c_conditional_expression;
1999 pp->assignment_expression = pp_c_assignment_expression;
2000 pp->expression = pp_c_expression;
2004 /* Print the tree T in full, on file FILE. */
2007 print_c_tree (FILE *file, tree t)
2009 static c_pretty_printer pp_rec;
2010 static bool initialized = 0;
2011 c_pretty_printer *pp = &pp_rec;
2016 pp_construct (pp_base (pp), NULL, 0);
2017 pp_c_pretty_printer_init (pp);
2018 pp_needs_newline (pp) = true;
2020 pp_base (pp)->buffer->stream = file;
2022 pp_statement (pp, t);
2028 /* Print the tree T in full, on stderr. */
2031 debug_c_tree (tree t)
2033 print_c_tree (stderr, t);
2034 fputc ('\n', stderr);
2037 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2038 up of T's memory address. */
2041 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2045 gcc_assert (DECL_P (t));
2048 name = IDENTIFIER_POINTER (DECL_NAME (t));
2051 static char xname[8];
2052 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2056 pp_c_identifier (pp, name);