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
24 #include "coretypes.h"
27 #include "c-pretty-print.h"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
42 if (pp_base (PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
47 static void pp_c_char (c_pretty_printer *, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer *, tree);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
53 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
54 static void pp_c_additive_expression (c_pretty_printer *, tree);
55 static void pp_c_shift_expression (c_pretty_printer *, tree);
56 static void pp_c_relational_expression (c_pretty_printer *, tree);
57 static void pp_c_equality_expression (c_pretty_printer *, tree);
58 static void pp_c_and_expression (c_pretty_printer *, tree);
59 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
61 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62 static void pp_c_conditional_expression (c_pretty_printer *, tree);
63 static void pp_c_assignment_expression (c_pretty_printer *, tree);
68 /* Helper functions. */
71 pp_c_whitespace (c_pretty_printer *pp)
74 pp_base (pp)->padding = pp_none;
78 pp_c_left_paren (c_pretty_printer *pp)
81 pp_base (pp)->padding = pp_none;
85 pp_c_right_paren (c_pretty_printer *pp)
88 pp_base (pp)->padding = pp_none;
92 pp_c_left_brace (c_pretty_printer *pp)
95 pp_base (pp)->padding = pp_none;
99 pp_c_right_brace (c_pretty_printer *pp)
102 pp_base (pp)->padding = pp_none;
106 pp_c_left_bracket (c_pretty_printer *pp)
108 pp_left_bracket (pp);
109 pp_base (pp)->padding = pp_none;
113 pp_c_right_bracket (c_pretty_printer *pp)
115 pp_right_bracket (pp);
116 pp_base (pp)->padding = pp_none;
120 pp_c_dot (c_pretty_printer *pp)
123 pp_base (pp)->padding = pp_none;
127 pp_c_ampersand (c_pretty_printer *pp)
130 pp_base (pp)->padding = pp_none;
134 pp_c_star (c_pretty_printer *pp)
137 pp_base (pp)->padding = pp_none;
141 pp_c_arrow (c_pretty_printer *pp)
144 pp_base (pp)->padding = pp_none;
148 pp_c_semicolon (c_pretty_printer *pp)
151 pp_base (pp)->padding = pp_none;
155 pp_c_complement (c_pretty_printer *pp)
158 pp_base (pp)->padding = pp_none;
162 pp_c_exclamation (c_pretty_printer *pp)
165 pp_base (pp)->padding = pp_none;
168 /* Print out the external representation of CV-QUALIFIER. */
171 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
173 const char *p = pp_last_position_in_text (pp);
174 /* The C programming language does not have references, but it is much
175 simpler to handle those here rather than going through the same
176 logic in the C++ pretty-printer. */
177 if (p != NULL && (*p == '*' || *p == '&'))
178 pp_c_whitespace (pp);
179 pp_c_identifier (pp, cv);
182 /* Pretty-print T using the type-cast notation '( type-name )'. */
185 pp_c_type_cast (c_pretty_printer *pp, tree t)
187 pp_c_left_paren (pp);
189 pp_c_right_paren (pp);
192 /* We're about to pretty-print a pointer type as indicated by T.
193 Output a whitespace, if needed, preparing for subsequent output. */
196 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
198 if (POINTER_TYPE_P (t))
200 tree pointee = strip_pointer_operator (TREE_TYPE (t));
201 if (TREE_CODE (pointee) != ARRAY_TYPE
202 && TREE_CODE (pointee) != FUNCTION_TYPE)
203 pp_c_whitespace (pp);
210 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
211 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
212 of its type. Take care of possible extensions.
216 type-qualifier-list type-qualifier
221 __restrict__ -- GNU C
225 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
232 qualifiers = TYPE_QUALS (t);
233 if (qualifiers & TYPE_QUAL_CONST)
234 pp_c_cv_qualifier (pp, "const");
235 if (qualifiers & TYPE_QUAL_VOLATILE)
236 pp_c_cv_qualifier (pp, "volatile");
237 if (qualifiers & TYPE_QUAL_RESTRICT)
238 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
242 * type-qualifier-list(opt)
243 * type-qualifier-list(opt) pointer */
246 pp_c_pointer (c_pretty_printer *pp, tree t)
248 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
250 switch (TREE_CODE (t))
253 /* It is easier to handle C++ reference types here. */
255 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
256 pp_c_pointer (pp, TREE_TYPE (t));
257 if (TREE_CODE (t) == POINTER_TYPE)
261 pp_c_type_qualifier_list (pp, t);
264 /* ??? This node is now in GENERIC and so shouldn't be here. But
265 we'll fix that later. */
267 pp_declaration (pp, DECL_EXPR_DECL (t));
268 pp_needs_newline (pp) = true;
272 pp_unsupported_tree (pp, t);
289 struct-or-union-specifier
294 simple-type-specifier:
299 pp_c_type_specifier (c_pretty_printer *pp, tree t)
301 const enum tree_code code = TREE_CODE (t);
305 pp_c_identifier (pp, "<type-error>");
308 case IDENTIFIER_NODE:
309 pp_c_tree_decl_identifier (pp, t);
320 pp_c_type_specifier (pp, t);
324 int prec = TYPE_PRECISION (t);
325 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
326 pp_c_type_specifier (pp, t);
327 if (TYPE_PRECISION (t) != prec)
330 pp_decimal_int (pp, prec);
337 pp_id_expression (pp, t);
339 pp_c_identifier (pp, "<typedef-error>");
345 if (code == UNION_TYPE)
346 pp_c_identifier (pp, "union");
347 else if (code == RECORD_TYPE)
348 pp_c_identifier (pp, "struct");
349 else if (code == ENUMERAL_TYPE)
350 pp_c_identifier (pp, "enum");
352 pp_c_identifier (pp, "<tag-error>");
355 pp_id_expression (pp, TYPE_NAME (t));
357 pp_c_identifier (pp, "<anonymous>");
361 pp_unsupported_tree (pp, t);
366 /* specifier-qualifier-list:
367 type-specifier specifier-qualifier-list-opt
368 type-qualifier specifier-qualifier-list-opt
371 Implementation note: Because of the non-linearities in array or
372 function declarations, this routine prints not just the
373 specifier-qualifier-list of such entities or types of such entities,
374 but also the 'pointer' production part of their declarators. The
375 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
378 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
380 const enum tree_code code = TREE_CODE (t);
382 if (TREE_CODE (t) != POINTER_TYPE)
383 pp_c_type_qualifier_list (pp, t);
389 /* Get the types-specifier of this type. */
390 tree pointee = strip_pointer_operator (TREE_TYPE (t));
391 pp_c_specifier_qualifier_list (pp, pointee);
392 if (TREE_CODE (pointee) == ARRAY_TYPE
393 || TREE_CODE (pointee) == FUNCTION_TYPE)
395 pp_c_whitespace (pp);
396 pp_c_left_paren (pp);
398 else if (!c_dialect_cxx ())
399 pp_c_whitespace (pp);
400 pp_ptr_operator (pp, t);
406 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
411 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
412 if (code == COMPLEX_TYPE)
413 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
414 else if (code == VECTOR_TYPE)
415 pp_c_identifier (pp, "__vector__");
419 pp_simple_type_specifier (pp, t);
424 /* parameter-type-list:
429 parameter-declaration
430 parameter-list , parameter-declaration
432 parameter-declaration:
433 declaration-specifiers declarator
434 declaration-specifiers abstract-declarator(opt) */
437 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
439 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
440 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
441 pp_c_left_paren (pp);
442 if (parms == void_list_node)
443 pp_c_identifier (pp, "void");
447 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
450 pp_separate_with (pp, ',');
452 pp_declaration_specifiers
453 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
455 pp_declarator (pp, parms);
457 pp_abstract_declarator (pp, TREE_VALUE (parms));
460 pp_c_right_paren (pp);
463 /* abstract-declarator:
465 pointer(opt) direct-abstract-declarator */
468 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
470 if (TREE_CODE (t) == POINTER_TYPE)
472 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
473 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
474 pp_c_right_paren (pp);
478 pp_direct_abstract_declarator (pp, t);
481 /* direct-abstract-declarator:
482 ( abstract-declarator )
483 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
484 direct-abstract-declarator(opt) [ * ]
485 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
488 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
490 switch (TREE_CODE (t))
493 pp_abstract_declarator (pp, t);
497 pp_c_parameter_type_list (pp, t);
498 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
502 pp_c_left_bracket (pp);
503 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
504 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
505 pp_c_right_bracket (pp);
506 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
509 case IDENTIFIER_NODE:
523 pp_unsupported_tree (pp, t);
529 specifier-qualifier-list abstract-declarator(opt) */
532 pp_c_type_id (c_pretty_printer *pp, tree t)
534 pp_c_specifier_qualifier_list (pp, t);
535 pp_abstract_declarator (pp, t);
538 /* storage-class-specifier:
546 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
548 if (TREE_CODE (t) == TYPE_DECL)
549 pp_c_identifier (pp, "typedef");
552 if (DECL_REGISTER (t))
553 pp_c_identifier (pp, "register");
554 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
555 pp_c_identifier (pp, "static");
559 /* function-specifier:
563 pp_c_function_specifier (c_pretty_printer *pp, tree t)
565 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
566 pp_c_identifier (pp, "inline");
569 /* declaration-specifiers:
570 storage-class-specifier declaration-specifiers(opt)
571 type-specifier declaration-specifiers(opt)
572 type-qualifier declaration-specifiers(opt)
573 function-specifier declaration-specifiers(opt) */
576 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
578 pp_storage_class_specifier (pp, t);
579 pp_function_specifier (pp, t);
580 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
586 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
587 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
588 direct-declarator [ type-qualifier-list static assignment-expression ]
589 direct-declarator [ type-qualifier-list * ]
590 direct-declarator ( parameter-type-list )
591 direct-declarator ( identifier-list(opt) ) */
594 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
596 switch (TREE_CODE (t))
603 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
604 pp_c_tree_decl_identifier (pp, t);
609 pp_abstract_declarator (pp, TREE_TYPE (t));
613 pp_parameter_list (pp, t);
614 pp_abstract_declarator (pp, TREE_TYPE (t));
618 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
619 pp_c_tree_decl_identifier (pp, t);
620 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
621 pp_abstract_declarator (pp, TREE_TYPE (t));
624 pp_parameter_list (pp, t);
625 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
637 pp_unsupported_tree (pp, t);
644 pointer(opt) direct-declarator */
647 pp_c_declarator (c_pretty_printer *pp, tree t)
649 switch (TREE_CODE (t))
665 pp_direct_declarator (pp, t);
670 pp_unsupported_tree (pp, t);
676 declaration-specifiers init-declarator-list(opt) ; */
679 pp_c_declaration (c_pretty_printer *pp, tree t)
681 pp_declaration_specifiers (pp, t);
682 pp_c_init_declarator (pp, t);
685 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
688 pp_c_attributes (c_pretty_printer *pp, tree attributes)
690 if (attributes == NULL_TREE)
693 pp_c_identifier (pp, "__attribute__");
694 pp_c_left_paren (pp);
695 pp_c_left_paren (pp);
696 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
698 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
699 if (TREE_VALUE (attributes))
700 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
702 if (TREE_CHAIN (attributes))
703 pp_separate_with (pp, ',');
705 pp_c_right_paren (pp);
706 pp_c_right_paren (pp);
709 /* function-definition:
710 declaration-specifiers declarator compound-statement */
713 pp_c_function_definition (c_pretty_printer *pp, tree t)
715 pp_declaration_specifiers (pp, t);
716 pp_declarator (pp, t);
717 pp_needs_newline (pp) = true;
718 pp_statement (pp, DECL_SAVED_TREE (t));
726 /* Print out a c-char. This is called solely for characters which are
727 in the *target* execution character set. We ought to convert them
728 back to the *host* execution character set before printing, but we
729 have no way to do this at present. A decent compromise is to print
730 all characters as if they were in the host execution character set,
731 and not attempt to recover any named escape characters, but render
732 all unprintables as octal escapes. If the host and target character
733 sets are the same, this produces relatively readable output. If they
734 are not the same, strings may appear as gibberish, but that's okay
735 (in fact, it may well be what the reader wants, e.g. if they are looking
736 to see if conversion to the target character set happened correctly).
738 A special case: we need to prefix \, ", and ' with backslashes. It is
739 correct to do so for the *host*'s \, ", and ', because the rest of the
740 file appears in the host character set. */
743 pp_c_char (c_pretty_printer *pp, int c)
749 case '\\': pp_string (pp, "\\\\"); break;
750 case '\'': pp_string (pp, "\\\'"); break;
751 case '\"': pp_string (pp, "\\\""); break;
752 default: pp_character (pp, c);
756 pp_scalar (pp, "\\%03o", (unsigned) c);
759 /* Print out a STRING literal. */
762 pp_c_string_literal (c_pretty_printer *pp, tree s)
764 const char *p = TREE_STRING_POINTER (s);
765 int n = TREE_STRING_LENGTH (s) - 1;
768 for (i = 0; i < n; ++i)
769 pp_c_char (pp, p[i]);
773 /* Pretty-print an INTEGER literal. */
776 pp_c_integer_constant (c_pretty_printer *pp, tree i)
778 tree type = TREE_TYPE (i);
780 if (TREE_INT_CST_HIGH (i) == 0)
781 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
784 if (tree_int_cst_sgn (i) < 0)
786 pp_character (pp, '-');
787 i = build_int_cst_wide (NULL_TREE,
788 -TREE_INT_CST_LOW (i),
789 ~TREE_INT_CST_HIGH (i)
790 + !TREE_INT_CST_LOW (i));
792 sprintf (pp_buffer (pp)->digit_buffer,
793 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
794 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
795 pp_string (pp, pp_buffer (pp)->digit_buffer);
797 if (TYPE_UNSIGNED (type))
798 pp_character (pp, 'u');
799 if (type == long_integer_type_node || type == long_unsigned_type_node)
800 pp_character (pp, 'l');
801 else if (type == long_long_integer_type_node
802 || type == long_long_unsigned_type_node)
803 pp_string (pp, "ll");
806 /* Print out a CHARACTER literal. */
809 pp_c_character_constant (c_pretty_printer *pp, tree c)
811 tree type = TREE_TYPE (c);
812 if (type == wchar_type_node)
813 pp_character (pp, 'L');
815 if (host_integerp (c, TYPE_UNSIGNED (type)))
816 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
818 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
822 /* Print out a BOOLEAN literal. */
825 pp_c_bool_constant (c_pretty_printer *pp, tree b)
827 if (b == boolean_false_node)
829 if (c_dialect_cxx ())
830 pp_c_identifier (pp, "false");
831 else if (flag_isoc99)
832 pp_c_identifier (pp, "_False");
834 pp_unsupported_tree (pp, b);
836 else if (b == boolean_true_node)
838 if (c_dialect_cxx ())
839 pp_c_identifier (pp, "true");
840 else if (flag_isoc99)
841 pp_c_identifier (pp, "_True");
843 pp_unsupported_tree (pp, b);
845 else if (TREE_CODE (b) == INTEGER_CST)
846 pp_c_integer_constant (pp, b);
848 pp_unsupported_tree (pp, b);
851 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
852 false; that means the value was obtained by a cast, in which case
853 print out the type-id part of the cast-expression -- the casted value
854 is then printed by pp_c_integer_literal. */
857 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
859 bool value_is_named = true;
860 tree type = TREE_TYPE (e);
863 /* Find the name of this constant. */
864 for (value = TYPE_VALUES (type);
865 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
866 value = TREE_CHAIN (value))
869 if (value != NULL_TREE)
870 pp_id_expression (pp, TREE_PURPOSE (value));
873 /* Value must have been cast. */
874 pp_c_type_cast (pp, type);
875 value_is_named = false;
878 return value_is_named;
881 /* Print out a REAL value as a decimal-floating-constant. */
884 pp_c_floating_constant (c_pretty_printer *pp, tree r)
886 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
887 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
888 pp_string (pp, pp_buffer(pp)->digit_buffer);
889 if (TREE_TYPE (r) == float_type_node)
890 pp_character (pp, 'f');
891 else if (TREE_TYPE (r) == long_double_type_node)
892 pp_character (pp, 'l');
895 /* Pretty-print a compound literal expression. GNU extensions include
899 pp_c_compound_literal (c_pretty_printer *pp, tree e)
901 tree type = TREE_TYPE (e);
902 pp_c_type_cast (pp, type);
904 switch (TREE_CODE (type))
911 pp_c_brace_enclosed_initializer_list (pp, e);
915 pp_unsupported_tree (pp, e);
924 character-constant */
927 pp_c_constant (c_pretty_printer *pp, tree e)
929 const enum tree_code code = TREE_CODE (e);
935 tree type = TREE_TYPE (e);
936 if (type == boolean_type_node)
937 pp_c_bool_constant (pp, e);
938 else if (type == char_type_node)
939 pp_c_character_constant (pp, e);
940 else if (TREE_CODE (type) == ENUMERAL_TYPE
941 && pp_c_enumeration_constant (pp, e))
944 pp_c_integer_constant (pp, e);
949 pp_c_floating_constant (pp, e);
953 pp_c_string_literal (pp, e);
957 pp_unsupported_tree (pp, e);
962 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
965 pp_c_identifier (c_pretty_printer *pp, const char *id)
967 pp_c_maybe_whitespace (pp);
968 pp_identifier (pp, id);
969 pp_base (pp)->padding = pp_before;
972 /* Pretty-print a C primary-expression.
980 pp_c_primary_expression (c_pretty_printer *pp, tree e)
982 switch (TREE_CODE (e))
990 pp_c_tree_decl_identifier (pp, e);
993 case IDENTIFIER_NODE:
994 pp_c_tree_identifier (pp, e);
998 pp_c_identifier (pp, "<erroneous-expression>");
1002 pp_c_identifier (pp, "<return-value>");
1008 pp_c_constant (pp, e);
1012 pp_c_identifier (pp, "__builtin_memcpy");
1013 pp_c_left_paren (pp);
1015 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1016 pp_separate_with (pp, ',');
1018 pp_initializer (pp, TREE_OPERAND (e, 1));
1019 if (TREE_OPERAND (e, 2))
1021 pp_separate_with (pp, ',');
1022 pp_c_expression (pp, TREE_OPERAND (e, 2));
1024 pp_c_right_paren (pp);
1028 /* FIXME: Make sure we won't get into an infinie loop. */
1029 pp_c_left_paren (pp);
1030 pp_expression (pp, e);
1031 pp_c_right_paren (pp);
1036 /* Print out a C initializer -- also support C compound-literals.
1038 assignment-expression:
1039 { initializer-list }
1040 { initializer-list , } */
1043 pp_c_initializer (c_pretty_printer *pp, tree e)
1045 if (TREE_CODE (e) == CONSTRUCTOR)
1046 pp_c_brace_enclosed_initializer_list (pp, e);
1048 pp_expression (pp, e);
1053 declarator = initializer */
1056 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1058 pp_declarator (pp, t);
1059 /* We don't want to output function definitions here. There are handled
1060 elsewhere (and the syntactic form is bogus anyway). */
1061 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1063 tree init = DECL_INITIAL (t);
1064 /* This C++ bit is handled here because it is easier to do so.
1065 In templates, the C++ parser builds a TREE_LIST for a
1066 direct-initialization; the TREE_PURPOSE is the variable to
1067 initialize and the TREE_VALUE is the initializer. */
1068 if (TREE_CODE (init) == TREE_LIST)
1070 pp_c_left_paren (pp);
1071 pp_expression (pp, TREE_VALUE (init));
1072 pp_right_paren (pp);
1079 pp_c_initializer (pp, init);
1084 /* initializer-list:
1085 designation(opt) initializer
1086 initializer-list , designation(opt) initializer
1093 designator-list designator
1096 [ constant-expression ]
1100 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1102 tree type = TREE_TYPE (e);
1103 const enum tree_code code = TREE_CODE (type);
1111 tree init = TREE_OPERAND (e, 0);
1112 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1114 if (code == RECORD_TYPE || code == UNION_TYPE)
1117 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1121 pp_c_left_bracket (pp);
1122 if (TREE_PURPOSE (init))
1123 pp_c_constant (pp, TREE_PURPOSE (init));
1124 pp_c_right_bracket (pp);
1126 pp_c_whitespace (pp);
1128 pp_c_whitespace (pp);
1129 pp_initializer (pp, TREE_VALUE (init));
1130 if (TREE_CHAIN (init))
1131 pp_separate_with (pp, ',');
1137 if (TREE_CODE (e) == VECTOR_CST)
1138 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1139 else if (TREE_CODE (e) == CONSTRUCTOR)
1140 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1146 if (TREE_CODE (e) == CONSTRUCTOR)
1147 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1148 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1150 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1151 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1152 pp_separate_with (pp, ',');
1153 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1163 pp_unsupported_tree (pp, type);
1166 /* Pretty-print a brace-enclosed initializer-list. */
1169 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1171 pp_c_left_brace (pp);
1172 pp_c_initializer_list (pp, l);
1173 pp_c_right_brace (pp);
1177 /* This is a convenient function, used to bridge gap between C and C++
1184 pp_c_id_expression (c_pretty_printer *pp, tree t)
1186 switch (TREE_CODE (t))
1195 pp_c_tree_decl_identifier (pp, t);
1198 case IDENTIFIER_NODE:
1199 pp_c_tree_identifier (pp, t);
1203 pp_unsupported_tree (pp, t);
1208 /* postfix-expression:
1210 postfix-expression [ expression ]
1211 postfix-expression ( argument-expression-list(opt) )
1212 postfix-expression . identifier
1213 postfix-expression -> identifier
1214 postfix-expression ++
1215 postfix-expression --
1216 ( type-name ) { initializer-list }
1217 ( type-name ) { initializer-list , } */
1220 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1222 enum tree_code code = TREE_CODE (e);
1225 case POSTINCREMENT_EXPR:
1226 case POSTDECREMENT_EXPR:
1227 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1228 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1232 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1233 pp_c_left_bracket (pp);
1234 pp_expression (pp, TREE_OPERAND (e, 1));
1235 pp_c_right_bracket (pp);
1239 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1240 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1243 case UNORDERED_EXPR:
1244 pp_c_identifier (pp, flag_isoc99
1246 : "__builtin_isunordered");
1250 pp_c_identifier (pp, flag_isoc99
1252 : "!__builtin_isunordered");
1256 pp_c_identifier (pp, flag_isoc99
1258 : "!__builtin_isgreaterequal");
1262 pp_c_identifier (pp, flag_isoc99
1264 : "!__builtin_isgreater");
1268 pp_c_identifier (pp, flag_isoc99
1270 : "!__builtin_islessequal");
1274 pp_c_identifier (pp, flag_isoc99
1276 : "!__builtin_isless");
1280 pp_c_identifier (pp, flag_isoc99
1282 : "!__builtin_islessgreater");
1286 pp_c_identifier (pp, flag_isoc99
1288 : "__builtin_islessgreater");
1292 pp_c_left_paren (pp);
1293 pp_expression (pp, TREE_OPERAND (e, 0));
1294 pp_separate_with (pp, ',');
1295 pp_expression (pp, TREE_OPERAND (e, 1));
1296 pp_c_right_paren (pp);
1300 pp_c_identifier (pp, "__builtin_abs");
1301 pp_c_left_paren (pp);
1302 pp_expression (pp, TREE_OPERAND (e, 0));
1303 pp_c_right_paren (pp);
1308 tree object = TREE_OPERAND (e, 0);
1309 if (TREE_CODE (object) == INDIRECT_REF)
1311 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1316 pp_postfix_expression (pp, object);
1319 pp_expression (pp, TREE_OPERAND (e, 1));
1326 pp_c_compound_literal (pp, e);
1329 case COMPOUND_LITERAL_EXPR:
1330 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1333 pp_initializer (pp, e);
1337 pp_c_identifier (pp, "__builtin_va_arg");
1338 pp_c_left_paren (pp);
1339 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1340 pp_separate_with (pp, ',');
1341 pp_type_id (pp, TREE_TYPE (e));
1342 pp_c_right_paren (pp);
1346 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1348 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1351 /* else fall through. */
1354 pp_primary_expression (pp, e);
1359 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1362 pp_c_expression_list (c_pretty_printer *pp, tree e)
1364 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1366 pp_expression (pp, TREE_VALUE (e));
1368 pp_separate_with (pp, ',');
1372 /* Print out an expression-list in parens, as in a function call. */
1375 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1377 pp_c_left_paren (pp);
1378 if (t && TREE_CODE (t) == TREE_LIST)
1379 pp_c_expression_list (pp, t);
1380 pp_c_right_paren (pp);
1383 /* unary-expression:
1387 unary-operator cast-expression
1388 sizeof unary-expression
1391 unary-operator: one of
1396 __alignof__ unary-expression
1397 __alignof__ ( type-id )
1398 __real__ unary-expression
1399 __imag__ unary-expression */
1402 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1404 enum tree_code code = TREE_CODE (e);
1407 case PREINCREMENT_EXPR:
1408 case PREDECREMENT_EXPR:
1409 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1410 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1417 case TRUTH_NOT_EXPR:
1419 /* String literal are used by address. */
1420 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1422 else if (code == INDIRECT_REF)
1424 else if (code == NEGATE_EXPR)
1426 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1428 else if (code == TRUTH_NOT_EXPR)
1429 pp_exclamation (pp);
1430 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1435 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1436 pp_c_whitespace (pp);
1437 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1441 pp_postfix_expression (pp, e);
1448 ( type-name ) cast-expression */
1451 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1453 switch (TREE_CODE (e))
1456 case FIX_TRUNC_EXPR:
1458 pp_c_type_cast (pp, TREE_TYPE (e));
1459 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1463 pp_unary_expression (pp, e);
1467 /* multiplicative-expression:
1469 multiplicative-expression * cast-expression
1470 multiplicative-expression / cast-expression
1471 multiplicative-expression % cast-expression */
1474 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1476 enum tree_code code = TREE_CODE (e);
1480 case TRUNC_DIV_EXPR:
1481 case TRUNC_MOD_EXPR:
1482 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1483 pp_c_whitespace (pp);
1484 if (code == MULT_EXPR)
1486 else if (code == TRUNC_DIV_EXPR)
1490 pp_c_whitespace (pp);
1491 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1495 pp_c_cast_expression (pp, e);
1500 /* additive-expression:
1501 multiplicative-expression
1502 additive-expression + multiplicative-expression
1503 additive-expression - multiplicative-expression */
1506 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1508 enum tree_code code = TREE_CODE (e);
1513 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1514 pp_c_whitespace (pp);
1515 if (code == PLUS_EXPR)
1519 pp_c_whitespace (pp);
1520 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1524 pp_multiplicative_expression (pp, e);
1529 /* additive-expression:
1531 shift-expression << additive-expression
1532 shift-expression >> additive-expression */
1535 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1537 enum tree_code code = TREE_CODE (e);
1542 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1543 pp_c_whitespace (pp);
1544 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1545 pp_c_whitespace (pp);
1546 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1550 pp_c_additive_expression (pp, e);
1554 /* relational-expression:
1556 relational-expression < shift-expression
1557 relational-expression > shift-expression
1558 relational-expression <= shift-expression
1559 relational-expression >= shift-expression */
1562 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1564 enum tree_code code = TREE_CODE (e);
1571 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1572 pp_c_whitespace (pp);
1573 if (code == LT_EXPR)
1575 else if (code == GT_EXPR)
1577 else if (code == LE_EXPR)
1578 pp_identifier (pp, "<=");
1579 else if (code == GE_EXPR)
1580 pp_identifier (pp, ">=");
1581 pp_c_whitespace (pp);
1582 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1586 pp_c_shift_expression (pp, e);
1591 /* equality-expression:
1592 relational-expression
1593 equality-expression == relational-expression
1594 equality-equality != relational-expression */
1597 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1599 enum tree_code code = TREE_CODE (e);
1604 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1605 pp_c_whitespace (pp);
1606 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1607 pp_c_whitespace (pp);
1608 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1612 pp_c_relational_expression (pp, e);
1619 AND-expression & equality-equality */
1622 pp_c_and_expression (c_pretty_printer *pp, tree e)
1624 if (TREE_CODE (e) == BIT_AND_EXPR)
1626 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1627 pp_c_whitespace (pp);
1629 pp_c_whitespace (pp);
1630 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1633 pp_c_equality_expression (pp, e);
1636 /* exclusive-OR-expression:
1638 exclusive-OR-expression ^ AND-expression */
1641 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1643 if (TREE_CODE (e) == BIT_XOR_EXPR)
1645 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1646 pp_c_maybe_whitespace (pp);
1648 pp_c_whitespace (pp);
1649 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1652 pp_c_and_expression (pp, e);
1655 /* inclusive-OR-expression:
1656 exclusive-OR-expression
1657 inclusive-OR-expression | exclusive-OR-expression */
1660 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1662 if (TREE_CODE (e) == BIT_IOR_EXPR)
1664 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1665 pp_c_whitespace (pp);
1667 pp_c_whitespace (pp);
1668 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1671 pp_c_exclusive_or_expression (pp, e);
1674 /* logical-AND-expression:
1675 inclusive-OR-expression
1676 logical-AND-expression && inclusive-OR-expression */
1679 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1681 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1683 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1684 pp_c_whitespace (pp);
1685 pp_identifier (pp, "&&");
1686 pp_c_whitespace (pp);
1687 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1690 pp_c_inclusive_or_expression (pp, e);
1693 /* logical-OR-expression:
1694 logical-AND-expression
1695 logical-OR-expression || logical-AND-expression */
1698 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1700 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1702 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1703 pp_c_whitespace (pp);
1704 pp_identifier (pp, "||");
1705 pp_c_whitespace (pp);
1706 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1709 pp_c_logical_and_expression (pp, e);
1712 /* conditional-expression:
1713 logical-OR-expression
1714 logical-OR-expression ? expression : conditional-expression */
1717 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1719 if (TREE_CODE (e) == COND_EXPR)
1721 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1722 pp_c_whitespace (pp);
1724 pp_c_whitespace (pp);
1725 pp_expression (pp, TREE_OPERAND (e, 1));
1726 pp_c_whitespace (pp);
1728 pp_c_whitespace (pp);
1729 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1732 pp_c_logical_or_expression (pp, e);
1736 /* assignment-expression:
1737 conditional-expression
1738 unary-expression assignment-operator assignment-expression
1740 assignment-expression: one of
1741 = *= /= %= += -= >>= <<= &= ^= |= */
1744 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1746 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1748 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1749 pp_c_whitespace (pp);
1752 pp_c_expression (pp, TREE_OPERAND (e, 1));
1755 pp_c_conditional_expression (pp, e);
1759 assignment-expression
1760 expression , assignment-expression
1762 Implementation note: instead of going through the usual recursion
1763 chain, I take the liberty of dispatching nodes to the appropriate
1764 functions. This makes some redundancy, but it worths it. That also
1765 prevents a possible infinite recursion between pp_c_primary_expression ()
1766 and pp_c_expression (). */
1769 pp_c_expression (c_pretty_printer *pp, tree e)
1771 switch (TREE_CODE (e))
1774 pp_c_integer_constant (pp, e);
1778 pp_c_floating_constant (pp, e);
1782 pp_c_string_literal (pp, e);
1785 case IDENTIFIER_NODE:
1794 pp_primary_expression (pp, e);
1797 case POSTINCREMENT_EXPR:
1798 case POSTDECREMENT_EXPR:
1806 case UNORDERED_EXPR:
1815 case COMPOUND_LITERAL_EXPR:
1817 pp_postfix_expression (pp, e);
1825 case TRUTH_NOT_EXPR:
1826 case PREINCREMENT_EXPR:
1827 case PREDECREMENT_EXPR:
1830 pp_c_unary_expression (pp, e);
1834 case FIX_TRUNC_EXPR:
1836 pp_c_cast_expression (pp, e);
1840 case TRUNC_MOD_EXPR:
1841 case TRUNC_DIV_EXPR:
1842 pp_multiplicative_expression (pp, e);
1847 pp_c_shift_expression (pp, e);
1854 pp_c_relational_expression (pp, e);
1858 pp_c_and_expression (pp, e);
1862 pp_c_exclusive_or_expression (pp, e);
1866 pp_c_inclusive_or_expression (pp, e);
1869 case TRUTH_ANDIF_EXPR:
1870 pp_c_logical_and_expression (pp, e);
1873 case TRUTH_ORIF_EXPR:
1874 pp_c_logical_or_expression (pp, e);
1879 pp_c_equality_expression (pp, e);
1883 pp_conditional_expression (pp, e);
1888 pp_c_additive_expression (pp, e);
1893 pp_assignment_expression (pp, e);
1897 pp_c_left_paren (pp);
1898 pp_expression (pp, TREE_OPERAND (e, 0));
1899 pp_separate_with (pp, ',');
1900 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1901 pp_c_right_paren (pp);
1905 case NON_LVALUE_EXPR:
1907 pp_expression (pp, TREE_OPERAND (e, 0));
1911 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1915 pp_unsupported_tree (pp, e);
1925 pp_c_statement (c_pretty_printer *pp, tree stmt)
1930 if (pp_needs_newline (pp))
1931 pp_newline_and_indent (pp, 0);
1933 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1937 /* Initialize the PRETTY-PRINTER for handling C codes. */
1940 pp_c_pretty_printer_init (c_pretty_printer *pp)
1942 pp->offset_list = 0;
1944 pp->declaration = pp_c_declaration;
1945 pp->declaration_specifiers = pp_c_declaration_specifiers;
1946 pp->declarator = pp_c_declarator;
1947 pp->direct_declarator = pp_c_direct_declarator;
1948 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
1949 pp->abstract_declarator = pp_c_abstract_declarator;
1950 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
1951 pp->ptr_operator = pp_c_pointer;
1952 pp->parameter_list = pp_c_parameter_type_list;
1953 pp->type_id = pp_c_type_id;
1954 pp->simple_type_specifier = pp_c_type_specifier;
1955 pp->function_specifier = pp_c_function_specifier;
1956 pp->storage_class_specifier = pp_c_storage_class_specifier;
1958 pp->statement = pp_c_statement;
1960 pp->id_expression = pp_c_id_expression;
1961 pp->primary_expression = pp_c_primary_expression;
1962 pp->postfix_expression = pp_c_postfix_expression;
1963 pp->unary_expression = pp_c_unary_expression;
1964 pp->initializer = pp_c_initializer;
1965 pp->multiplicative_expression = pp_c_multiplicative_expression;
1966 pp->conditional_expression = pp_c_conditional_expression;
1967 pp->assignment_expression = pp_c_assignment_expression;
1968 pp->expression = pp_c_expression;
1972 /* Print the tree T in full, on file FILE. */
1975 print_c_tree (FILE *file, tree t)
1977 static c_pretty_printer pp_rec;
1978 static bool initialized = 0;
1979 c_pretty_printer *pp = &pp_rec;
1984 pp_construct (pp_base (pp), NULL, 0);
1985 pp_c_pretty_printer_init (pp);
1986 pp_needs_newline (pp) = true;
1988 pp_base (pp)->buffer->stream = file;
1990 pp_statement (pp, t);
1996 /* Print the tree T in full, on stderr. */
1999 debug_c_tree (tree t)
2001 print_c_tree (stderr, t);
2002 fputc ('\n', stderr);
2005 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2006 up of T's memory address. */
2009 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2013 gcc_assert (DECL_P (t));
2016 name = IDENTIFIER_POINTER (DECL_NAME (t));
2019 static char xname[8];
2020 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2024 pp_c_identifier (pp, name);