1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003 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"
30 /* The pretty-printer code is primarily designed to closely follow
31 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
32 codes we used to have in the past. Following a structured
33 approach (preferaably the official grammars) is believed to make it
34 much easier o add extensions and nifty pretty-printing effects that
35 takes expresssion or declaration contexts into account. */
38 static void pp_c_char (c_pretty_printer, int);
39 static void pp_c_character_literal (c_pretty_printer, tree);
40 static void pp_c_bool_literal (c_pretty_printer, tree);
41 static bool pp_c_enumerator (c_pretty_printer, tree);
42 static void pp_c_integer_literal (c_pretty_printer, tree);
43 static void pp_c_real_literal (c_pretty_printer, tree);
44 static void pp_c_string_literal (c_pretty_printer, tree);
46 static void pp_c_primary_expression (c_pretty_printer, tree);
48 /* postfix-expression */
49 static void pp_c_initializer_list (c_pretty_printer, tree);
51 static void pp_c_unary_expression (c_pretty_printer, tree);
52 static void pp_c_multiplicative_expression (c_pretty_printer, tree);
53 static void pp_c_additive_expression (c_pretty_printer, tree);
54 static void pp_c_shift_expression (c_pretty_printer, tree);
55 static void pp_c_relational_expression (c_pretty_printer, tree);
56 static void pp_c_equality_expression (c_pretty_printer, tree);
57 static void pp_c_and_expression (c_pretty_printer, tree);
58 static void pp_c_exclusive_or_expression (c_pretty_printer, tree);
59 static void pp_c_inclusive_or_expression (c_pretty_printer, tree);
60 static void pp_c_logical_and_expression (c_pretty_printer, tree);
61 static void pp_c_conditional_expression (c_pretty_printer, tree);
62 static void pp_c_assignment_expression (c_pretty_printer, tree);
65 static void pp_c_declaration_specifiers (c_pretty_printer, tree);
66 static void pp_c_init_declarator (c_pretty_printer, tree);
67 static void pp_c_declarator (c_pretty_printer, tree);
68 static void pp_c_direct_declarator (c_pretty_printer, tree);
69 static void pp_c_abstract_declarator (c_pretty_printer, tree);
70 static void pp_c_specifier_qualifier_list (c_pretty_printer, tree);
71 static void pp_c_simple_type_specifier (c_pretty_printer, tree);
72 static void pp_c_parameter_declaration (c_pretty_printer, tree);
73 static void pp_c_type_id (c_pretty_printer, tree);
74 static void pp_c_storage_class_specifier (c_pretty_printer, tree);
75 static void pp_c_function_specifier (c_pretty_printer, tree);
77 #define pp_buffer(PP) pp_base (PP)->buffer
78 #define pp_newline(PP) (pp_newline) (pp_base (PP))
83 /* Print out CV-qualifiers. Take care of possible extensions.
90 pp_c_cv_qualifier (c_pretty_printer ppi, int cv)
92 if (cv & TYPE_QUAL_CONST)
93 pp_c_identifier (ppi, "const");
94 if (cv & TYPE_QUAL_VOLATILE)
95 pp_c_identifier (ppi, "volatile");
96 if (cv & TYPE_QUAL_RESTRICT)
97 pp_c_identifier (ppi, flag_isoc99 ? "restrict" : "__restrict__");
101 simple-type-specifier:
117 simple-type-specifier:
121 pp_c_simple_type_specifier (c_pretty_printer ppi, tree t)
123 const enum tree_code code = TREE_CODE (t);
127 pp_c_identifier (ppi, "<type-error>");
132 pp_c_identifier (ppi, "<unkown-type>");
136 case IDENTIFIER_NODE:
137 pp_c_tree_identifier (ppi, t);
145 pp_c_tree_identifier (ppi, DECL_NAME (t));
150 pp_c_simple_type_specifier (ppi, TYPE_MAIN_VARIANT (TREE_TYPE (t)));
151 if (code == COMPLEX_TYPE)
152 pp_c_identifier (ppi, flag_isoc99 ? "_Complex" : "__complex__");
153 else if (code == VECTOR_TYPE)
154 pp_c_identifier (ppi, "__vector__");
159 pp_c_tree_identifier (ppi, DECL_NAME (t));
161 pp_c_identifier (ppi, "<typedef-error>");
167 if (code == UNION_TYPE)
168 pp_c_identifier (ppi, "union");
169 else if (code == RECORD_TYPE)
170 pp_c_identifier (ppi, "struct");
171 else if (code == ENUMERAL_TYPE)
172 pp_c_identifier (ppi, "enum");
174 pp_c_identifier (ppi, "<tag-error>");
177 pp_c_tree_identifier (ppi, TYPE_NAME (t));
179 pp_c_identifier (ppi, "<anonymous>");
183 pp_unsupported_tree (ppi, t);
187 /* specifier-qualifier-list:
188 type-specifier specifier-qualifier-list-opt
189 cv-qualifier specifier-qualifier-list-opt */
191 pp_c_specifier_qualifier_list (c_pretty_printer ppi, tree t)
193 pp_c_simple_type_specifier (ppi, TYPE_MAIN_VARIANT (TREE_TYPE (t)));
194 pp_c_cv_qualifier (ppi, TYPE_QUALS (t));
198 pp_c_abstract_declarator (c_pretty_printer ppi, tree t)
200 pp_unsupported_tree (ppi, t);
205 pp_c_type_id (c_pretty_printer ppi, tree t)
207 pp_c_specifier_qualifier_list (ppi, t);
208 pp_c_abstract_declarator (ppi, t);
212 pp_c_storage_class_specifier (c_pretty_printer pp, tree t)
214 if (TREE_CODE (t) == TYPE_DECL)
215 pp_c_identifier (pp, "typedef");
216 else if (DECL_REGISTER (t))
217 pp_c_identifier (pp, "register");
221 pp_c_function_specifier (c_pretty_printer pp, tree t)
223 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
224 pp_c_identifier (pp, "inline");
228 pp_c_declaration_specifiers (c_pretty_printer pp, tree t)
230 pp_c_storage_class_specifier (pp, t);
231 pp_c_function_specifier (pp, t);
232 pp_type_specifier (pp, TYPE_MAIN_VARIANT (TREE_TYPE (t)));
233 pp_c_cv_qualifier (pp, TYPE_QUALS (TREE_TYPE (t)));
237 pp_c_direct_declarator (c_pretty_printer pp, tree t)
239 pp_unsupported_tree (pp, t);
243 pp_c_declarator (c_pretty_printer pp, tree t)
245 pp_unsupported_tree (pp, t);
249 pp_c_init_declarator (c_pretty_printer pp, tree t)
251 pp_declarator (pp, t);
252 if (DECL_INITIAL (t))
257 pp_c_initializer (pp, DECL_INITIAL (t));
262 pp_c_declaration (c_pretty_printer pp, tree t)
264 pp_declaration_specifiers (pp, t);
265 pp_c_init_declarator (pp, t);
269 pp_c_parameter_declaration (c_pretty_printer pp, tree t)
271 pp_unsupported_tree (pp, t);
274 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
276 pp_c_attributes (c_pretty_printer pp, tree attributes)
278 if (attributes == NULL_TREE)
281 pp_c_identifier (pp, "__attribute__");
282 pp_c_left_paren (pp);
283 pp_c_left_paren (pp);
284 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
286 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
287 if (TREE_VALUE (attributes))
289 pp_c_left_paren (pp);
290 pp_c_expression_list (pp, TREE_VALUE (attributes));
291 pp_c_right_paren (pp);
294 if (TREE_CHAIN (attributes))
295 pp_separate_with (pp, ',');
297 pp_c_right_paren (pp);
298 pp_c_right_paren (pp);
304 /* Print out a c-char. */
306 pp_c_char (c_pretty_printer ppi, int c)
311 pp_identifier (ppi, "\\n");
314 pp_identifier (ppi, "\\t");
317 pp_identifier (ppi, "\\v");
320 pp_identifier (ppi, "\\b");
323 pp_identifier (ppi, "\\r");
326 pp_identifier (ppi, "\\f");
329 pp_identifier (ppi, "\\a");
332 pp_identifier (ppi, "\\\\");
335 pp_identifier (ppi, "\\'");
338 pp_identifier (ppi, "\\\"");
342 pp_character (&ppi->base, c);
344 pp_scalar (ppi, "\\%03o", (unsigned) c);
349 /* Print out a STRING literal. */
351 pp_c_string_literal (c_pretty_printer ppi, tree s)
353 const char *p = TREE_STRING_POINTER (s);
354 int n = TREE_STRING_LENGTH (s) - 1;
356 pp_doublequote (ppi);
357 for (i = 0; i < n; ++i)
358 pp_c_char (ppi, p[i]);
359 pp_doublequote (ppi);
362 /* Print out a CHARACTER literal. */
364 pp_c_character_literal (c_pretty_printer ppi, tree c)
367 pp_c_char (ppi, tree_low_cst (c, 0));
371 /* Print out a BOOLEAN literal. */
373 pp_c_bool_literal (c_pretty_printer ppi, tree b)
375 if (b == boolean_false_node || integer_zerop (b))
377 if (c_dialect_cxx ())
378 pp_c_identifier (ppi, "false");
379 else if (flag_isoc99)
380 pp_c_identifier (ppi, "_False");
382 pp_unsupported_tree (ppi, b);
384 else if (b == boolean_true_node)
386 if (c_dialect_cxx ())
387 pp_c_identifier (ppi, "true");
388 else if (flag_isoc99)
389 pp_c_identifier (ppi, "_True");
391 pp_unsupported_tree (ppi, b);
394 pp_unsupported_tree (ppi, b);
397 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
398 false; that means the value was obtained by a cast, in which case
399 print out the type-id part of the cast-expression -- the casted value
400 is then printed by pp_c_integer_literal. */
402 pp_c_enumerator (c_pretty_printer ppi, tree e)
404 tree type = TREE_TYPE (e);
407 /* Find the name of this constant. */
408 for (value = TYPE_VALUES (type);
409 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
410 value = TREE_CHAIN (value))
413 if (value != NULL_TREE)
414 pp_c_tree_identifier (ppi, TREE_PURPOSE (value));
417 /* Value must have been cast. */
418 pp_c_left_paren (ppi);
419 pp_type_id (ppi, type);
420 pp_c_right_paren (ppi);
427 /* Print out an INTEGER constant value. */
429 pp_c_integer_literal (c_pretty_printer ppi, tree i)
431 tree type = TREE_TYPE (i);
433 if (type == boolean_type_node)
434 pp_c_bool_literal (ppi, i);
435 else if (type == char_type_node)
436 pp_c_character_literal (ppi, i);
437 else if (TREE_CODE (type) == ENUMERAL_TYPE
438 && pp_c_enumerator (ppi, i))
442 if (host_integerp (i, 0))
443 pp_wide_integer (ppi, TREE_INT_CST_LOW (i));
446 if (tree_int_cst_sgn (i) < 0)
448 pp_c_char (ppi, '-');
449 i = build_int_2 (-TREE_INT_CST_LOW (i),
450 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
451 sprintf (pp_buffer (ppi)->digit_buffer,
452 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
453 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
454 pp_identifier (ppi, pp_buffer (ppi)->digit_buffer);
461 /* Print out a REAL value. */
463 pp_c_real_literal (c_pretty_printer ppi, tree r)
465 real_to_decimal (pp_buffer (ppi)->digit_buffer, &TREE_REAL_CST (r),
466 sizeof (pp_buffer (ppi)->digit_buffer), 0, 1);
467 pp_identifier (ppi, pp_buffer(ppi)->digit_buffer);
472 pp_c_literal (c_pretty_printer ppi, tree e)
474 switch (TREE_CODE (e))
477 pp_c_integer_literal (ppi, e);
481 pp_c_real_literal (ppi, e);
485 pp_c_string_literal (ppi, e);
489 pp_unsupported_tree (ppi, e);
494 /* Pretty-print a C primary-expression. */
496 pp_c_primary_expression (c_pretty_printer ppi, tree e)
498 switch (TREE_CODE (e))
508 case IDENTIFIER_NODE:
509 pp_c_tree_identifier (ppi, e);
513 pp_c_identifier (ppi, "<erroneous-expression>");
517 pp_c_identifier (ppi, "<return-value>");
523 pp_c_literal (ppi, e);
527 pp_c_left_paren (ppi);
528 pp_c_identifier (ppi, "__builtin_memcpy");
529 pp_c_left_paren (ppi);
531 pp_c_primary_expression (ppi, TREE_OPERAND (e, 0));
532 pp_separate_with (ppi, ',');
534 pp_initializer (ppi, TREE_OPERAND (e, 1));
535 if (TREE_OPERAND (e, 2))
537 pp_separate_with (ppi, ',');
538 pp_c_expression (ppi, TREE_OPERAND (e, 2));
540 pp_c_right_paren (ppi);
543 pp_c_left_paren (ppi);
544 pp_statement (ppi, STMT_EXPR_STMT (e));
545 pp_c_right_paren (ppi);
549 /* Make sure this call won't cause any infinite loop. */
550 pp_c_left_paren (ppi);
551 pp_c_expression (ppi, e);
552 pp_c_right_paren (ppi);
557 /* Print out a C initializer -- also support C compound-literals. */
559 pp_c_initializer (c_pretty_printer ppi, tree e)
561 if (TREE_CODE (e) == CONSTRUCTOR)
563 enum tree_code code = TREE_CODE (TREE_TYPE (e));
564 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
567 pp_c_initializer_list (ppi, e);
568 pp_right_brace (ppi);
571 pp_unsupported_tree (ppi, TREE_OPERAND (e, 1));
574 pp_assignment_expression (ppi, e);
578 pp_c_initializer_list (c_pretty_printer ppi, tree e)
580 tree type = TREE_TYPE (e);
581 const enum tree_code code = TREE_CODE (type);
583 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
585 tree init = TREE_OPERAND (e, 1);
586 for (; init != NULL_TREE; init = TREE_CHAIN (init))
588 if (code == RECORD_TYPE || code == UNION_TYPE)
591 pp_c_primary_expression (ppi, TREE_PURPOSE (init));
595 pp_c_left_bracket (ppi);
596 if (TREE_PURPOSE (init))
597 pp_c_literal (ppi, TREE_PURPOSE (init));
598 pp_c_right_bracket (ppi);
600 pp_c_whitespace (ppi);
602 pp_c_whitespace (ppi);
603 pp_initializer (ppi, TREE_VALUE (init));
604 if (TREE_CHAIN (init))
605 pp_separate_with (ppi, ',');
609 pp_unsupported_tree (ppi, type);
613 pp_c_postfix_expression (c_pretty_printer ppi, tree e)
615 enum tree_code code = TREE_CODE (e);
618 case POSTINCREMENT_EXPR:
619 case POSTDECREMENT_EXPR:
620 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
621 pp_identifier (ppi, code == POSTINCREMENT_EXPR ? "++" : "--");
625 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
630 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
631 pp_c_left_bracket (ppi);
632 pp_c_expression (ppi, TREE_OPERAND (e, 1));
633 pp_c_right_bracket (ppi);
637 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
638 pp_c_left_paren (ppi);
639 pp_c_expression_list (ppi, TREE_OPERAND (e, 1));
640 pp_c_right_paren (ppi);
645 pp_c_identifier (ppi,
646 code == ABS_EXPR ? "__builtin_abs" : "__builtin_ffs");
647 pp_c_left_paren (ppi);
648 pp_c_expression (ppi, TREE_OPERAND (e, 0));
649 pp_c_right_paren (ppi);
654 tree object = TREE_OPERAND (e, 0);
655 if (TREE_CODE (object) == INDIRECT_REF)
657 pp_postfix_expression (ppi, TREE_OPERAND (object, 0));
662 pp_postfix_expression (ppi, object);
665 pp_c_expression (ppi, TREE_OPERAND (e, 1));
672 pp_c_left_paren (ppi);
673 pp_type_id (ppi, TREE_TYPE (e));
674 pp_c_right_paren (ppi);
677 if (code == COMPLEX_CST)
679 pp_c_expression (ppi, TREE_REALPART (e));
680 pp_separate_with (ppi, ',');
681 pp_c_expression (ppi, TREE_IMAGPART (e));
683 else if (code == VECTOR_CST)
684 pp_c_expression_list (ppi, TREE_VECTOR_CST_ELTS (e));
685 else if (code == COMPLEX_EXPR)
687 pp_c_expression (ppi, TREE_OPERAND (e, 0));
688 pp_separate_with (ppi, ',');
689 pp_c_expression (ppi, TREE_OPERAND (e, 1));
692 pp_right_brace (ppi);
695 case COMPOUND_LITERAL_EXPR:
696 e = DECL_INITIAL (e);
699 pp_initializer (ppi, e);
703 pp_c_identifier (ppi, "__builtin_va_arg");
704 pp_c_left_paren (ppi);
705 pp_assignment_expression (ppi, TREE_OPERAND (e, 0));
706 pp_separate_with (ppi, ',');
707 pp_type_id (ppi, TREE_TYPE (e));
708 pp_c_right_paren (ppi);
712 pp_primary_expression (ppi, e);
717 /* Print out an expression-list; E is expected to be a TREE_LIST */
719 pp_c_expression_list (c_pretty_printer ppi, tree e)
721 for (; e != NULL_TREE; e = TREE_CHAIN (e))
723 pp_c_assignment_expression (ppi, TREE_VALUE (e));
725 pp_separate_with (ppi, ',');
733 unary-operator cast-expression
734 sizeof unary-expression
737 unary-operator: one of
742 __alignof__ unary-expression
743 __alignof__ ( type-id )
744 __real__ unary-expression
745 __imag__ unary-expression */
747 pp_c_unary_expression (c_pretty_printer ppi, tree e)
749 enum tree_code code = TREE_CODE (e);
752 case PREINCREMENT_EXPR:
753 case PREDECREMENT_EXPR:
754 pp_identifier (ppi, code == PREINCREMENT_EXPR ? "++" : "--");
755 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
764 if (code == ADDR_EXPR)
766 else if (code == INDIRECT_REF)
768 else if (code == NEGATE_EXPR)
770 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
772 else if (code == TRUTH_NOT_EXPR)
773 pp_exclamation (ppi);
774 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
779 pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
780 pp_c_whitespace (ppi);
781 if (TYPE_P (TREE_OPERAND (e, 0)))
783 pp_c_left_paren (ppi);
784 pp_type_id (ppi, TREE_OPERAND (e, 0));
785 pp_c_right_paren (ppi);
788 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
793 pp_c_identifier (ppi, code == REALPART_EXPR ? "__real__" : "__imag__");
794 pp_c_whitespace (ppi);
795 pp_unary_expression (ppi, TREE_OPERAND (e, 0));
799 pp_postfix_expression (ppi, e);
805 pp_c_cast_expression (c_pretty_printer ppi, tree e)
807 if (TREE_CODE (e) == CONVERT_EXPR || TREE_CODE (e) == FLOAT_EXPR)
809 pp_c_left_paren (ppi);
810 pp_type_id (ppi, TREE_TYPE (e));
811 pp_c_right_paren (ppi);
812 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
815 pp_unary_expression (ppi, e);
819 pp_c_multiplicative_expression (c_pretty_printer ppi, tree e)
821 enum tree_code code = TREE_CODE (e);
827 pp_c_multiplicative_expression (ppi, TREE_OPERAND (e, 0));
828 pp_c_whitespace (ppi);
829 if (code == MULT_EXPR)
831 else if (code == TRUNC_DIV_EXPR)
835 pp_c_whitespace (ppi);
836 pp_c_cast_expression (ppi, TREE_OPERAND (e, 1));
840 pp_c_cast_expression (ppi, e);
846 pp_c_additive_expression (c_pretty_printer ppi, tree e)
848 enum tree_code code = TREE_CODE (e);
853 pp_c_additive_expression (ppi, TREE_OPERAND (e, 0));
854 pp_c_whitespace (ppi);
855 if (code == PLUS_EXPR)
859 pp_c_whitespace (ppi);
860 pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1));
864 pp_multiplicative_expression (ppi, e);
870 pp_c_shift_expression (c_pretty_printer ppi, tree e)
872 enum tree_code code = TREE_CODE (e);
877 pp_c_shift_expression (ppi, TREE_OPERAND (e, 0));
878 pp_c_whitespace (ppi);
879 pp_identifier (ppi, code == LSHIFT_EXPR ? "<<" : ">>");
880 pp_c_whitespace (ppi);
881 pp_c_additive_expression (ppi, TREE_OPERAND (e, 1));
885 pp_c_additive_expression (ppi, e);
890 pp_c_relational_expression (c_pretty_printer ppi, tree e)
892 enum tree_code code = TREE_CODE (e);
899 pp_c_relational_expression (ppi, TREE_OPERAND (e, 0));
900 pp_c_whitespace (ppi);
903 else if (code == GT_EXPR)
905 else if (code == LE_EXPR)
906 pp_identifier (ppi, "<=");
907 else if (code == GE_EXPR)
908 pp_identifier (ppi, ">=");
909 pp_c_whitespace (ppi);
910 pp_c_shift_expression (ppi, TREE_OPERAND (e, 1));
914 pp_c_shift_expression (ppi, e);
920 pp_c_equality_expression (c_pretty_printer ppi, tree e)
922 enum tree_code code = TREE_CODE (e);
927 pp_c_equality_expression (ppi, TREE_OPERAND (e, 0));
928 pp_c_maybe_whitespace (ppi);
929 pp_identifier (ppi, code == EQ_EXPR ? "==" : "!=");
930 pp_c_whitespace (ppi);
931 pp_c_relational_expression (ppi, TREE_OPERAND (e, 1));
935 pp_c_relational_expression (ppi, e);
941 pp_c_and_expression (c_pretty_printer ppi, tree e)
943 if (TREE_CODE (e) == BIT_AND_EXPR)
945 pp_c_and_expression (ppi, TREE_OPERAND (e, 0));
946 pp_c_maybe_whitespace (ppi);
948 pp_c_whitespace (ppi);
949 pp_c_equality_expression (ppi, TREE_OPERAND (e, 1));
952 pp_c_equality_expression (ppi, e);
956 pp_c_exclusive_or_expression (c_pretty_printer ppi, tree e)
958 if (TREE_CODE (e) == BIT_XOR_EXPR)
960 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
961 pp_c_maybe_whitespace (ppi);
963 pp_c_whitespace (ppi);
964 pp_c_and_expression (ppi, TREE_OPERAND (e, 1));
967 pp_c_and_expression (ppi, e);
971 pp_c_inclusive_or_expression (c_pretty_printer ppi, tree e)
973 if (TREE_CODE (e) == BIT_IOR_EXPR)
975 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
976 pp_c_maybe_whitespace (ppi);
978 pp_c_whitespace (ppi);
979 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1));
982 pp_c_exclusive_or_expression (ppi, e);
986 pp_c_logical_and_expression (c_pretty_printer ppi, tree e)
988 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
990 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0));
991 pp_c_maybe_whitespace (ppi);
992 pp_identifier (ppi, "&&");
993 pp_c_whitespace (ppi);
994 pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1));
997 pp_c_inclusive_or_expression (ppi, e);
1001 pp_c_logical_or_expression (c_pretty_printer ppi, tree e)
1003 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1005 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
1006 pp_c_maybe_whitespace (ppi);
1007 pp_identifier (ppi, "||");
1008 pp_c_whitespace (ppi);
1009 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1));
1012 pp_c_logical_and_expression (ppi, e);
1016 pp_c_conditional_expression (c_pretty_printer ppi, tree e)
1018 if (TREE_CODE (e) == COND_EXPR)
1020 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
1021 pp_c_maybe_whitespace (ppi);
1023 pp_c_whitespace (ppi);
1024 pp_c_expression (ppi, TREE_OPERAND (e, 1));
1025 pp_c_maybe_whitespace (ppi);
1027 pp_c_whitespace (ppi);
1028 pp_c_conditional_expression (ppi, TREE_OPERAND (e, 2));
1031 pp_c_logical_or_expression (ppi, e);
1035 /* Pretty-print a C assignment-expression. */
1037 pp_c_assignment_expression (c_pretty_printer ppi, tree e)
1039 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1041 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
1042 pp_c_maybe_whitespace (ppi);
1045 pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1));
1048 pp_c_conditional_expression (ppi, e);
1051 /* Pretty-print an expression. */
1053 pp_c_expression (c_pretty_printer ppi, tree e)
1055 switch (TREE_CODE (e))
1058 pp_c_integer_literal (ppi, e);
1062 pp_c_real_literal (ppi, e);
1066 pp_c_string_literal (ppi, e);
1079 pp_c_primary_expression (ppi, e);
1082 case POSTINCREMENT_EXPR:
1083 case POSTDECREMENT_EXPR:
1093 case COMPOUND_LITERAL_EXPR:
1096 pp_c_postfix_expression (ppi, e);
1104 case TRUTH_NOT_EXPR:
1105 case PREINCREMENT_EXPR:
1106 case PREDECREMENT_EXPR:
1111 pp_c_unary_expression (ppi, e);
1116 pp_c_cast_expression (ppi, e);
1120 case TRUNC_MOD_EXPR:
1121 case TRUNC_DIV_EXPR:
1122 pp_c_multiplicative_expression (ppi, e);
1127 pp_c_shift_expression (ppi, e);
1134 pp_c_relational_expression (ppi, e);
1138 pp_c_and_expression (ppi, e);
1142 pp_c_exclusive_or_expression (ppi, e);
1146 pp_c_inclusive_or_expression (ppi, e);
1149 case TRUTH_ANDIF_EXPR:
1150 pp_c_logical_and_expression (ppi, e);
1153 case TRUTH_ORIF_EXPR:
1154 pp_c_logical_or_expression (ppi, e);
1158 pp_c_conditional_expression (ppi, e);
1163 pp_c_assignment_expression (ppi, e);
1167 pp_c_expression (ppi, TREE_OPERAND (e, 0));
1171 pp_c_left_paren (ppi);
1172 pp_c_expression (ppi, TREE_OPERAND (e, 0));
1173 pp_separate_with (ppi, ',');
1174 pp_assignment_expression (ppi, TREE_OPERAND (e, 1));
1175 pp_c_right_paren (ppi);
1180 pp_unsupported_tree (ppi, e);
1188 pp_c_statement (c_pretty_printer ppi, tree stmt)
1190 const enum tree_code code = TREE_CODE (stmt);
1196 if (code == LABEL_STMT)
1197 pp_tree_identifier (ppi, DECL_NAME (LABEL_STMT_LABEL (stmt)));
1198 else if (code == LABEL_STMT)
1200 if (CASE_LOW (stmt) == NULL_TREE)
1201 pp_identifier (ppi, "default");
1204 pp_c_identifier (ppi, "case");
1205 pp_c_whitespace (ppi);
1206 pp_conditional_expression (ppi, CASE_LOW (stmt));
1207 if (CASE_HIGH (stmt))
1209 pp_identifier (ppi, "...");
1210 pp_conditional_expression (ppi, CASE_HIGH (stmt));
1215 pp_newline_and_indent (ppi, 3);
1219 pp_left_brace (ppi);
1220 pp_newline_and_indent (ppi, 3);
1221 for (stmt = COMPOUND_BODY (stmt); stmt; stmt = TREE_CHAIN (stmt))
1222 pp_c_statement (ppi, stmt);
1223 pp_newline_and_indent (ppi, -3);
1224 pp_right_brace (ppi);
1231 pp_c_expression (ppi, code == EXPR_STMT
1232 ? EXPR_STMT_EXPR (stmt)
1233 : CLEANUP_EXPR (stmt));
1239 pp_c_identifier (ppi, "if");
1241 pp_c_left_paren (ppi);
1242 pp_c_expression (ppi, IF_COND (stmt));
1243 pp_right_paren (ppi);
1244 pp_newline_and_indent (ppi, 3);
1245 pp_statement (ppi, THEN_CLAUSE (stmt));
1246 pp_newline_and_indent (ppi, -3);
1247 if (ELSE_CLAUSE (stmt))
1249 tree else_clause = ELSE_CLAUSE (stmt);
1250 pp_c_identifier (ppi, "else");
1251 if (TREE_CODE (else_clause) == IF_STMT)
1252 pp_c_whitespace (ppi);
1254 pp_newline_and_indent (ppi, 3);
1255 pp_statement (ppi, else_clause);
1256 if (TREE_CODE (else_clause) != IF_STMT)
1257 pp_newline_and_indent (ppi, -3);
1263 pp_c_identifier (ppi, "switch");
1265 pp_c_left_paren (ppi);
1266 pp_c_expression (ppi, SWITCH_COND (stmt));
1267 pp_right_paren (ppi);
1268 pp_newline_and_indent (ppi, 3);
1269 pp_statement (ppi, SWITCH_BODY (stmt));
1270 pp_newline_and_indent (ppi, -3);
1274 pp_c_identifier (ppi, "while");
1276 pp_c_left_paren (ppi);
1277 pp_c_expression (ppi, WHILE_COND (stmt));
1278 pp_right_paren (ppi);
1279 pp_newline_and_indent (ppi, 3);
1280 pp_statement (ppi, WHILE_BODY (stmt));
1281 pp_newline_and_indent (ppi, -3);
1285 pp_c_identifier (ppi, "do");
1286 pp_newline_and_indent (ppi, 3);
1287 pp_statement (ppi, DO_BODY (stmt));
1288 pp_newline_and_indent (ppi, -3);
1289 pp_c_identifier (ppi, "while");
1291 pp_c_left_paren (ppi);
1292 pp_c_expression (ppi, DO_COND (stmt));
1293 pp_c_right_paren (ppi);
1299 pp_c_identifier (ppi, "for");
1301 pp_c_left_paren (ppi);
1302 pp_statement (ppi, FOR_INIT_STMT (stmt));
1303 pp_c_whitespace (ppi);
1304 if (FOR_COND (stmt))
1305 pp_c_expression (ppi, FOR_COND (stmt));
1307 pp_c_whitespace (ppi);
1308 if (FOR_EXPR (stmt))
1309 pp_c_expression (ppi, FOR_EXPR (stmt));
1310 pp_right_paren (ppi);
1311 pp_newline_and_indent (ppi, 3);
1312 pp_statement (ppi, FOR_BODY (stmt));
1313 pp_newline_and_indent (ppi, -3);
1319 pp_identifier (ppi, code == BREAK_STMT ? "break" : "continue");
1327 tree e = code == RETURN_STMT
1328 ? RETURN_STMT_EXPR (stmt)
1329 : GOTO_DESTINATION (stmt);
1332 pp_c_identifier (ppi, code == RETURN_STMT ? "return" : "goto");
1334 pp_c_expression (ppi, e);
1341 if (!SCOPE_NULLIFIED_P (stmt) && SCOPE_NO_CLEANUPS_P (stmt))
1343 if (SCOPE_BEGIN_P (stmt))
1345 pp_left_brace (ppi);
1346 pp_newline_and_indent (ppi, 3);
1348 else if (SCOPE_END_P (stmt))
1350 pp_right_brace (ppi);
1351 pp_newline_and_indent (ppi, -3);
1357 pp_declaration (ppi, DECL_STMT_DECL (stmt));
1364 bool has_volatile_p = ASM_VOLATILE_P (stmt);
1365 bool is_extended = has_volatile_p || ASM_INPUTS (stmt)
1366 || ASM_OUTPUTS (stmt) || ASM_CLOBBERS (stmt);
1367 pp_c_identifier (ppi, is_extended ? "__asm__" : "asm");
1369 pp_c_identifier (ppi, "__volatile__");
1371 pp_c_left_paren (ppi);
1372 pp_c_string_literal (ppi, ASM_STRING (stmt));
1376 pp_separate_with (ppi, ':');
1377 if (ASM_OUTPUTS (stmt))
1378 pp_c_expression (ppi, ASM_OUTPUTS (stmt));
1380 pp_separate_with (ppi, ':');
1381 if (ASM_INPUTS (stmt))
1382 pp_c_expression (ppi, ASM_INPUTS (stmt));
1384 pp_separate_with (ppi, ':');
1385 if (ASM_CLOBBERS (stmt))
1386 pp_c_expression (ppi, ASM_CLOBBERS (stmt));
1388 pp_right_paren (ppi);
1394 pp_c_identifier (ppi, "__FILE__");
1397 pp_c_whitespace (ppi);
1398 pp_c_identifier (ppi, FILE_STMT_FILENAME (stmt));
1404 pp_unsupported_tree (ppi, stmt);
1410 /* Initialize the PRETTY-PRINTER for handling C codes. */
1412 pp_c_pretty_printer_init (c_pretty_printer pp)
1414 pp->offset_list = 0;
1416 pp->declaration = pp_c_declaration;
1417 pp->declaration_specifiers = pp_c_declaration_specifiers;
1418 pp->type_specifier = pp_c_simple_type_specifier;
1419 pp->declarator = pp_c_declarator;
1420 pp->direct_declarator = pp_c_direct_declarator;
1421 pp->parameter_declaration = pp_c_parameter_declaration;
1422 pp->type_id = pp_c_type_id;
1424 pp->statement = pp_c_statement;
1426 pp->primary_expression = pp_c_primary_expression;
1427 pp->postfix_expression = pp_c_postfix_expression;
1428 pp->unary_expression = pp_c_unary_expression;
1429 pp->initializer = pp_c_initializer;
1430 pp->multiplicative_expression = pp_c_multiplicative_expression;
1431 pp->conditional_expression = pp_c_conditional_expression;
1432 pp->assignment_expression = pp_c_assignment_expression;