1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004 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, 59 Temple Place - Suite 330, 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 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
321 pp_c_type_specifier (pp, t);
326 pp_id_expression (pp, t);
328 pp_c_identifier (pp, "<typedef-error>");
334 if (code == UNION_TYPE)
335 pp_c_identifier (pp, "union");
336 else if (code == RECORD_TYPE)
337 pp_c_identifier (pp, "struct");
338 else if (code == ENUMERAL_TYPE)
339 pp_c_identifier (pp, "enum");
341 pp_c_identifier (pp, "<tag-error>");
344 pp_id_expression (pp, TYPE_NAME (t));
346 pp_c_identifier (pp, "<anonymous>");
350 pp_unsupported_tree (pp, t);
355 /* specifier-qualifier-list:
356 type-specifier specifier-qualifier-list-opt
357 type-qualifier specifier-qualifier-list-opt
360 Implementation note: Because of the non-linearities in array or
361 function declarations, this routine prints not just the
362 specifier-qualifier-list of such entities or types of such entities,
363 but also the 'pointer' production part of their declarators. The
364 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
367 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
369 const enum tree_code code = TREE_CODE (t);
371 if (TREE_CODE (t) != POINTER_TYPE)
372 pp_c_type_qualifier_list (pp, t);
378 /* Get the types-specifier of this type. */
379 tree pointee = strip_pointer_operator (TREE_TYPE (t));
380 pp_c_specifier_qualifier_list (pp, pointee);
381 if (TREE_CODE (pointee) == ARRAY_TYPE
382 || TREE_CODE (pointee) == FUNCTION_TYPE)
384 pp_c_whitespace (pp);
385 pp_c_left_paren (pp);
387 else if (!c_dialect_cxx ())
388 pp_c_whitespace (pp);
389 pp_ptr_operator (pp, t);
395 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
400 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
401 if (code == COMPLEX_TYPE)
402 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
403 else if (code == VECTOR_TYPE)
404 pp_c_identifier (pp, "__vector__");
408 pp_simple_type_specifier (pp, t);
413 /* parameter-type-list:
418 parameter-declaration
419 parameter-list , parameter-declaration
421 parameter-declaration:
422 declaration-specifiers declarator
423 declaration-specifiers abstract-declarator(opt) */
426 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
428 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
429 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
430 pp_c_left_paren (pp);
431 if (parms == void_list_node)
432 pp_c_identifier (pp, "void");
436 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
439 pp_separate_with (pp, ',');
441 pp_declaration_specifiers
442 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
444 pp_declarator (pp, parms);
446 pp_abstract_declarator (pp, TREE_VALUE (parms));
449 pp_c_right_paren (pp);
452 /* abstract-declarator:
454 pointer(opt) direct-abstract-declarator */
457 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
459 if (TREE_CODE (t) == POINTER_TYPE)
461 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
462 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
463 pp_c_right_paren (pp);
467 pp_direct_abstract_declarator (pp, t);
470 /* direct-abstract-declarator:
471 ( abstract-declarator )
472 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
473 direct-abstract-declarator(opt) [ * ]
474 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
477 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
479 switch (TREE_CODE (t))
482 pp_abstract_declarator (pp, t);
486 pp_c_parameter_type_list (pp, t);
487 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
491 pp_c_left_bracket (pp);
492 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
493 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
494 pp_c_right_bracket (pp);
495 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
498 case IDENTIFIER_NODE:
512 pp_unsupported_tree (pp, t);
518 specifier-qualifier-list abstract-declarator(opt) */
521 pp_c_type_id (c_pretty_printer *pp, tree t)
523 pp_c_specifier_qualifier_list (pp, t);
524 pp_abstract_declarator (pp, t);
527 /* storage-class-specifier:
535 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
537 if (TREE_CODE (t) == TYPE_DECL)
538 pp_c_identifier (pp, "typedef");
541 if (DECL_REGISTER (t))
542 pp_c_identifier (pp, "register");
543 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
544 pp_c_identifier (pp, "static");
548 /* function-specifier:
552 pp_c_function_specifier (c_pretty_printer *pp, tree t)
554 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
555 pp_c_identifier (pp, "inline");
558 /* declaration-specifiers:
559 storage-class-specifier declaration-specifiers(opt)
560 type-specifier declaration-specifiers(opt)
561 type-qualifier declaration-specifiers(opt)
562 function-specifier declaration-specifiers(opt) */
565 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
567 pp_storage_class_specifier (pp, t);
568 pp_function_specifier (pp, t);
569 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
575 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
576 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
577 direct-declarator [ type-qualifier-list static assignment-expression ]
578 direct-declarator [ type-qualifier-list * ]
579 direct-declarator ( parameter-type-list )
580 direct-declarator ( identifier-list(opt) ) */
583 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
585 switch (TREE_CODE (t))
592 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
593 pp_c_tree_decl_identifier (pp, t);
598 pp_abstract_declarator (pp, TREE_TYPE (t));
602 pp_parameter_list (pp, t);
603 pp_abstract_declarator (pp, TREE_TYPE (t));
607 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
608 pp_c_tree_decl_identifier (pp, t);
609 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
610 pp_abstract_declarator (pp, TREE_TYPE (t));
613 pp_parameter_list (pp, t);
614 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
626 pp_unsupported_tree (pp, t);
633 pointer(opt) direct-declarator */
636 pp_c_declarator (c_pretty_printer *pp, tree t)
638 switch (TREE_CODE (t))
654 pp_direct_declarator (pp, t);
659 pp_unsupported_tree (pp, t);
665 declaration-specifiers init-declarator-list(opt) ; */
668 pp_c_declaration (c_pretty_printer *pp, tree t)
670 pp_declaration_specifiers (pp, t);
671 pp_c_init_declarator (pp, t);
674 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
677 pp_c_attributes (c_pretty_printer *pp, tree attributes)
679 if (attributes == NULL_TREE)
682 pp_c_identifier (pp, "__attribute__");
683 pp_c_left_paren (pp);
684 pp_c_left_paren (pp);
685 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
687 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
688 if (TREE_VALUE (attributes))
689 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
691 if (TREE_CHAIN (attributes))
692 pp_separate_with (pp, ',');
694 pp_c_right_paren (pp);
695 pp_c_right_paren (pp);
698 /* function-definition:
699 declaration-specifiers declarator compound-statement */
702 pp_c_function_definition (c_pretty_printer *pp, tree t)
704 pp_declaration_specifiers (pp, t);
705 pp_declarator (pp, t);
706 pp_needs_newline (pp) = true;
707 pp_statement (pp, DECL_SAVED_TREE (t));
715 /* Print out a c-char. */
718 pp_c_char (c_pretty_printer *pp, int c)
723 pp_string (pp, "\\n");
726 pp_string (pp, "\\t");
729 pp_string (pp, "\\v");
732 pp_string (pp, "\\b");
735 pp_string (pp, "\\r");
738 pp_string (pp, "\\f");
741 pp_string (pp, "\\a");
744 pp_string (pp, "\\\\");
747 pp_string (pp, "\\'");
750 pp_string (pp, "\\\"");
754 pp_character (pp, c);
756 pp_scalar (pp, "\\%03o", (unsigned) c);
761 /* Print out a STRING literal. */
764 pp_c_string_literal (c_pretty_printer *pp, tree s)
766 const char *p = TREE_STRING_POINTER (s);
767 int n = TREE_STRING_LENGTH (s) - 1;
770 for (i = 0; i < n; ++i)
771 pp_c_char (pp, p[i]);
775 /* Pretty-print an INTEGER literal. */
778 pp_c_integer_constant (c_pretty_printer *pp, tree i)
780 tree type = TREE_TYPE (i);
782 if (TREE_INT_CST_HIGH (i) == 0)
783 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
786 if (tree_int_cst_sgn (i) < 0)
789 i = build_int_2 (-TREE_INT_CST_LOW (i),
790 ~TREE_INT_CST_HIGH (i) + !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 pp_c_left_paren (pp);
1029 pp_statement (pp, STMT_EXPR_STMT (e));
1030 pp_c_right_paren (pp);
1034 /* FIXME: Make sure we won't get into an infinie loop. */
1035 pp_c_left_paren (pp);
1036 pp_expression (pp, e);
1037 pp_c_right_paren (pp);
1042 /* Print out a C initializer -- also support C compound-literals.
1044 assignment-expression:
1045 { initializer-list }
1046 { initializer-list , } */
1049 pp_c_initializer (c_pretty_printer *pp, tree e)
1051 if (TREE_CODE (e) == CONSTRUCTOR)
1052 pp_c_brace_enclosed_initializer_list (pp, e);
1054 pp_expression (pp, e);
1059 declarator = initializer */
1062 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1064 pp_declarator (pp, t);
1065 /* We don't want to output function definitions here. There are handled
1066 elsewhere (and the syntactic form is bogus anyway). */
1067 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1069 tree init = DECL_INITIAL (t);
1070 /* This C++ bit is handled here because it is easier to do so.
1071 In templates, the C++ parser builds a TREE_LIST for a
1072 direct-initialization; the TREE_PURPOSE is the variable to
1073 initialize and the TREE_VALUE is the initializer. */
1074 if (TREE_CODE (init) == TREE_LIST)
1076 pp_c_left_paren (pp);
1077 pp_expression (pp, TREE_VALUE (init));
1078 pp_right_paren (pp);
1085 pp_c_initializer (pp, init);
1090 /* initializer-list:
1091 designation(opt) initializer
1092 initializer-list , designation(opt) initializer
1099 designator-list designator
1102 [ constant-expression ]
1106 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1108 tree type = TREE_TYPE (e);
1109 const enum tree_code code = TREE_CODE (type);
1117 tree init = TREE_OPERAND (e, 0);
1118 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1120 if (code == RECORD_TYPE || code == UNION_TYPE)
1123 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1127 pp_c_left_bracket (pp);
1128 if (TREE_PURPOSE (init))
1129 pp_c_constant (pp, TREE_PURPOSE (init));
1130 pp_c_right_bracket (pp);
1132 pp_c_whitespace (pp);
1134 pp_c_whitespace (pp);
1135 pp_initializer (pp, TREE_VALUE (init));
1136 if (TREE_CHAIN (init))
1137 pp_separate_with (pp, ',');
1143 if (TREE_CODE (e) == VECTOR_CST)
1144 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1145 else if (TREE_CODE (e) == CONSTRUCTOR)
1146 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1152 if (TREE_CODE (e) == CONSTRUCTOR)
1153 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1154 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1156 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1157 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1158 pp_separate_with (pp, ',');
1159 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1169 pp_unsupported_tree (pp, type);
1172 /* Pretty-print a brace-enclosed initializer-list. */
1175 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1177 pp_c_left_brace (pp);
1178 pp_c_initializer_list (pp, l);
1179 pp_c_right_brace (pp);
1183 /* This is a convenient function, used to bridge gap between C and C++
1190 pp_c_id_expression (c_pretty_printer *pp, tree t)
1192 switch (TREE_CODE (t))
1201 pp_c_tree_decl_identifier (pp, t);
1204 case IDENTIFIER_NODE:
1205 pp_c_tree_identifier (pp, t);
1209 pp_unsupported_tree (pp, t);
1214 /* postfix-expression:
1216 postfix-expression [ expression ]
1217 postfix-expression ( argument-expression-list(opt) )
1218 postfix-expression . identifier
1219 postfix-expression -> identifier
1220 postfix-expression ++
1221 postfix-expression --
1222 ( type-name ) { initializer-list }
1223 ( type-name ) { initializer-list , } */
1226 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1228 enum tree_code code = TREE_CODE (e);
1231 case POSTINCREMENT_EXPR:
1232 case POSTDECREMENT_EXPR:
1233 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1234 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1238 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1243 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1244 pp_c_left_bracket (pp);
1245 pp_expression (pp, TREE_OPERAND (e, 1));
1246 pp_c_right_bracket (pp);
1250 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1251 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1255 pp_c_identifier (pp, "__builtin_abs");
1256 pp_c_left_paren (pp);
1257 pp_expression (pp, TREE_OPERAND (e, 0));
1258 pp_c_right_paren (pp);
1263 tree object = TREE_OPERAND (e, 0);
1264 if (TREE_CODE (object) == INDIRECT_REF)
1266 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1271 pp_postfix_expression (pp, object);
1274 pp_expression (pp, TREE_OPERAND (e, 1));
1281 pp_c_compound_literal (pp, e);
1284 case COMPOUND_LITERAL_EXPR:
1285 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1288 pp_initializer (pp, e);
1292 pp_c_identifier (pp, "__builtin_va_arg");
1293 pp_c_left_paren (pp);
1294 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1295 pp_separate_with (pp, ',');
1296 pp_type_id (pp, TREE_TYPE (e));
1297 pp_c_right_paren (pp);
1301 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1303 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1306 /* else fall through. */
1309 pp_primary_expression (pp, e);
1314 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1317 pp_c_expression_list (c_pretty_printer *pp, tree e)
1319 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1321 pp_expression (pp, TREE_VALUE (e));
1323 pp_separate_with (pp, ',');
1327 /* Print out an expression-list in parens, as in a function call. */
1330 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1332 pp_c_left_paren (pp);
1333 if (t && TREE_CODE (t) == TREE_LIST)
1334 pp_c_expression_list (pp, t);
1335 pp_c_right_paren (pp);
1338 /* unary-expression:
1342 unary-operator cast-expression
1343 sizeof unary-expression
1346 unary-operator: one of
1351 __alignof__ unary-expression
1352 __alignof__ ( type-id )
1353 __real__ unary-expression
1354 __imag__ unary-expression */
1357 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1359 enum tree_code code = TREE_CODE (e);
1362 case PREINCREMENT_EXPR:
1363 case PREDECREMENT_EXPR:
1364 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1365 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1372 case TRUTH_NOT_EXPR:
1374 /* String literal are used by address. */
1375 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1377 else if (code == INDIRECT_REF)
1379 else if (code == NEGATE_EXPR)
1381 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1383 else if (code == TRUTH_NOT_EXPR)
1384 pp_exclamation (pp);
1385 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1390 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1391 pp_c_whitespace (pp);
1392 if (TYPE_P (TREE_OPERAND (e, 0)))
1393 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1395 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1400 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1401 pp_c_whitespace (pp);
1402 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1406 pp_postfix_expression (pp, e);
1413 ( type-name ) cast-expression */
1416 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1418 switch (TREE_CODE (e))
1421 case FIX_TRUNC_EXPR:
1423 pp_c_type_cast (pp, TREE_TYPE (e));
1424 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1428 pp_unary_expression (pp, e);
1432 /* multiplicative-expression:
1434 multiplicative-expression * cast-expression
1435 multiplicative-expression / cast-expression
1436 multiplicative-expression % cast-expression */
1439 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1441 enum tree_code code = TREE_CODE (e);
1445 case TRUNC_DIV_EXPR:
1446 case TRUNC_MOD_EXPR:
1447 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1448 pp_c_whitespace (pp);
1449 if (code == MULT_EXPR)
1451 else if (code == TRUNC_DIV_EXPR)
1455 pp_c_whitespace (pp);
1456 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1460 pp_c_cast_expression (pp, e);
1465 /* additive-expression:
1466 multiplicative-expression
1467 additive-expression + multiplicative-expression
1468 additive-expression - multiplicative-expression */
1471 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1473 enum tree_code code = TREE_CODE (e);
1478 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1479 pp_c_whitespace (pp);
1480 if (code == PLUS_EXPR)
1484 pp_c_whitespace (pp);
1485 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1489 pp_multiplicative_expression (pp, e);
1494 /* additive-expression:
1496 shift-expression << additive-expression
1497 shift-expression >> additive-expression */
1500 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1502 enum tree_code code = TREE_CODE (e);
1507 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1508 pp_c_whitespace (pp);
1509 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1510 pp_c_whitespace (pp);
1511 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1515 pp_c_additive_expression (pp, e);
1519 /* relational-expression:
1521 relational-expression < shift-expression
1522 relational-expression > shift-expression
1523 relational-expression <= shift-expression
1524 relational-expression >= shift-expression */
1527 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1529 enum tree_code code = TREE_CODE (e);
1536 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1537 pp_c_whitespace (pp);
1538 if (code == LT_EXPR)
1540 else if (code == GT_EXPR)
1542 else if (code == LE_EXPR)
1543 pp_identifier (pp, "<=");
1544 else if (code == GE_EXPR)
1545 pp_identifier (pp, ">=");
1546 pp_c_whitespace (pp);
1547 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1551 pp_c_shift_expression (pp, e);
1556 /* equality-expression:
1557 relational-expression
1558 equality-expression == relational-expression
1559 equality-equality != relational-expression */
1562 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1564 enum tree_code code = TREE_CODE (e);
1569 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1570 pp_c_whitespace (pp);
1571 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1572 pp_c_whitespace (pp);
1573 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1577 pp_c_relational_expression (pp, e);
1584 AND-expression & equality-equality */
1587 pp_c_and_expression (c_pretty_printer *pp, tree e)
1589 if (TREE_CODE (e) == BIT_AND_EXPR)
1591 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1592 pp_c_whitespace (pp);
1594 pp_c_whitespace (pp);
1595 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1598 pp_c_equality_expression (pp, e);
1601 /* exclusive-OR-expression:
1603 exclusive-OR-expression ^ AND-expression */
1606 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1608 if (TREE_CODE (e) == BIT_XOR_EXPR)
1610 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1611 pp_c_maybe_whitespace (pp);
1613 pp_c_whitespace (pp);
1614 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1617 pp_c_and_expression (pp, e);
1620 /* inclusive-OR-expression:
1621 exclusive-OR-expression
1622 inclusive-OR-expression | exclusive-OR-expression */
1625 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1627 if (TREE_CODE (e) == BIT_IOR_EXPR)
1629 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1630 pp_c_whitespace (pp);
1632 pp_c_whitespace (pp);
1633 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1636 pp_c_exclusive_or_expression (pp, e);
1639 /* logical-AND-expression:
1640 inclusive-OR-expression
1641 logical-AND-expression && inclusive-OR-expression */
1644 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1646 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1648 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1649 pp_c_whitespace (pp);
1650 pp_identifier (pp, "&&");
1651 pp_c_whitespace (pp);
1652 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1655 pp_c_inclusive_or_expression (pp, e);
1658 /* logical-OR-expression:
1659 logical-AND-expression
1660 logical-OR-expression || logical-AND-expression */
1663 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1665 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1667 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1668 pp_c_whitespace (pp);
1669 pp_identifier (pp, "||");
1670 pp_c_whitespace (pp);
1671 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1674 pp_c_logical_and_expression (pp, e);
1677 /* conditional-expression:
1678 logical-OR-expression
1679 logical-OR-expression ? expression : conditional-expression */
1682 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1684 if (TREE_CODE (e) == COND_EXPR)
1686 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1687 pp_c_whitespace (pp);
1689 pp_c_whitespace (pp);
1690 pp_expression (pp, TREE_OPERAND (e, 1));
1691 pp_c_whitespace (pp);
1693 pp_c_whitespace (pp);
1694 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1697 pp_c_logical_or_expression (pp, e);
1701 /* assignment-expression:
1702 conditional-expression
1703 unary-expression assignment-operator assignment-expression
1705 assignment-expression: one of
1706 = *= /= %= += -= >>= <<= &= ^= |= */
1709 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1711 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1713 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1714 pp_c_whitespace (pp);
1717 pp_c_expression (pp, TREE_OPERAND (e, 1));
1720 pp_c_conditional_expression (pp, e);
1724 assignment-expression
1725 expression , assignment-expression
1727 Implementation note: instead of going through the usual recursion
1728 chain, I take the liberty of dispatching nodes to the appropriate
1729 functions. This makes some redundancy, but it worths it. That also
1730 prevents a possible infinite recursion between pp_c_primary_expression ()
1731 and pp_c_expression (). */
1734 pp_c_expression (c_pretty_printer *pp, tree e)
1736 switch (TREE_CODE (e))
1739 pp_c_integer_constant (pp, e);
1743 pp_c_floating_constant (pp, e);
1747 pp_c_string_literal (pp, e);
1750 case IDENTIFIER_NODE:
1760 pp_primary_expression (pp, e);
1763 case POSTINCREMENT_EXPR:
1764 case POSTDECREMENT_EXPR:
1774 case COMPOUND_LITERAL_EXPR:
1776 pp_postfix_expression (pp, e);
1784 case TRUTH_NOT_EXPR:
1785 case PREINCREMENT_EXPR:
1786 case PREDECREMENT_EXPR:
1791 pp_c_unary_expression (pp, e);
1795 case FIX_TRUNC_EXPR:
1797 pp_c_cast_expression (pp, e);
1801 case TRUNC_MOD_EXPR:
1802 case TRUNC_DIV_EXPR:
1803 pp_multiplicative_expression (pp, e);
1808 pp_c_shift_expression (pp, e);
1815 pp_c_relational_expression (pp, e);
1819 pp_c_and_expression (pp, e);
1823 pp_c_exclusive_or_expression (pp, e);
1827 pp_c_inclusive_or_expression (pp, e);
1830 case TRUTH_ANDIF_EXPR:
1831 pp_c_logical_and_expression (pp, e);
1834 case TRUTH_ORIF_EXPR:
1835 pp_c_logical_or_expression (pp, e);
1840 pp_c_equality_expression (pp, e);
1844 pp_conditional_expression (pp, e);
1849 pp_c_additive_expression (pp, e);
1854 pp_assignment_expression (pp, e);
1858 pp_c_left_paren (pp);
1859 pp_expression (pp, TREE_OPERAND (e, 0));
1860 pp_separate_with (pp, ',');
1861 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1862 pp_c_right_paren (pp);
1866 case NON_LVALUE_EXPR:
1869 pp_expression (pp, TREE_OPERAND (e, 0));
1873 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1877 pp_unsupported_tree (pp, e);
1889 expression-statement
1895 pp_c_statement (c_pretty_printer *pp, tree stmt)
1897 enum tree_code code;
1902 if (pp_needs_newline (pp))
1903 pp_newline_and_indent (pp, 0);
1905 code = TREE_CODE (stmt);
1908 /* expression-statement:
1909 expression(opt) ; */
1911 pp_expression (pp, EXPR_STMT_EXPR (stmt));
1912 pp_c_semicolon (pp);
1913 pp_needs_newline (pp) = true;
1917 pp_c_identifier (pp, "switch");
1919 pp_c_left_paren (pp);
1920 pp_expression (pp, SWITCH_COND (stmt));
1921 pp_c_right_paren (pp);
1922 pp_indentation (pp) += 3;
1923 pp_needs_newline (pp) = true;
1924 pp_statement (pp, SWITCH_BODY (stmt));
1925 pp_newline_and_indent (pp, -3);
1928 /* iteration-statement:
1929 while ( expression ) statement
1930 do statement while ( expression ) ;
1931 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1932 for ( declaration expression(opt) ; expression(opt) ) statement */
1934 pp_c_identifier (pp, "while");
1936 pp_c_left_paren (pp);
1937 pp_expression (pp, WHILE_COND (stmt));
1938 pp_c_right_paren (pp);
1939 pp_newline_and_indent (pp, 3);
1940 pp_statement (pp, WHILE_BODY (stmt));
1941 pp_indentation (pp) -= 3;
1942 pp_needs_newline (pp) = true;
1946 pp_c_identifier (pp, "do");
1947 pp_newline_and_indent (pp, 3);
1948 pp_statement (pp, DO_BODY (stmt));
1949 pp_newline_and_indent (pp, -3);
1950 pp_c_identifier (pp, "while");
1952 pp_c_left_paren (pp);
1953 pp_expression (pp, DO_COND (stmt));
1954 pp_c_right_paren (pp);
1955 pp_c_semicolon (pp);
1956 pp_needs_newline (pp) = true;
1960 pp_c_identifier (pp, "for");
1962 pp_c_left_paren (pp);
1963 if (FOR_INIT_STMT (stmt))
1964 pp_statement (pp, FOR_INIT_STMT (stmt));
1966 pp_c_semicolon (pp);
1967 pp_needs_newline (pp) = false;
1968 pp_c_whitespace (pp);
1969 if (FOR_COND (stmt))
1970 pp_expression (pp, FOR_COND (stmt));
1971 pp_c_semicolon (pp);
1972 pp_needs_newline (pp) = false;
1973 pp_c_whitespace (pp);
1974 if (FOR_EXPR (stmt))
1975 pp_expression (pp, FOR_EXPR (stmt));
1976 pp_c_right_paren (pp);
1977 pp_newline_and_indent (pp, 3);
1978 pp_statement (pp, FOR_BODY (stmt));
1979 pp_indentation (pp) -= 3;
1980 pp_needs_newline (pp) = true;
1986 return expression(opt) ; */
1989 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
1990 pp_c_semicolon (pp);
1991 pp_needs_newline (pp) = true;
1995 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2001 /* Initialize the PRETTY-PRINTER for handling C codes. */
2004 pp_c_pretty_printer_init (c_pretty_printer *pp)
2006 pp->offset_list = 0;
2008 pp->declaration = pp_c_declaration;
2009 pp->declaration_specifiers = pp_c_declaration_specifiers;
2010 pp->declarator = pp_c_declarator;
2011 pp->direct_declarator = pp_c_direct_declarator;
2012 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2013 pp->abstract_declarator = pp_c_abstract_declarator;
2014 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2015 pp->ptr_operator = pp_c_pointer;
2016 pp->parameter_list = pp_c_parameter_type_list;
2017 pp->type_id = pp_c_type_id;
2018 pp->simple_type_specifier = pp_c_type_specifier;
2019 pp->function_specifier = pp_c_function_specifier;
2020 pp->storage_class_specifier = pp_c_storage_class_specifier;
2022 pp->statement = pp_c_statement;
2024 pp->id_expression = pp_c_id_expression;
2025 pp->primary_expression = pp_c_primary_expression;
2026 pp->postfix_expression = pp_c_postfix_expression;
2027 pp->unary_expression = pp_c_unary_expression;
2028 pp->initializer = pp_c_initializer;
2029 pp->multiplicative_expression = pp_c_multiplicative_expression;
2030 pp->conditional_expression = pp_c_conditional_expression;
2031 pp->assignment_expression = pp_c_assignment_expression;
2032 pp->expression = pp_c_expression;
2036 /* Print the tree T in full, on file FILE. */
2039 print_c_tree (FILE *file, tree t)
2041 static c_pretty_printer pp_rec;
2042 static bool initialized = 0;
2043 c_pretty_printer *pp = &pp_rec;
2048 pp_construct (pp_base (pp), NULL, 0);
2049 pp_c_pretty_printer_init (pp);
2050 pp_needs_newline (pp) = true;
2052 pp_base (pp)->buffer->stream = file;
2054 pp_statement (pp, t);
2060 /* Print the tree T in full, on stderr. */
2063 debug_c_tree (tree t)
2065 print_c_tree (stderr, t);
2066 fputc ('\n', stderr);
2069 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2070 up of T's memory address. */
2073 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2081 name = IDENTIFIER_POINTER (DECL_NAME (t));
2084 static char xname[8];
2085 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2089 pp_c_identifier (pp, name);